blob: 0879a4f813c6a6b6b89fce6445e0b404228feb3e [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 Hyttinenb31b13f2020-03-18 10:25:30 +020024 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
Juha Hyttinen86a46202020-01-14 12:49:09 +020025 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020026 "sync"
kalnagy93cc3e22019-09-19 11:29:29 +020027)
28
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020029//-----------------------------------------------------------------------------
30//
31//-----------------------------------------------------------------------------
kalnagy93cc3e22019-09-19 11:29:29 +020032type Tracker struct {
Juha Hyttinen379ff082019-12-30 15:49:41 +020033 mutex sync.Mutex
Juha Hyttinen83ada002020-01-30 10:36:33 +020034 transactionXappTable map[TransactionXappKey]*TransactionXapp
Juha Hyttinen422d0182020-01-17 13:37:05 +020035 transSeq uint64
Balint Uvegese9608cd2019-09-20 18:00:32 +000036}
37
38func (t *Tracker) Init() {
Juha Hyttinen83ada002020-01-30 10:36:33 +020039 t.transactionXappTable = make(map[TransactionXappKey]*TransactionXapp)
kalnagy93cc3e22019-09-19 11:29:29 +020040}
41
Juha Hyttinen83ada002020-01-30 10:36:33 +020042func (t *Tracker) initTransaction(transBase *Transaction) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020043 t.mutex.Lock()
44 defer t.mutex.Unlock()
Juha Hyttinen83ada002020-01-30 10:36:33 +020045 transBase.EventChan = make(chan interface{})
46 transBase.tracker = t
47 transBase.Seq = t.transSeq
Juha Hyttinen422d0182020-01-17 13:37:05 +020048 t.transSeq++
Juha Hyttinen83ada002020-01-30 10:36:33 +020049}
50
51func (t *Tracker) NewSubsTransaction(subs *Subscription) *TransactionSubs {
52 trans := &TransactionSubs{}
53 trans.Meid = subs.GetMeid()
Juha Hyttinen83ada002020-01-30 10:36:33 +020054 t.initTransaction(&trans.Transaction)
55 xapp.Logger.Debug("CREATE %s", trans.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +020056 return trans
57}
58
Juha Hyttinen83ada002020-01-30 10:36:33 +020059func (t *Tracker) NewXappTransaction(
Juha Hyttinenb31b13f2020-03-18 10:25:30 +020060 endpoint *xapptweaks.RmrEndpoint,
Juha Hyttinen86a46202020-01-14 12:49:09 +020061 xid string,
Juha Hyttinen47942b42020-02-27 10:41:43 +020062 subid uint32,
Juha Hyttinen83ada002020-01-30 10:36:33 +020063 meid *xapp.RMRMeid) *TransactionXapp {
Juha Hyttinen86a46202020-01-14 12:49:09 +020064
Juha Hyttinen83ada002020-01-30 10:36:33 +020065 trans := &TransactionXapp{}
Juha Hyttinen422d0182020-01-17 13:37:05 +020066 trans.XappKey = &TransactionXappKey{*endpoint, xid}
67 trans.Meid = meid
Juha Hyttinen47942b42020-02-27 10:41:43 +020068 trans.SubId = subid
Juha Hyttinen83ada002020-01-30 10:36:33 +020069 t.initTransaction(&trans.Transaction)
70 xapp.Logger.Debug("CREATE %s", trans.String())
71 return trans
72}
73
74func (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 }
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020080
Juha Hyttinen1a503442019-12-10 12:14:24 +020081 t.mutex.Lock()
82 defer t.mutex.Unlock()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020083
Juha Hyttinen83ada002020-01-30 10:36:33 +020084 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
Juha Hyttinen379ff082019-12-30 15:49:41 +020089 }
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020090
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020091 trans.tracker = t
Juha Hyttinen83ada002020-01-30 10:36:33 +020092 t.transactionXappTable[theKey] = trans
93 xapp.Logger.Debug("Tracker: Append %s", trans.String())
Juha Hyttinen422d0182020-01-17 13:37:05 +020094 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
Juha Hyttinen83ada002020-01-30 10:36:33 +020095 return nil
kalnagy93cc3e22019-09-19 11:29:29 +020096}
97
Juha Hyttinen83ada002020-01-30 10:36:33 +020098func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*TransactionXapp, error) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020099 t.mutex.Lock()
100 defer t.mutex.Unlock()
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200101 if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200102 xapp.Logger.Debug("Tracker: Remove %s", trans.String())
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200103 delete(t.transactionXappTable, xappKey)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200104 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
Juha Hyttinen379ff082019-12-30 15:49:41 +0200105 return trans, nil
kalnagy93cc3e22019-09-19 11:29:29 +0200106 }
Juha Hyttinen31797b42020-01-16 14:05:01 +0200107 return nil, fmt.Errorf("Tracker: No record %s", xappKey)
Anssi Mannila8046c702020-01-02 13:39:05 +0200108}