Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 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 { |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 33 | mutex sync.Mutex |
| 34 | registry *Registry |
| 35 | Seq uint16 |
| 36 | Active bool |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 37 | // |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 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() + ")" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func (s *Subscription) String() string { |
| 48 | s.mutex.Lock() |
| 49 | defer s.mutex.Unlock() |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 50 | return s.stringImpl() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 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 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 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 | |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 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 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 92 | func (s *Subscription) SetTransaction(trans *Transaction) error { |
| 93 | s.mutex.Lock() |
| 94 | defer s.mutex.Unlock() |
| 95 | |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 96 | if s.Trans != nil { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 97 | return fmt.Errorf("subs(%s) trans(%s) exist, can not register trans(%s)", s.stringImpl(), s.Trans, trans) |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 98 | } |
| 99 | trans.Subs = s |
| 100 | s.Trans = trans |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 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 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 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 | |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 128 | func (s *Subscription) updateRouteImpl(act Action) error { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 129 | subRouteAction := SubRouteInfo{act, s.EpList, s.Seq} |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 130 | err := s.registry.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 131 | if err != nil { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 132 | return fmt.Errorf("subs(%s) %s", s.stringImpl(), err.Error()) |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 133 | } |
| 134 | return nil |
| 135 | } |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 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() { |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 144 | s.registry.DelSubscription(s.Seq) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 145 | err := s.UpdateRoute(DELETE) |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 146 | if err != nil { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame^] | 147 | xapp.Logger.Error("%s", err.Error()) |
Juha Hyttinen | 56e0383 | 2020-01-14 17:08:43 +0200 | [diff] [blame] | 148 | } |
| 149 | } |