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" |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 27 | "time" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 30 | //----------------------------------------------------------------------------- |
| 31 | // |
| 32 | //----------------------------------------------------------------------------- |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 33 | type Registry struct { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 34 | mutex sync.Mutex |
| 35 | register map[uint16]*Subscription |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 36 | subIds []uint16 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 37 | rtmgrClient *RtmgrClient |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 38 | } |
| 39 | |
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 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 48 | func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 49 | r.mutex.Lock() |
| 50 | defer r.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 51 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 52 | var sequenceNumber uint16 |
| 53 | |
| 54 | // |
| 55 | // Allocate subscription |
| 56 | // |
| 57 | if len(r.subIds) > 0 { |
| 58 | sequenceNumber = r.subIds[0] |
| 59 | r.subIds = r.subIds[1:] |
| 60 | if _, ok := r.register[sequenceNumber]; ok == true { |
| 61 | r.subIds = append(r.subIds, sequenceNumber) |
| 62 | return nil, fmt.Errorf("Registry: Failed to reserves subscription") |
| 63 | } |
| 64 | } else { |
| 65 | return nil, fmt.Errorf("Registry: Failed to reserves subscription no free ids") |
| 66 | } |
| 67 | subs := &Subscription{ |
| 68 | registry: r, |
| 69 | Seq: sequenceNumber, |
| 70 | Meid: trans.Meid, |
| 71 | } |
| 72 | |
| 73 | // |
| 74 | // Add to subscription |
| 75 | // |
| 76 | subs.mutex.Lock() |
| 77 | defer subs.mutex.Unlock() |
| 78 | |
| 79 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
| 80 | r.subIds = append(r.subIds, sequenceNumber) |
| 81 | return nil, fmt.Errorf("Registry: Endpoint existing already in subscription") |
| 82 | } |
| 83 | epamount := subs.EpList.Size() |
| 84 | |
| 85 | r.mutex.Unlock() |
| 86 | // |
| 87 | // Subscription route updates |
| 88 | // |
| 89 | var err error |
| 90 | if epamount == 1 { |
| 91 | subRouteAction := SubRouteInfo{CREATE, subs.EpList, subs.Seq} |
| 92 | err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 93 | } else { |
| 94 | subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq} |
| 95 | err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 96 | } |
| 97 | r.mutex.Lock() |
| 98 | |
| 99 | if err != nil { |
| 100 | r.subIds = append(r.subIds, sequenceNumber) |
| 101 | return nil, err |
| 102 | } |
| 103 | subs.SubReqMsg = subReqMsg |
| 104 | |
| 105 | r.register[sequenceNumber] = subs |
| 106 | xapp.Logger.Debug("Registry: Create %s", subs.String()) |
| 107 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 108 | return subs, nil |
| 109 | } |
| 110 | |
| 111 | func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error { |
| 112 | r.mutex.Lock() |
| 113 | defer r.mutex.Unlock() |
| 114 | subs.mutex.Lock() |
| 115 | defer subs.mutex.Unlock() |
| 116 | |
| 117 | delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint()) |
| 118 | epamount := subs.EpList.Size() |
| 119 | |
| 120 | // |
| 121 | // If last endpoint remove from register map |
| 122 | // |
| 123 | if epamount == 0 { |
| 124 | if _, ok := r.register[subs.Seq]; ok { |
| 125 | xapp.Logger.Debug("Registry: Delete %s", subs.String()) |
| 126 | delete(r.register, subs.Seq) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 127 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 128 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 129 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 130 | r.mutex.Unlock() |
| 131 | |
| 132 | // |
| 133 | // Wait some time before really do route updates |
| 134 | // |
| 135 | if waitRouteClean > 0 { |
| 136 | subs.mutex.Unlock() |
| 137 | time.Sleep(waitRouteClean) |
| 138 | subs.mutex.Lock() |
| 139 | } |
| 140 | |
| 141 | xapp.Logger.Info("Registry: Cleaning %s", subs.String()) |
| 142 | |
| 143 | // |
| 144 | // Subscription route updates |
| 145 | // |
| 146 | if delStatus { |
| 147 | if epamount == 0 { |
| 148 | tmpList := RmrEndpointList{} |
| 149 | tmpList.AddEndpoint(trans.GetEndpoint()) |
| 150 | subRouteAction := SubRouteInfo{DELETE, tmpList, subs.Seq} |
| 151 | r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 152 | } else { |
| 153 | subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq} |
| 154 | r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | r.mutex.Lock() |
| 159 | // |
| 160 | // If last endpoint free seq nro |
| 161 | // |
| 162 | if epamount == 0 { |
| 163 | r.subIds = append(r.subIds, subs.Seq) |
| 164 | } |
| 165 | |
| 166 | return nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 167 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 168 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 169 | func (r *Registry) GetSubscription(sn uint16) *Subscription { |
| 170 | r.mutex.Lock() |
| 171 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 172 | if _, ok := r.register[sn]; ok { |
| 173 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 174 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 175 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 178 | func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) { |
| 179 | r.mutex.Lock() |
| 180 | defer r.mutex.Unlock() |
| 181 | for _, id := range ids { |
| 182 | if _, ok := r.register[id]; ok { |
| 183 | return r.register[id], nil |
| 184 | } |
| 185 | } |
| 186 | return nil, fmt.Errorf("No valid subscription found with ids %v", ids) |
| 187 | } |