RICPLT-3014 Subscription multiple endpoints
[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/xapp-frame/pkg/xapp"
25         "sync"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type Tracker struct {
32         mutex                sync.Mutex
33         transactionXappTable map[TransactionXappKey]*Transaction
34         transSeq             uint64
35 }
36
37 func (t *Tracker) Init() {
38         t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
39 }
40
41 func (t *Tracker) NewTransactionFromSkel(transSkel *Transaction) *Transaction {
42         t.mutex.Lock()
43         defer t.mutex.Unlock()
44         trans := transSkel
45         if trans == nil {
46                 trans = &Transaction{}
47         }
48         trans.EventChan = make(chan interface{})
49         trans.tracker = t
50         trans.Seq = t.transSeq
51         t.transSeq++
52         xapp.Logger.Debug("Transaction: Create %s", trans.String())
53         return trans
54 }
55
56 func (t *Tracker) NewTransaction(meid *xapp.RMRMeid) *Transaction {
57         trans := &Transaction{}
58         trans.Meid = meid
59         trans = t.NewTransactionFromSkel(trans)
60         return trans
61 }
62
63 func (t *Tracker) TrackTransaction(
64         endpoint *RmrEndpoint,
65         xid string,
66         meid *xapp.RMRMeid) (*Transaction, error) {
67
68         if endpoint == nil {
69                 err := fmt.Errorf("Tracker: No valid endpoint given")
70                 return nil, err
71         }
72
73         trans := &Transaction{}
74         trans.XappKey = &TransactionXappKey{*endpoint, xid}
75         trans.Meid = meid
76         trans = t.NewTransactionFromSkel(trans)
77
78         t.mutex.Lock()
79         defer t.mutex.Unlock()
80
81         if othtrans, ok := t.transactionXappTable[*trans.XappKey]; ok {
82                 err := fmt.Errorf("Tracker: %s is ongoing, %s not created ", othtrans, trans)
83                 return nil, err
84         }
85
86         trans.tracker = t
87         t.transactionXappTable[*trans.XappKey] = trans
88         xapp.Logger.Debug("Tracker: Add %s", trans.String())
89         //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
90         return trans, nil
91 }
92
93 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
94         t.mutex.Lock()
95         defer t.mutex.Unlock()
96         if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
97                 xapp.Logger.Debug("Tracker: Delete %s", trans.String())
98                 delete(t.transactionXappTable, xappKey)
99                 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
100                 return trans, nil
101         }
102         return nil, fmt.Errorf("Tracker: No record %s", xappKey)
103 }