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 ( |
| 23 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 24 | "strconv" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 25 | "sync" |
| 26 | ) |
| 27 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 28 | //----------------------------------------------------------------------------- |
| 29 | // |
| 30 | //----------------------------------------------------------------------------- |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 31 | type Subscription struct { |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 32 | mutex sync.Mutex |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 33 | Seq uint16 |
| 34 | Active bool |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 35 | // |
| 36 | Meid *xapp.RMRMeid |
| 37 | RmrEndpoint // xapp endpoint |
| 38 | Trans *Transaction |
| 39 | } |
| 40 | |
| 41 | func (s *Subscription) String() string { |
| 42 | s.mutex.Lock() |
| 43 | defer s.mutex.Unlock() |
| 44 | return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func (s *Subscription) Confirmed() { |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 48 | s.mutex.Lock() |
| 49 | defer s.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 50 | s.Active = true |
| 51 | } |
| 52 | |
| 53 | func (s *Subscription) UnConfirmed() { |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 54 | s.mutex.Lock() |
| 55 | defer s.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 56 | s.Active = false |
| 57 | } |
| 58 | |
| 59 | func (s *Subscription) IsConfirmed() bool { |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 60 | s.mutex.Lock() |
| 61 | defer s.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 62 | return s.Active |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 65 | func (s *Subscription) SetTransaction(trans *Transaction) bool { |
| 66 | s.mutex.Lock() |
| 67 | defer s.mutex.Unlock() |
| 68 | if s.Trans == nil { |
| 69 | s.Trans = trans |
| 70 | return true |
| 71 | } |
| 72 | return false |
| 73 | } |
| 74 | |
| 75 | func (s *Subscription) UnSetTransaction(trans *Transaction) bool { |
| 76 | s.mutex.Lock() |
| 77 | defer s.mutex.Unlock() |
| 78 | if trans == nil || trans == s.Trans { |
| 79 | s.Trans = nil |
| 80 | return true |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | func (s *Subscription) GetTransaction() *Transaction { |
| 86 | s.mutex.Lock() |
| 87 | defer s.mutex.Unlock() |
| 88 | return s.Trans |
| 89 | } |
| 90 | |
| 91 | func (s *Subscription) SubRouteInfo(act Action) SubRouteInfo { |
| 92 | s.mutex.Lock() |
| 93 | defer s.mutex.Unlock() |
| 94 | return SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq} |
| 95 | } |
| 96 | |
| 97 | //----------------------------------------------------------------------------- |
| 98 | // |
| 99 | //----------------------------------------------------------------------------- |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 100 | type Registry struct { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 101 | register map[uint16]*Subscription |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 102 | counter uint16 |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 103 | mutex sync.Mutex |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 106 | // This method should run as a constructor |
| 107 | func (r *Registry) Initialize(seedsn uint16) { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 108 | r.register = make(map[uint16]*Subscription) |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 109 | r.counter = seedsn |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 112 | // Reserves and returns the next free sequence number |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 113 | func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription { |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 114 | // Check is current SequenceNumber valid |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 115 | // Allocate next SequenceNumber value and retry N times |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 116 | r.mutex.Lock() |
| 117 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 118 | var subs *Subscription = nil |
| 119 | var retrytimes uint16 = 1000 |
| 120 | for ; subs == nil && retrytimes > 0; retrytimes-- { |
| 121 | sequenceNumber := r.counter |
| 122 | if r.counter == 65535 { |
| 123 | r.counter = 0 |
| 124 | } else { |
| 125 | r.counter++ |
| 126 | } |
| 127 | if _, ok := r.register[sequenceNumber]; ok == false { |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame^] | 128 | r.register[sequenceNumber] = &Subscription{ |
| 129 | Seq: sequenceNumber, |
| 130 | Active: false, |
| 131 | RmrEndpoint: endPoint, |
| 132 | Meid: meid, |
| 133 | Trans: nil, |
| 134 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 135 | return r.register[sequenceNumber] |
| 136 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 137 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 138 | return nil |
| 139 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 140 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 141 | func (r *Registry) GetSubscription(sn uint16) *Subscription { |
| 142 | r.mutex.Lock() |
| 143 | defer r.mutex.Unlock() |
| 144 | xapp.Logger.Debug("Registry map: %v", r.register) |
| 145 | if _, ok := r.register[sn]; ok { |
| 146 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 147 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 148 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 151 | //This function releases the given id as unused in the register |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 152 | func (r *Registry) releaseSequenceNumber(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 153 | r.mutex.Lock() |
| 154 | defer r.mutex.Unlock() |
| 155 | if _, ok := r.register[sn]; ok { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 156 | delete(r.register, sn) |
| 157 | return true |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 158 | } else { |
| 159 | return false |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 160 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 161 | } |