kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package control |
| 21 | |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 22 | import ( |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 23 | "fmt" |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 25 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 29 | //----------------------------------------------------------------------------- |
| 30 | // |
| 31 | //----------------------------------------------------------------------------- |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 32 | type Registry struct { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 33 | mutex sync.Mutex |
| 34 | register map[uint16]*Subscription |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 35 | subIds []uint16 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 36 | rtmgrClient *RtmgrClient |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 39 | // This method should run as a constructor |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 40 | func (r *Registry) Initialize() { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 41 | r.register = make(map[uint16]*Subscription) |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 42 | var i uint16 |
| 43 | for i = 0; i < 65535; i++ { |
| 44 | r.subIds = append(r.subIds, i+1) |
| 45 | } |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 48 | // Reserves and returns the next free sequence number |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 49 | func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 50 | r.mutex.Lock() |
| 51 | defer r.mutex.Unlock() |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 52 | if len(r.subIds) > 0 { |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 53 | sequenceNumber := r.subIds[0] |
| 54 | r.subIds = r.subIds[1:] |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 55 | if _, ok := r.register[sequenceNumber]; ok == false { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 56 | subs := &Subscription{ |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 57 | registry: r, |
| 58 | Seq: sequenceNumber, |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 59 | Meid: trans.Meid, |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 60 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 61 | err := subs.AddEndpoint(trans.GetEndpoint()) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | subs.SubReqMsg = subReqMsg |
| 66 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 67 | r.register[sequenceNumber] = subs |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 68 | xapp.Logger.Debug("Registry: Create %s", subs.String()) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 69 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 70 | return subs, nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 71 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 72 | } |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 73 | return nil, fmt.Errorf("Registry: Failed to reserves subscription") |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 74 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 75 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 76 | func (r *Registry) GetSubscription(sn uint16) *Subscription { |
| 77 | r.mutex.Lock() |
| 78 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 79 | if _, ok := r.register[sn]; ok { |
| 80 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 81 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 82 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 85 | func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) { |
| 86 | r.mutex.Lock() |
| 87 | defer r.mutex.Unlock() |
| 88 | for _, id := range ids { |
| 89 | if _, ok := r.register[id]; ok { |
| 90 | return r.register[id], nil |
| 91 | } |
| 92 | } |
| 93 | return nil, fmt.Errorf("No valid subscription found with ids %v", ids) |
| 94 | } |
| 95 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 96 | func (r *Registry) DelSubscription(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 97 | r.mutex.Lock() |
| 98 | defer r.mutex.Unlock() |
| 99 | if _, ok := r.register[sn]; ok { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 100 | subs := r.register[sn] |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 101 | xapp.Logger.Debug("Registry: Delete %s", subs.String()) |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 102 | r.subIds = append(r.subIds, sn) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 103 | delete(r.register, sn) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 104 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 105 | return true |
| 106 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 107 | return false |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 108 | } |