kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 25 | "sync" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 28 | //----------------------------------------------------------------------------- |
| 29 | // |
| 30 | //----------------------------------------------------------------------------- |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 31 | type Tracker struct { |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 32 | mutex sync.Mutex |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 33 | transactionXappTable map[TransactionXappKey]*Transaction |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 34 | transSeq uint64 |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func (t *Tracker) Init() { |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 38 | t.transactionXappTable = make(map[TransactionXappKey]*Transaction) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 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 | |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 63 | func (t *Tracker) TrackTransaction( |
| 64 | endpoint *RmrEndpoint, |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 65 | xid string, |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 66 | meid *xapp.RMRMeid) (*Transaction, error) { |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 67 | |
| 68 | if endpoint == nil { |
| 69 | err := fmt.Errorf("Tracker: No valid endpoint given") |
| 70 | return nil, err |
| 71 | } |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 72 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 73 | trans := &Transaction{} |
| 74 | trans.XappKey = &TransactionXappKey{*endpoint, xid} |
| 75 | trans.Meid = meid |
| 76 | trans = t.NewTransactionFromSkel(trans) |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 77 | |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 78 | t.mutex.Lock() |
| 79 | defer t.mutex.Unlock() |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 80 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 81 | if othtrans, ok := t.transactionXappTable[*trans.XappKey]; ok { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 82 | err := fmt.Errorf("Tracker: %s is ongoing, %s not created ", othtrans, trans) |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 83 | return nil, err |
| 84 | } |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 85 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 86 | trans.tracker = t |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 87 | t.transactionXappTable[*trans.XappKey] = trans |
| 88 | xapp.Logger.Debug("Tracker: Add %s", trans.String()) |
| 89 | //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable) |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 90 | return trans, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 93 | func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 94 | t.mutex.Lock() |
| 95 | defer t.mutex.Unlock() |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 96 | if trans, ok2 := t.transactionXappTable[xappKey]; ok2 { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 97 | xapp.Logger.Debug("Tracker: Delete %s", trans.String()) |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 98 | delete(t.transactionXappTable, xappKey) |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame^] | 99 | //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable) |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 100 | return trans, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 101 | } |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 102 | return nil, fmt.Errorf("Tracker: No record %s", xappKey) |
Anssi Mannila | 8046c70 | 2020-01-02 13:39:05 +0200 | [diff] [blame] | 103 | } |