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 | b31b13f | 2020-03-18 10:25:30 +0200 | [diff] [blame^] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 25 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 26 | "sync" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 29 | //----------------------------------------------------------------------------- |
| 30 | // |
| 31 | //----------------------------------------------------------------------------- |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 32 | type Tracker struct { |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 33 | mutex sync.Mutex |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 34 | transactionXappTable map[TransactionXappKey]*TransactionXapp |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 35 | transSeq uint64 |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (t *Tracker) Init() { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 39 | t.transactionXappTable = make(map[TransactionXappKey]*TransactionXapp) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 42 | func (t *Tracker) initTransaction(transBase *Transaction) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 43 | t.mutex.Lock() |
| 44 | defer t.mutex.Unlock() |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 45 | transBase.EventChan = make(chan interface{}) |
| 46 | transBase.tracker = t |
| 47 | transBase.Seq = t.transSeq |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 48 | t.transSeq++ |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func (t *Tracker) NewSubsTransaction(subs *Subscription) *TransactionSubs { |
| 52 | trans := &TransactionSubs{} |
| 53 | trans.Meid = subs.GetMeid() |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 54 | t.initTransaction(&trans.Transaction) |
| 55 | xapp.Logger.Debug("CREATE %s", trans.String()) |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 56 | return trans |
| 57 | } |
| 58 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 59 | func (t *Tracker) NewXappTransaction( |
Juha Hyttinen | b31b13f | 2020-03-18 10:25:30 +0200 | [diff] [blame^] | 60 | endpoint *xapptweaks.RmrEndpoint, |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 61 | xid string, |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 62 | subid uint32, |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 63 | meid *xapp.RMRMeid) *TransactionXapp { |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 64 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 65 | trans := &TransactionXapp{} |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 66 | trans.XappKey = &TransactionXappKey{*endpoint, xid} |
| 67 | trans.Meid = meid |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 68 | trans.SubId = subid |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 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 | } |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 80 | |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 81 | t.mutex.Lock() |
| 82 | defer t.mutex.Unlock() |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 83 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 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 |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 89 | } |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 90 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 91 | trans.tracker = t |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 92 | t.transactionXappTable[theKey] = trans |
| 93 | xapp.Logger.Debug("Tracker: Append %s", trans.String()) |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 94 | //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable) |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 95 | return nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 98 | func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*TransactionXapp, error) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 99 | t.mutex.Lock() |
| 100 | defer t.mutex.Unlock() |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 101 | if trans, ok2 := t.transactionXappTable[xappKey]; ok2 { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 102 | xapp.Logger.Debug("Tracker: Remove %s", trans.String()) |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 103 | delete(t.transactionXappTable, xappKey) |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 104 | //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable) |
Juha Hyttinen | 379ff08 | 2019-12-30 15:49:41 +0200 | [diff] [blame] | 105 | return trans, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 106 | } |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 107 | return nil, fmt.Errorf("Tracker: No record %s", xappKey) |
Anssi Mannila | 8046c70 | 2020-01-02 13:39:05 +0200 | [diff] [blame] | 108 | } |