blob: 6fd806c610ce7c3761a9cd8d593c4e25e800a6fe [file] [log] [blame]
Juha Hyttinen0388dd92020-01-09 14:14:16 +02001/*
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
20package control
21
22import (
23 "fmt"
24 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25 "strconv"
26 "sync"
27)
28
29//-----------------------------------------------------------------------------
30//
31//-----------------------------------------------------------------------------
32type Subscription struct {
Juha Hyttinen56e03832020-01-14 17:08:43 +020033 mutex sync.Mutex
34 registry *Registry
35 Seq uint16
36 Active bool
Juha Hyttinen0388dd92020-01-09 14:14:16 +020037 //
Juha Hyttinen31797b42020-01-16 14:05:01 +020038 Meid *xapp.RMRMeid
39 EpList RmrEndpointList
40 Trans *Transaction
41}
42
43func (s *Subscription) stringImpl() string {
44 return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
Juha Hyttinen0388dd92020-01-09 14:14:16 +020045}
46
47func (s *Subscription) String() string {
48 s.mutex.Lock()
49 defer s.mutex.Unlock()
Juha Hyttinen31797b42020-01-16 14:05:01 +020050 return s.stringImpl()
Juha Hyttinen0388dd92020-01-09 14:14:16 +020051}
52
Juha Hyttinene406a342020-01-13 13:02:26 +020053func (s *Subscription) GetSubId() uint16 {
54 s.mutex.Lock()
55 defer s.mutex.Unlock()
56 return s.Seq
57}
58
59func (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
Juha Hyttinen0388dd92020-01-09 14:14:16 +020068func (s *Subscription) Confirmed() {
69 s.mutex.Lock()
70 defer s.mutex.Unlock()
71 s.Active = true
72}
73
74func (s *Subscription) UnConfirmed() {
75 s.mutex.Lock()
76 defer s.mutex.Unlock()
77 s.Active = false
78}
79
80func (s *Subscription) IsConfirmed() bool {
81 s.mutex.Lock()
82 defer s.mutex.Unlock()
83 return s.Active
84}
85
Juha Hyttinen31797b42020-01-16 14:05:01 +020086func (s *Subscription) IsEndpoint(ep *RmrEndpoint) bool {
87 s.mutex.Lock()
88 defer s.mutex.Unlock()
89 return s.EpList.HasEndpoint(ep)
90}
91
Juha Hyttinen0388dd92020-01-09 14:14:16 +020092func (s *Subscription) SetTransaction(trans *Transaction) error {
93 s.mutex.Lock()
94 defer s.mutex.Unlock()
95
Juha Hyttinen0388dd92020-01-09 14:14:16 +020096 if s.Trans != nil {
Juha Hyttinen31797b42020-01-16 14:05:01 +020097 return fmt.Errorf("subs(%s) trans(%s) exist, can not register trans(%s)", s.stringImpl(), s.Trans, trans)
Juha Hyttinen0388dd92020-01-09 14:14:16 +020098 }
99 trans.Subs = s
100 s.Trans = trans
Juha Hyttinen31797b42020-01-16 14:05:01 +0200101
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 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200109 return nil
110}
111
112func (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
122func (s *Subscription) GetTransaction() *Transaction {
123 s.mutex.Lock()
124 defer s.mutex.Unlock()
125 return s.Trans
126}
127
Juha Hyttinen56e03832020-01-14 17:08:43 +0200128func (s *Subscription) updateRouteImpl(act Action) error {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200129 subRouteAction := SubRouteInfo{act, s.EpList, s.Seq}
Juha Hyttinen56e03832020-01-14 17:08:43 +0200130 err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200131 if err != nil {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200132 return fmt.Errorf("subs(%s) %s", s.stringImpl(), err.Error())
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200133 }
134 return nil
135}
Juha Hyttinen56e03832020-01-14 17:08:43 +0200136
137func (s *Subscription) UpdateRoute(act Action) error {
138 s.mutex.Lock()
139 defer s.mutex.Unlock()
140 return s.updateRouteImpl(act)
141}
142
143func (s *Subscription) Release() {
Juha Hyttinen56e03832020-01-14 17:08:43 +0200144 s.registry.DelSubscription(s.Seq)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200145 err := s.UpdateRoute(DELETE)
Juha Hyttinen56e03832020-01-14 17:08:43 +0200146 if err != nil {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200147 xapp.Logger.Error("%s", err.Error())
Juha Hyttinen56e03832020-01-14 17:08:43 +0200148 }
149}