New submgr for testing. Tagged as ric-plt-submgr:r3-test-v2. Tested that submgr can...
[ric-plt/submgr.git] / pkg / control / timers.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         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
24         "sync"
25         "time"
26 )
27
28 var timerMutex = &sync.Mutex{}
29
30 type TimerInfo struct {
31         timerAddress *time.Timer        
32         timerFunctionAddress func()
33 }
34
35 type TimerMap struct {
36         timer map[uint16] TimerInfo
37 }
38
39 // This method should run as a constructor
40 func (t *TimerMap) Init() {
41         t.timer = make(map[uint16] TimerInfo)
42 }
43
44 func (t *TimerMap) StartTimer(subId uint16, expireAfterTime time.Duration, timerFunction func(subId uint16)) bool {
45         timerMutex.Lock()
46         defer timerMutex.Unlock()
47         if (timerFunction == nil) {
48                 xapp.Logger.Error("StartTimer() timerFunc == nil")
49                 return false
50         }
51
52         // Stop timer if there is already timer running with the same id
53         if val, ok := t.timer[subId]; ok {
54                 xapp.Logger.Error("StartTimer() old timer found")
55                 if val.timerAddress != nil {
56                         xapp.Logger.Error("StartTimer() deleting old timer")
57                         val.timerAddress.Stop()
58                 }
59                 delete(t.timer, subId)
60         }
61
62         // Store timer + timer function excecutor function and the function to be excecuted when timer expires, in map
63         t.timer[subId] = TimerInfo{timerAddress: time.AfterFunc(expireAfterTime, func(){t.timerFunctionExcecutor(subId)}),
64                                                            timerFunctionAddress: func(){timerFunction(subId)}}
65         return true
66 }
67
68 func (t *TimerMap) StopTimer(subId uint16) bool {
69         timerMutex.Lock()
70         defer timerMutex.Unlock()
71         if val, ok := t.timer[subId]; ok {
72                 if val.timerAddress != nil {
73                         val.timerAddress.Stop()
74                         delete(t.timer, subId)
75                         return true
76                 } else {
77                         xapp.Logger.Error("StopTimer() timerAddress == nil")
78                         return false
79                 }
80         } else {
81                 xapp.Logger.Info("StopTimer() Timer not found. May be expired or stopped already. subId: %v",subId)
82                 return false
83         }
84 }
85
86 func (t *TimerMap) timerFunctionExcecutor(subId uint16) {
87         timerMutex.Lock()
88         if val, ok := t.timer[subId]; ok {
89                 if val.timerFunctionAddress != nil {
90                         // Take local copy of timer function address
91                         f := val.timerFunctionAddress
92                         // Delete timer instance from map
93                         delete(t.timer, subId)
94                         timerMutex.Unlock()
95                         // Excecute the timer function
96                         f()
97                         return
98                 } else {
99                         xapp.Logger.Error("timerExcecutorFunc() timerFunctionAddress == nil")
100                         timerMutex.Unlock()
101                         return
102                 }
103         } else {
104                 xapp.Logger.Error("timerExcecutorFunc() Timer not anymore in map. subId: %v",subId)
105                 timerMutex.Unlock()
106                 return
107         }
108 }