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" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 25 | "sync" |
| 26 | ) |
| 27 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 28 | //----------------------------------------------------------------------------- |
| 29 | // |
| 30 | //----------------------------------------------------------------------------- |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 31 | type Registry struct { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 32 | mutex sync.Mutex |
| 33 | register map[uint16]*Subscription |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 34 | subIds []uint16 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 35 | rtmgrClient *RtmgrClient |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 38 | // This method should run as a constructor |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 39 | func (r *Registry) Initialize() { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 40 | r.register = make(map[uint16]*Subscription) |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 41 | var i uint16 |
| 42 | for i = 0; i < 65535; i++ { |
| 43 | r.subIds = append(r.subIds, i+1) |
| 44 | } |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 47 | // Reserves and returns the next free sequence number |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 48 | func (r *Registry) ReserveSubscription(endPoint *RmrEndpoint, meid *xapp.RMRMeid) (*Subscription, error) { |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 49 | // Check is current SequenceNumber valid |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 50 | // Allocate next SequenceNumber value and retry N times |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 51 | r.mutex.Lock() |
| 52 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 53 | var subs *Subscription = nil |
| 54 | var retrytimes uint16 = 1000 |
| 55 | for ; subs == nil && retrytimes > 0; retrytimes-- { |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 56 | sequenceNumber := r.subIds[0] |
| 57 | r.subIds = r.subIds[1:] |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 58 | if _, ok := r.register[sequenceNumber]; ok == false { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 59 | subs := &Subscription{ |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 60 | registry: r, |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 61 | Seq: sequenceNumber, |
| 62 | Active: false, |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 63 | RmrEndpoint: *endPoint, |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 64 | Meid: meid, |
| 65 | Trans: nil, |
| 66 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 67 | r.register[sequenceNumber] = subs |
| 68 | |
| 69 | // Update routing |
| 70 | r.mutex.Unlock() |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 71 | err := subs.UpdateRoute(CREATE) |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 72 | r.mutex.Lock() |
| 73 | if err != nil { |
| 74 | if _, ok := r.register[sequenceNumber]; ok { |
| 75 | delete(r.register, sequenceNumber) |
| 76 | } |
| 77 | return nil, err |
| 78 | } |
| 79 | return subs, nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 80 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 81 | } |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 82 | return nil, fmt.Errorf("Registry: Failed to reserves subscription. RmrEndpoint: %s, Meid: %s", endPoint, meid.RanName) |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 83 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 84 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 85 | func (r *Registry) GetSubscription(sn uint16) *Subscription { |
| 86 | r.mutex.Lock() |
| 87 | defer r.mutex.Unlock() |
| 88 | xapp.Logger.Debug("Registry map: %v", r.register) |
| 89 | if _, ok := r.register[sn]; ok { |
| 90 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 91 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 92 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 95 | func (r *Registry) DelSubscription(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 96 | r.mutex.Lock() |
| 97 | defer r.mutex.Unlock() |
| 98 | if _, ok := r.register[sn]; ok { |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 99 | r.subIds = append(r.subIds, sn) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 100 | delete(r.register, sn) |
| 101 | return true |
| 102 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 103 | return false |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 104 | } |