6fd806c610ce7c3761a9cd8d593c4e25e800a6fe
[ric-plt/submgr.git] / pkg / control / subscription.go
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
22 import (
23         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25         "strconv"
26         "sync"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type Subscription struct {
33         mutex    sync.Mutex
34         registry *Registry
35         Seq      uint16
36         Active   bool
37         //
38         Meid   *xapp.RMRMeid
39         EpList RmrEndpointList
40         Trans  *Transaction
41 }
42
43 func (s *Subscription) stringImpl() string {
44         return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
45 }
46
47 func (s *Subscription) String() string {
48         s.mutex.Lock()
49         defer s.mutex.Unlock()
50         return s.stringImpl()
51 }
52
53 func (s *Subscription) GetSubId() uint16 {
54         s.mutex.Lock()
55         defer s.mutex.Unlock()
56         return s.Seq
57 }
58
59 func (s *Subscription) GetMeid() *xapp.RMRMeid {
60         s.mutex.Lock()
61         defer s.mutex.Unlock()
62         if s.Meid != nil {
63                 return s.Meid
64         }
65         return nil
66 }
67
68 func (s *Subscription) Confirmed() {
69         s.mutex.Lock()
70         defer s.mutex.Unlock()
71         s.Active = true
72 }
73
74 func (s *Subscription) UnConfirmed() {
75         s.mutex.Lock()
76         defer s.mutex.Unlock()
77         s.Active = false
78 }
79
80 func (s *Subscription) IsConfirmed() bool {
81         s.mutex.Lock()
82         defer s.mutex.Unlock()
83         return s.Active
84 }
85
86 func (s *Subscription) IsEndpoint(ep *RmrEndpoint) bool {
87         s.mutex.Lock()
88         defer s.mutex.Unlock()
89         return s.EpList.HasEndpoint(ep)
90 }
91
92 func (s *Subscription) SetTransaction(trans *Transaction) error {
93         s.mutex.Lock()
94         defer s.mutex.Unlock()
95
96         if s.Trans != nil {
97                 return fmt.Errorf("subs(%s) trans(%s) exist, can not register trans(%s)", s.stringImpl(), s.Trans, trans)
98         }
99         trans.Subs = s
100         s.Trans = trans
101
102         if len(s.EpList.Endpoints) == 0 {
103                 s.EpList.Endpoints = append(s.EpList.Endpoints, trans.RmrEndpoint)
104                 return s.updateRouteImpl(CREATE)
105         } else if s.EpList.HasEndpoint(&trans.RmrEndpoint) == false {
106                 s.EpList.Endpoints = append(s.EpList.Endpoints, trans.RmrEndpoint)
107                 return s.updateRouteImpl(MERGE)
108         }
109         return nil
110 }
111
112 func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
113         s.mutex.Lock()
114         defer s.mutex.Unlock()
115         if trans == nil || trans == s.Trans {
116                 s.Trans = nil
117                 return true
118         }
119         return false
120 }
121
122 func (s *Subscription) GetTransaction() *Transaction {
123         s.mutex.Lock()
124         defer s.mutex.Unlock()
125         return s.Trans
126 }
127
128 func (s *Subscription) updateRouteImpl(act Action) error {
129         subRouteAction := SubRouteInfo{act, s.EpList, s.Seq}
130         err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
131         if err != nil {
132                 return fmt.Errorf("subs(%s) %s", s.stringImpl(), err.Error())
133         }
134         return nil
135 }
136
137 func (s *Subscription) UpdateRoute(act Action) error {
138         s.mutex.Lock()
139         defer s.mutex.Unlock()
140         return s.updateRouteImpl(act)
141 }
142
143 func (s *Subscription) Release() {
144         s.registry.DelSubscription(s.Seq)
145         err := s.UpdateRoute(DELETE)
146         if err != nil {
147                 xapp.Logger.Error("%s", err.Error())
148         }
149 }