0879a4f813c6a6b6b89fce6445e0b404228feb3e
[ric-plt/submgr.git] / pkg / control / tracker.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/submgr/pkg/xapptweaks"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "sync"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type Tracker struct {
33         mutex                sync.Mutex
34         transactionXappTable map[TransactionXappKey]*TransactionXapp
35         transSeq             uint64
36 }
37
38 func (t *Tracker) Init() {
39         t.transactionXappTable = make(map[TransactionXappKey]*TransactionXapp)
40 }
41
42 func (t *Tracker) initTransaction(transBase *Transaction) {
43         t.mutex.Lock()
44         defer t.mutex.Unlock()
45         transBase.EventChan = make(chan interface{})
46         transBase.tracker = t
47         transBase.Seq = t.transSeq
48         t.transSeq++
49 }
50
51 func (t *Tracker) NewSubsTransaction(subs *Subscription) *TransactionSubs {
52         trans := &TransactionSubs{}
53         trans.Meid = subs.GetMeid()
54         t.initTransaction(&trans.Transaction)
55         xapp.Logger.Debug("CREATE %s", trans.String())
56         return trans
57 }
58
59 func (t *Tracker) NewXappTransaction(
60         endpoint *xapptweaks.RmrEndpoint,
61         xid string,
62         subid uint32,
63         meid *xapp.RMRMeid) *TransactionXapp {
64
65         trans := &TransactionXapp{}
66         trans.XappKey = &TransactionXappKey{*endpoint, xid}
67         trans.Meid = meid
68         trans.SubId = subid
69         t.initTransaction(&trans.Transaction)
70         xapp.Logger.Debug("CREATE %s", trans.String())
71         return trans
72 }
73
74 func (t *Tracker) Track(trans *TransactionXapp) error {
75
76         if trans.GetEndpoint() == nil {
77                 err := fmt.Errorf("Tracker: No valid endpoint given in %s", trans.String())
78                 return err
79         }
80
81         t.mutex.Lock()
82         defer t.mutex.Unlock()
83
84         theKey := *trans.XappKey
85
86         if othtrans, ok := t.transactionXappTable[theKey]; ok {
87                 err := fmt.Errorf("Tracker: %s is ongoing, not tracking %s", othtrans, trans)
88                 return err
89         }
90
91         trans.tracker = t
92         t.transactionXappTable[theKey] = trans
93         xapp.Logger.Debug("Tracker: Append %s", trans.String())
94         //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
95         return nil
96 }
97
98 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*TransactionXapp, error) {
99         t.mutex.Lock()
100         defer t.mutex.Unlock()
101         if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
102                 xapp.Logger.Debug("Tracker: Remove %s", trans.String())
103                 delete(t.transactionXappTable, xappKey)
104                 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
105                 return trans, nil
106         }
107         return nil, fmt.Errorf("Tracker: No record %s", xappKey)
108 }