blob: c16a76a33fca9d995ff6b9583df4030446a098f5 [file] [log] [blame]
kalnagy93cc3e22019-09-19 11:29:29 +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"
Juha Hyttinen86a46202020-01-14 12:49:09 +020024 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020025 "sync"
kalnagy93cc3e22019-09-19 11:29:29 +020026)
27
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020028//-----------------------------------------------------------------------------
29//
30//-----------------------------------------------------------------------------
kalnagy93cc3e22019-09-19 11:29:29 +020031type Tracker struct {
Juha Hyttinen379ff082019-12-30 15:49:41 +020032 mutex sync.Mutex
Juha Hyttinen0388dd92020-01-09 14:14:16 +020033 transactionXappTable map[TransactionXappKey]*Transaction
Juha Hyttinen422d0182020-01-17 13:37:05 +020034 transSeq uint64
Balint Uvegese9608cd2019-09-20 18:00:32 +000035}
36
37func (t *Tracker) Init() {
Juha Hyttinen379ff082019-12-30 15:49:41 +020038 t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
kalnagy93cc3e22019-09-19 11:29:29 +020039}
40
Juha Hyttinen422d0182020-01-17 13:37:05 +020041func (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
56func (t *Tracker) NewTransaction(meid *xapp.RMRMeid) *Transaction {
57 trans := &Transaction{}
58 trans.Meid = meid
59 trans = t.NewTransactionFromSkel(trans)
60 return trans
61}
62
Juha Hyttinen86a46202020-01-14 12:49:09 +020063func (t *Tracker) TrackTransaction(
64 endpoint *RmrEndpoint,
Juha Hyttinen86a46202020-01-14 12:49:09 +020065 xid string,
Juha Hyttinen422d0182020-01-17 13:37:05 +020066 meid *xapp.RMRMeid) (*Transaction, error) {
Juha Hyttinen86a46202020-01-14 12:49:09 +020067
68 if endpoint == nil {
69 err := fmt.Errorf("Tracker: No valid endpoint given")
70 return nil, err
71 }
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020072
Juha Hyttinen422d0182020-01-17 13:37:05 +020073 trans := &Transaction{}
74 trans.XappKey = &TransactionXappKey{*endpoint, xid}
75 trans.Meid = meid
76 trans = t.NewTransactionFromSkel(trans)
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020077
Juha Hyttinen1a503442019-12-10 12:14:24 +020078 t.mutex.Lock()
79 defer t.mutex.Unlock()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020080
Juha Hyttinen422d0182020-01-17 13:37:05 +020081 if othtrans, ok := t.transactionXappTable[*trans.XappKey]; ok {
Juha Hyttinen31797b42020-01-16 14:05:01 +020082 err := fmt.Errorf("Tracker: %s is ongoing, %s not created ", othtrans, trans)
Juha Hyttinen379ff082019-12-30 15:49:41 +020083 return nil, err
84 }
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020085
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020086 trans.tracker = t
Juha Hyttinen422d0182020-01-17 13:37:05 +020087 t.transactionXappTable[*trans.XappKey] = trans
88 xapp.Logger.Debug("Tracker: Add %s", trans.String())
89 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
Juha Hyttinen379ff082019-12-30 15:49:41 +020090 return trans, nil
kalnagy93cc3e22019-09-19 11:29:29 +020091}
92
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020093func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020094 t.mutex.Lock()
95 defer t.mutex.Unlock()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020096 if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
Juha Hyttinen422d0182020-01-17 13:37:05 +020097 xapp.Logger.Debug("Tracker: Delete %s", trans.String())
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020098 delete(t.transactionXappTable, xappKey)
Juha Hyttinen422d0182020-01-17 13:37:05 +020099 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
Juha Hyttinen379ff082019-12-30 15:49:41 +0200100 return trans, nil
kalnagy93cc3e22019-09-19 11:29:29 +0200101 }
Juha Hyttinen31797b42020-01-16 14:05:01 +0200102 return nil, fmt.Errorf("Tracker: No record %s", xappKey)
Anssi Mannila8046c702020-01-02 13:39:05 +0200103}