Submgr restart improvement
[ric-plt/submgr.git] / pkg / control / ut_ctrl_submgr_test.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         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27         "testing"
28         "time"
29 )
30
31 //-----------------------------------------------------------------------------
32 //
33 //-----------------------------------------------------------------------------
34 type testingSubmgrControl struct {
35         teststub.RmrControl
36         c *Control
37 }
38
39 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
40         mainCtrl = &testingSubmgrControl{}
41         mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
42         mainCtrl.c = NewControl()
43         xapp.Logger.Debug("Replacing real db with test db")
44         mainCtrl.c.db = CreateMock() // This overrides real database for testing
45         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
46         go xapp.RunWithParams(mainCtrl.c, false)
47         mainCtrl.WaitCB()
48         mainCtrl.c.ReadyCB(nil)
49         return mainCtrl
50 }
51
52 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
53         mc.TestLog(t, "Simulating submgr restart")
54         mainCtrl.c.registry.subIds = nil
55         // Initialize subIds slice and subscription map
56         mainCtrl.c.registry.Initialize()
57         // Read subIds and subscriptions from database
58         subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
59         if err != nil {
60                 mc.TestError(t, "%v", err)
61         } else {
62                 mainCtrl.c.registry.register = nil
63                 mainCtrl.c.registry.subIds = subIds
64                 mainCtrl.c.registry.register = register
65
66                 fmt.Println("register:")
67                 for subId, subs := range register {
68                         fmt.Println("  subId", subId)
69                         fmt.Println("  subs.SubRespRcvd", subs.SubRespRcvd)
70                         fmt.Printf("  subs %v\n", subs)
71                 }
72
73                 fmt.Println("mainCtrl.c.registry.register:")
74                 for subId, subs := range mainCtrl.c.registry.register {
75                         fmt.Println("  subId", subId)
76                         fmt.Println("  subs.SubRespRcvd", subs.SubRespRcvd)
77                         fmt.Printf("  subs %v\n", subs)
78                 }
79         }
80         go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
81 }
82
83 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
84         mc.TestLog(t, "ResetTestFlag set to %v", status)
85         mainCtrl.c.ResetTestFlag = status
86 }
87
88 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
89
90         mc.TestLog(t, "Removing existing subscriptions")
91         mainCtrl.c.RemoveAllSubscriptionsFromSdl()
92         mainCtrl.c.registry.subIds = nil
93         // Initialize subIds slice and subscription map
94         mainCtrl.c.registry.Initialize()
95 }
96
97 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
98         for _, item := range resp {
99                 fmt.Printf("item.SubscriptionID %v\n", item.SubscriptionID)
100                 fmt.Printf("item.Meid %v\n", item.Meid)
101                 fmt.Printf("item.Endpoint %v\n", item.Endpoint)
102         }
103 }
104
105 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
106         cnt := int(0)
107         i := 1
108         for ; i <= secs*2; i++ {
109                 cnt = len(mc.c.registry.register)
110                 if cnt == 0 {
111                         return true
112                 }
113                 time.Sleep(500 * time.Millisecond)
114         }
115         mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
116         return false
117 }
118
119 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
120         mc.c.registry.mutex.Lock()
121         defer mc.c.registry.mutex.Unlock()
122         return mc.c.registry.subIds[0]
123 }
124
125 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
126         i := 1
127         for ; i <= secs*2; i++ {
128                 mc.c.registry.mutex.Lock()
129                 currSubId := mc.c.registry.subIds[0]
130                 mc.c.registry.mutex.Unlock()
131                 if currSubId != origSubId {
132                         return currSubId, true
133                 }
134                 time.Sleep(500 * time.Millisecond)
135         }
136         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
137         return 0, false
138 }
139
140 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
141         var subs *Subscription
142         i := 1
143         for ; i <= secs*2; i++ {
144                 subs = mc.c.registry.GetSubscription(e2SubsId)
145                 if subs == nil {
146                         return true
147                 }
148                 time.Sleep(500 * time.Millisecond)
149         }
150         if subs != nil {
151                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
152         } else {
153                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
154         }
155         return false
156 }
157
158 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
159         var trans TransactionIf
160         i := 1
161         for ; i <= secs*2; i++ {
162                 subs := mc.c.registry.GetSubscription(e2SubsId)
163                 if subs == nil {
164                         return true
165                 }
166                 trans = subs.GetTransaction()
167                 if trans == nil {
168                         return true
169                 }
170                 time.Sleep(500 * time.Millisecond)
171         }
172         if trans != nil {
173                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
174         } else {
175                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
176         }
177         return false
178 }
179
180 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
181         subs := mc.c.registry.GetSubscription(origSubId)
182         if subs == nil {
183                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
184                 return -1
185         }
186         return subs.EpList.Size()
187 }
188
189 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
190
191         subs := mc.c.registry.GetSubscription(origSubId)
192         if subs == nil {
193                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
194                 return -1, true
195         }
196
197         i := 1
198         for ; i <= secs*2; i++ {
199                 curr := subs.EpList.Size()
200                 if curr != orig {
201                         return curr, true
202                 }
203                 time.Sleep(500 * time.Millisecond)
204         }
205         mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
206         return 0, false
207 }
208
209 //
210 // Counter check for received message. Note might not be yet handled
211 //
212 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
213         return mc.c.CntRecvMsg
214 }
215
216 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
217         i := 1
218         for ; i <= secs*2; i++ {
219                 curr := mc.c.CntRecvMsg
220                 if curr != orig {
221                         return curr, true
222                 }
223                 time.Sleep(500 * time.Millisecond)
224         }
225         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
226         return 0, false
227 }