Metrics for submgr
[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         "io/ioutil"
25         "net/http"
26         "strconv"
27         "strings"
28         "testing"
29         "time"
30
31         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
32         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
33         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
34 )
35
36 //-----------------------------------------------------------------------------
37 //
38 //-----------------------------------------------------------------------------
39 type testingSubmgrControl struct {
40         teststub.RmrControl
41         c *Control
42 }
43
44 type Counter struct {
45         Name  string
46         Value uint64
47 }
48
49 var countersBeforeMap map[string]Counter
50 var toBeAddedCountersMap map[string]Counter
51
52 func createSubmgrControl(srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc) *testingSubmgrControl {
53         mainCtrl = &testingSubmgrControl{}
54         mainCtrl.RmrControl.Init("SUBMGRCTL", srcId, rtgSvc)
55         mainCtrl.c = NewControl()
56         xapp.Logger.Debug("Replacing real db with test db")
57         mainCtrl.c.db = CreateMock() // This overrides real database for testing
58         xapp.SetReadyCB(mainCtrl.ReadyCB, nil)
59         go xapp.RunWithParams(mainCtrl.c, false)
60         mainCtrl.WaitCB()
61         mainCtrl.c.ReadyCB(nil)
62         return mainCtrl
63 }
64
65 func (mc *testingSubmgrControl) SimulateRestart(t *testing.T) {
66         mc.TestLog(t, "Simulating submgr restart")
67         mainCtrl.c.registry.subIds = nil
68         // Initialize subIds slice and subscription map
69         mainCtrl.c.registry.Initialize()
70         // Read subIds and subscriptions from database
71         subIds, register, err := mainCtrl.c.ReadAllSubscriptionsFromSdl()
72         if err != nil {
73                 mc.TestError(t, "%v", err)
74         } else {
75                 mainCtrl.c.registry.register = nil
76                 mainCtrl.c.registry.subIds = subIds
77                 mainCtrl.c.registry.register = register
78
79                 mc.TestLog(t, "register:")
80                 for subId, subs := range register {
81                         mc.TestLog(t, "  subId=%v", subId)
82                         mc.TestLog(t, "  subs.SubRespRcvd=%v", subs.SubRespRcvd)
83                         mc.TestLog(t, "  subs=%v\n", subs)
84                 }
85
86                 mc.TestLog(t, "mainCtrl.c.registry.register:")
87                 for subId, subs := range mainCtrl.c.registry.register {
88                         mc.TestLog(t, "  subId=%v", subId)
89                         mc.TestLog(t, "  subs.SubRespRcvd=%v", subs.SubRespRcvd)
90                         mc.TestLog(t, "  subs=%v\n", subs)
91                 }
92         }
93         go mainCtrl.c.HandleUncompletedSubscriptions(mainCtrl.c.registry.register)
94 }
95
96 func (mc *testingSubmgrControl) SetResetTestFlag(t *testing.T, status bool) {
97         mc.TestLog(t, "ResetTestFlag set to %v=", status)
98         mainCtrl.c.ResetTestFlag = status
99 }
100
101 func (mc *testingSubmgrControl) removeExistingSubscriptions(t *testing.T) {
102
103         mc.TestLog(t, "Removing existing subscriptions")
104         mainCtrl.c.RemoveAllSubscriptionsFromSdl()
105         mainCtrl.c.registry.subIds = nil
106         // Initialize subIds slice and subscription map
107         mainCtrl.c.registry.Initialize()
108 }
109
110 func PringSubscriptionQueryResult(resp models.SubscriptionList) {
111         for _, item := range resp {
112                 fmt.Printf("item.SubscriptionID=%v\n", item.SubscriptionID)
113                 fmt.Printf("item.Meid=%v\n", item.Meid)
114                 fmt.Printf("item.Endpoint=%v\n", item.Endpoint)
115         }
116 }
117
118 func (mc *testingSubmgrControl) wait_registry_empty(t *testing.T, secs int) bool {
119         cnt := int(0)
120         i := 1
121         for ; i <= secs*2; i++ {
122                 cnt = len(mc.c.registry.register)
123                 if cnt == 0 {
124                         return true
125                 }
126                 time.Sleep(500 * time.Millisecond)
127         }
128         mc.TestError(t, "(submgr) no registry empty within %d secs: %d", secs, cnt)
129         return false
130 }
131
132 func (mc *testingSubmgrControl) get_registry_next_subid(t *testing.T) uint32 {
133         mc.c.registry.mutex.Lock()
134         defer mc.c.registry.mutex.Unlock()
135         return mc.c.registry.subIds[0]
136 }
137
138 func (mc *testingSubmgrControl) wait_registry_next_subid_change(t *testing.T, origSubId uint32, secs int) (uint32, bool) {
139         i := 1
140         for ; i <= secs*2; i++ {
141                 mc.c.registry.mutex.Lock()
142                 currSubId := mc.c.registry.subIds[0]
143                 mc.c.registry.mutex.Unlock()
144                 if currSubId != origSubId {
145                         return currSubId, true
146                 }
147                 time.Sleep(500 * time.Millisecond)
148         }
149         mc.TestError(t, "(submgr) no subId change within %d secs", secs)
150         return 0, false
151 }
152
153 func (mc *testingSubmgrControl) wait_subs_clean(t *testing.T, e2SubsId uint32, secs int) bool {
154         var subs *Subscription
155         i := 1
156         for ; i <= secs*2; i++ {
157                 subs = mc.c.registry.GetSubscription(e2SubsId)
158                 if subs == nil {
159                         return true
160                 }
161                 time.Sleep(500 * time.Millisecond)
162         }
163         if subs != nil {
164                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, subs.String())
165         } else {
166                 mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A)", secs)
167         }
168         return false
169 }
170
171 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
172         var trans TransactionIf
173         i := 1
174         for ; i <= secs*2; i++ {
175                 subs := mc.c.registry.GetSubscription(e2SubsId)
176                 if subs == nil {
177                         return true
178                 }
179                 trans = subs.GetTransaction()
180                 if trans == nil {
181                         return true
182                 }
183                 time.Sleep(500 * time.Millisecond)
184         }
185         if trans != nil {
186                 mc.TestError(t, "(submgr) no clean within %d secs: %s", secs, trans.String())
187         } else {
188                 mc.TestError(t, "(submgr) no clean within %d secs: trans(N/A)", secs)
189         }
190         return false
191 }
192
193 func (mc *testingSubmgrControl) get_subs_entrypoint_cnt(t *testing.T, origSubId uint32) int {
194         subs := mc.c.registry.GetSubscription(origSubId)
195         if subs == nil {
196                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt get", origSubId)
197                 return -1
198         }
199         return subs.EpList.Size()
200 }
201
202 func (mc *testingSubmgrControl) wait_subs_entrypoint_cnt_change(t *testing.T, origSubId uint32, orig int, secs int) (int, bool) {
203
204         subs := mc.c.registry.GetSubscription(origSubId)
205         if subs == nil {
206                 mc.TestError(t, "(submgr) no subs %d exists during entrypoint cnt wait", origSubId)
207                 return -1, true
208         }
209
210         i := 1
211         for ; i <= secs*2; i++ {
212                 curr := subs.EpList.Size()
213                 if curr != orig {
214                         return curr, true
215                 }
216                 time.Sleep(500 * time.Millisecond)
217         }
218         mc.TestError(t, "(submgr) no subs %d entrypoint cnt change within %d secs", origSubId, secs)
219         return 0, false
220 }
221
222 //
223 // Counter check for received message. Note might not be yet handled
224 //
225 func (mc *testingSubmgrControl) get_msgcounter(t *testing.T) uint64 {
226         return mc.c.CntRecvMsg
227 }
228
229 func (mc *testingSubmgrControl) wait_msgcounter_change(t *testing.T, orig uint64, secs int) (uint64, bool) {
230         i := 1
231         for ; i <= secs*2; i++ {
232                 curr := mc.c.CntRecvMsg
233                 if curr != orig {
234                         return curr, true
235                 }
236                 time.Sleep(500 * time.Millisecond)
237         }
238         mc.TestError(t, "(submgr) no msg counter change within %d secs", secs)
239         return 0, false
240 }
241
242 func (mc *testingSubmgrControl) GetMetrics(t *testing.T) (string, error) {
243         req, err := http.NewRequest("GET", "http://localhost:8080/ric/v1/metrics", nil)
244         if err != nil {
245                 return "", fmt.Errorf("Error reading request. %v", err)
246         }
247         client := &http.Client{Timeout: time.Second * 10}
248         resp, err := client.Do(req)
249         if err != nil {
250                 return "", fmt.Errorf("Error reading response. %v", err)
251         }
252         defer resp.Body.Close()
253
254         respBody, err := ioutil.ReadAll(resp.Body)
255         if err != nil {
256                 return "", fmt.Errorf("Error reading body. %v", err)
257         }
258         return string(respBody[:]), nil
259 }
260
261 func (mc *testingSubmgrControl) SetTimesCounterWillBeAdded(counterName string, addedValue uint64) {
262         if len(toBeAddedCountersMap) == 0 {
263                 toBeAddedCountersMap = make(map[string]Counter)
264         }
265         counter := Counter{}
266         counter.Name = counterName
267         counter.Value = addedValue
268         toBeAddedCountersMap[counterName] = counter
269 }
270
271 func (mc *testingSubmgrControl) GetCounterValuesBefore(t *testing.T) {
272         countersBeforeMap = make(map[string]Counter)
273         countersBeforeMap = mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
274 }
275
276 func (mc *testingSubmgrControl) VerifyCounterValues(t *testing.T) {
277         currentCountersMap := mc.GetCurrentCounterValues(t, toBeAddedCountersMap)
278         for _, toBeAddedCounter := range toBeAddedCountersMap {
279                 if currentCounter, ok := currentCountersMap[toBeAddedCounter.Name]; ok == true {
280                         if beforeCounter, ok := countersBeforeMap[toBeAddedCounter.Name]; ok == true {
281                                 if currentCounter.Value != beforeCounter.Value+toBeAddedCounter.Value {
282                                         mc.TestError(t, "Error in expected counter value: counterName %v, current value %v, expected value %v",
283                                                 currentCounter.Name, currentCounter.Value, beforeCounter.Value+toBeAddedCounter.Value)
284
285                                         //fmt.Printf("beforeCounter.Value=%v, toBeAddedCounter.Value=%v, \n",beforeCounter.Value, toBeAddedCounter.Value)
286                                 }
287                         } else {
288                                 mc.TestError(t, "Counter %v not in countersBeforeMap", toBeAddedCounter.Name)
289                         }
290                 } else {
291                         mc.TestError(t, "Counter %v not in currentCountersMap", toBeAddedCounter.Name)
292                 }
293         }
294
295         // Make map empty
296         //fmt.Printf("toBeAddedCountersMap=%v\n",toBeAddedCountersMap)
297         toBeAddedCountersMap = make(map[string]Counter)
298 }
299
300 func (mc *testingSubmgrControl) GetCurrentCounterValues(t *testing.T, chekedCountersMap map[string]Counter) map[string]Counter {
301         countersString, err := mc.GetMetrics(t)
302         if err != nil {
303                 mc.TestError(t, "Error GetMetrics() failed %v", err)
304                 return nil
305         }
306
307         retCounterMap := make(map[string]Counter)
308         stringsTable := strings.Split(countersString, "\n")
309         for _, counter := range chekedCountersMap {
310                 for _, counterString := range stringsTable {
311                         if !strings.Contains(counterString, "#") && strings.Contains(counterString, counter.Name) {
312                                 counterString := strings.Split(counterString, " ")
313                                 if strings.Contains(counterString[0], counter.Name) {
314                                         val, err := strconv.ParseUint(counterString[1], 10, 64)
315                                         if err != nil {
316                                                 mc.TestError(t, "Error: strconv.ParseUint failed %v", err)
317                                         }
318                                         counter.Value = val
319                                         //fmt.Printf("counter=%v\n", counter)
320                                         retCounterMap[counter.Name] = counter
321                                 }
322                         }
323                 }
324         }
325
326         if len(retCounterMap) != len(chekedCountersMap) {
327                 mc.TestError(t, "Error: len(retCounterMap) != len(chekedCountersMap)")
328
329         }
330         return retCounterMap
331 }