Fixed function id handling and improved ut fail handling
[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         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
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                     // Lock
34         registry   *Registry                      // Registry
35         Seq        uint16                         // SubsId
36         Meid       *xapp.RMRMeid                  // Meid/ RanName
37         EpList     RmrEndpointList                // Endpoints
38         TransLock  sync.Mutex                     // Lock transactions, only one executed per time for subs
39         TheTrans   *Transaction                   // Ongoing transaction from xapp
40         SubReqMsg  *e2ap.E2APSubscriptionRequest  // Subscription information
41         SubRespMsg *e2ap.E2APSubscriptionResponse // Subscription information
42 }
43
44 func (s *Subscription) String() string {
45         return "subs(" + strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
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) IsTransactionReserved() bool {
64         s.mutex.Lock()
65         defer s.mutex.Unlock()
66         if s.TheTrans != nil {
67                 return true
68         }
69         return false
70
71 }
72
73 func (s *Subscription) GetTransaction() *Transaction {
74         s.mutex.Lock()
75         defer s.mutex.Unlock()
76         return s.TheTrans
77 }
78
79 func (s *Subscription) WaitTransactionTurn(trans *Transaction) {
80         s.TransLock.Lock()
81         s.mutex.Lock()
82         s.TheTrans = trans
83         s.mutex.Unlock()
84 }
85
86 func (s *Subscription) ReleaseTransactionTurn(trans *Transaction) {
87         s.mutex.Lock()
88         if trans != nil && trans == s.TheTrans {
89                 s.TheTrans = nil
90         }
91         s.mutex.Unlock()
92         s.TransLock.Unlock()
93 }