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"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 type Subscription struct {
35 mutex sync.Mutex // Lock
36 registry *Registry // Registry
38 Meid *xapp.RMRMeid // Meid/ RanName
39 EpList RmrEndpointList // Endpoints
40 DelEpList RmrEndpointList // Endpoints
42 TransLock sync.Mutex // Lock transactions, only one executed per time for subs
43 TheTrans *Transaction // Ongoing transaction from xapp
44 SubReqMsg *e2ap.E2APSubscriptionRequest // Subscription information
45 SubRespMsg *e2ap.E2APSubscriptionResponse // Subscription information
48 func (s *Subscription) stringImpl() string {
49 return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
52 func (s *Subscription) String() string {
54 defer s.mutex.Unlock()
58 func (s *Subscription) GetSubId() uint16 {
60 defer s.mutex.Unlock()
64 func (s *Subscription) GetMeid() *xapp.RMRMeid {
66 defer s.mutex.Unlock()
73 func (s *Subscription) AddEndpoint(ep *RmrEndpoint) error {
75 defer s.mutex.Unlock()
77 return fmt.Errorf("AddEndpoint no endpoint given")
79 if s.EpList.AddEndpoint(ep) {
80 s.DelEpList.DelEndpoint(ep)
81 if s.EpList.Size() == 1 {
82 return s.updateRouteImpl(CREATE)
84 return s.updateRouteImpl(MERGE)
89 func (s *Subscription) DelEndpoint(ep *RmrEndpoint) error {
91 defer s.mutex.Unlock()
94 return fmt.Errorf("DelEndpoint no endpoint given")
96 if s.EpList.HasEndpoint(ep) == false {
97 return fmt.Errorf("DelEndpoint endpoint not found")
99 if s.DelEpList.HasEndpoint(ep) == true {
100 return fmt.Errorf("DelEndpoint endpoint already under del")
102 s.DelEpList.AddEndpoint(ep)
107 func (s *Subscription) CleanCheck() {
109 defer s.mutex.Unlock()
111 // Only one clean ongoing
115 var currSeq uint64 = 0
116 // Make sure that routes to be deleted
117 // are not deleted too fast
118 for currSeq < s.DelSeq {
121 time.Sleep(5 * time.Second)
124 xapp.Logger.Info("DelEndpoint: delete cleaning %s", s.stringImpl())
125 if s.EpList.Size() <= s.DelEpList.Size() {
126 s.updateRouteImpl(DELETE)
127 go s.registry.DelSubscription(s.Seq)
128 } else if s.EpList.DelEndpoints(&s.DelEpList) {
129 s.updateRouteImpl(MERGE)
135 func (s *Subscription) IsTransactionReserved() bool {
137 defer s.mutex.Unlock()
138 if s.TheTrans != nil {
145 func (s *Subscription) GetTransaction() *Transaction {
147 defer s.mutex.Unlock()
151 func (s *Subscription) WaitTransactionTurn(trans *Transaction) {
158 func (s *Subscription) ReleaseTransactionTurn(trans *Transaction) {
160 if trans != nil && trans == s.TheTrans {
167 func (s *Subscription) updateRouteImpl(act Action) error {
168 subRouteAction := SubRouteInfo{act, s.EpList, s.Seq}
169 err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
171 return fmt.Errorf("%s %s", s.stringImpl(), err.Error())