9bbe3d47dd12675fad0c157683a82bd2d4c44abc
[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) Confirmed() {
49         s.mutex.Lock()
50         defer s.mutex.Unlock()
51         s.Active = true
52 }
53
54 func (s *Subscription) UnConfirmed() {
55         s.mutex.Lock()
56         defer s.mutex.Unlock()
57         s.Active = false
58 }
59
60 func (s *Subscription) IsConfirmed() bool {
61         s.mutex.Lock()
62         defer s.mutex.Unlock()
63         return s.Active
64 }
65
66 func (s *Subscription) SetTransaction(trans *Transaction) error {
67         s.mutex.Lock()
68         defer s.mutex.Unlock()
69
70         subString := strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
71
72         if (s.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (s.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
73                 return fmt.Errorf("Subscription: %s endpoint mismatch with trans: %s", subString, trans)
74         }
75         if s.Trans != nil {
76                 return fmt.Errorf("Subscription: %s trans %s exist, can not register %s", subString, s.Trans, trans)
77         }
78         trans.Subs = s
79         s.Trans = trans
80         return nil
81 }
82
83 func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
84         s.mutex.Lock()
85         defer s.mutex.Unlock()
86         if trans == nil || trans == s.Trans {
87                 s.Trans = nil
88                 return true
89         }
90         return false
91 }
92
93 func (s *Subscription) GetTransaction() *Transaction {
94         s.mutex.Lock()
95         defer s.mutex.Unlock()
96         return s.Trans
97 }
98
99 func (s *Subscription) UpdateRoute(act Action, rtmgrClient *RtmgrClient) error {
100         s.mutex.Lock()
101         defer s.mutex.Unlock()
102         xapp.Logger.Info("Subscription: Starting routing manager route add. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
103         subRouteAction := SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
104         err := rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
105         if err != nil {
106                 return fmt.Errorf("Subscription: Failed to add route. SubId: %d, RmrEndpoint: %s", s.Seq, s.RmrEndpoint)
107         }
108         return nil
109 }