2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
24 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
25 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
33 type Registry struct {
35 register map[uint16]*Subscription
37 rtmgrClient *RtmgrClient
40 func (r *Registry) Initialize() {
41 r.register = make(map[uint16]*Subscription)
43 for i = 0; i < 65535; i++ {
44 r.subIds = append(r.subIds, i+1)
48 func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
50 defer r.mutex.Unlock()
52 var sequenceNumber uint16
55 // Allocate subscription
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")
65 return nil, fmt.Errorf("Registry: Failed to reserves subscription no free ids")
67 subs := &Subscription{
74 // Add to subscription
77 defer subs.mutex.Unlock()
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")
83 epamount := subs.EpList.Size()
87 // Subscription route updates
91 subRouteAction := SubRouteInfo{CREATE, subs.EpList, subs.Seq}
92 err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
94 subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
95 err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
100 r.subIds = append(r.subIds, sequenceNumber)
103 subs.SubReqMsg = subReqMsg
105 r.register[sequenceNumber] = subs
106 xapp.Logger.Debug("Registry: Create %s", subs.String())
107 xapp.Logger.Debug("Registry: substable=%v", r.register)
111 func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error {
113 defer r.mutex.Unlock()
115 defer subs.mutex.Unlock()
117 delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint())
118 epamount := subs.EpList.Size()
121 // If last endpoint remove from register map
124 if _, ok := r.register[subs.Seq]; ok {
125 xapp.Logger.Debug("Registry: Delete %s", subs.String())
126 delete(r.register, subs.Seq)
127 xapp.Logger.Debug("Registry: substable=%v", r.register)
133 // Wait some time before really do route updates
135 if waitRouteClean > 0 {
137 time.Sleep(waitRouteClean)
141 xapp.Logger.Info("Registry: Cleaning %s", subs.String())
144 // Subscription route updates
148 tmpList := RmrEndpointList{}
149 tmpList.AddEndpoint(trans.GetEndpoint())
150 subRouteAction := SubRouteInfo{DELETE, tmpList, subs.Seq}
151 r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
153 subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
154 r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
160 // If last endpoint free seq nro
163 r.subIds = append(r.subIds, subs.Seq)
169 func (r *Registry) GetSubscription(sn uint16) *Subscription {
171 defer r.mutex.Unlock()
172 if _, ok := r.register[sn]; ok {
173 return r.register[sn]
178 func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
180 defer r.mutex.Unlock()
181 for _, id := range ids {
182 if _, ok := r.register[id]; ok {
183 return r.register[id], nil
186 return nil, fmt.Errorf("No valid subscription found with ids %v", ids)