RICPLT-2962 Preparation for subs merge
[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         Seq    uint16
35         Active bool
36         //
37         Meid        *xapp.RMRMeid
38         RmrEndpoint // xapp endpoint. Now only one xapp can have relation to single subscription. To be changed in merge
39         Trans       *Transaction
40 }
41
42 func (s *Subscription) String() string {
43         s.mutex.Lock()
44         defer s.mutex.Unlock()
45         return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
46 }
47
48 func (s *Subscription) GetSubId() uint16 {
49         s.mutex.Lock()
50         defer s.mutex.Unlock()
51         return s.Seq
52 }
53
54 func (s *Subscription) GetMeid() *xapp.RMRMeid {
55         s.mutex.Lock()
56         defer s.mutex.Unlock()
57         if s.Meid != nil {
58                 return s.Meid
59         }
60         return nil
61 }
62
63 func (s *Subscription) Confirmed() {
64         s.mutex.Lock()
65         defer s.mutex.Unlock()
66         s.Active = true
67 }
68
69 func (s *Subscription) UnConfirmed() {
70         s.mutex.Lock()
71         defer s.mutex.Unlock()
72         s.Active = false
73 }
74
75 func (s *Subscription) IsConfirmed() bool {
76         s.mutex.Lock()
77         defer s.mutex.Unlock()
78         return s.Active
79 }
80
81 func (s *Subscription) SetTransaction(trans *Transaction) error {
82         s.mutex.Lock()
83         defer s.mutex.Unlock()
84
85         subString := strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
86
87         if (s.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (s.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
88                 return fmt.Errorf("Subscription: %s endpoint mismatch with trans: %s", subString, trans)
89         }
90         if s.Trans != nil {
91                 return fmt.Errorf("Subscription: %s trans %s exist, can not register %s", subString, s.Trans, trans)
92         }
93         trans.Subs = s
94         s.Trans = trans
95         return nil
96 }
97
98 func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
99         s.mutex.Lock()
100         defer s.mutex.Unlock()
101         if trans == nil || trans == s.Trans {
102                 s.Trans = nil
103                 return true
104         }
105         return false
106 }
107
108 func (s *Subscription) GetTransaction() *Transaction {
109         s.mutex.Lock()
110         defer s.mutex.Unlock()
111         return s.Trans
112 }
113
114 func (s *Subscription) UpdateRoute(act Action, rtmgrClient *RtmgrClient) error {
115         s.mutex.Lock()
116         defer s.mutex.Unlock()
117         xapp.Logger.Info("Subscription: Starting routing manager route add. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
118         subRouteAction := SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
119         err := rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
120         if err != nil {
121                 return fmt.Errorf("Subscription: Failed to add route. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
122         }
123         return nil
124 }