Xapp-frame, v0.8.1 Rest Subscription Creation /Query /Deletion
[ric-plt/submgr.git] / pkg / teststube2ap / stubE2.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 teststube2ap
21
22 import (
23         "fmt"
24         "strconv"
25         "testing"
26         "time"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
29         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
30         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
31         clientmodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
32         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 )
34
35 //-----------------------------------------------------------------------------
36 //
37 //-----------------------------------------------------------------------------
38 var e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
39
40 //-----------------------------------------------------------------------------
41 //
42 //-----------------------------------------------------------------------------
43 type RmrTransactionId struct {
44         xid  string
45         meid *xapp.RMRMeid
46 }
47
48 type E2RestIds struct {
49         RestSubsId string
50         E2SubsId   uint32
51 }
52
53 func (trans *RmrTransactionId) String() string {
54         meidstr := "N/A"
55         if trans.meid != nil {
56                 meidstr = trans.meid.String()
57         }
58         return "trans(" + trans.xid + "/" + meidstr + ")"
59 }
60
61 type E2Stub struct {
62         teststub.RmrStubControl
63         xid_seq                     uint64
64         subscriptionId              string
65         requestCount                int
66         CallBackNotification        chan int64
67         RESTNotification            chan uint32
68         CallBackListedNotifications chan E2RestIds
69         ListedRESTNotifications     chan E2RestIds
70         clientEndpoint              clientmodel.SubscriptionParamsClientEndpoint
71         meid                        string
72         restSubsIdList              []string
73 }
74
75 //-----------------------------------------------------------------------------
76 //
77 //-----------------------------------------------------------------------------
78 func CreateNewE2Stub(desc string, srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc, stat string, mtypeseed int, ranName string, host string, RMRPort int64, HTTPPort int64) *E2Stub {
79         tc := &E2Stub{}
80         tc.RmrStubControl.Init(desc, srcId, rtgSvc, stat, mtypeseed)
81         tc.xid_seq = 1
82         tc.SetCheckXid(true)
83         tc.CallBackNotification = make(chan int64)
84         tc.RESTNotification = make(chan uint32)
85         tc.CallBackListedNotifications = make(chan E2RestIds)
86         tc.ListedRESTNotifications = make(chan E2RestIds, 2)
87         var endPoint clientmodel.SubscriptionParamsClientEndpoint
88         endPoint.Host = host
89         endPoint.HTTPPort = &HTTPPort
90         endPoint.RMRPort = &RMRPort
91         tc.clientEndpoint = endPoint
92         tc.meid = ranName
93         return tc
94 }
95
96 //-----------------------------------------------------------------------------
97 //
98 //-----------------------------------------------------------------------------
99 func CreateNewE2termStub(desc string, srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc, stat string, mtypeseed int) *E2Stub {
100         tc := &E2Stub{}
101         tc.RmrStubControl.Init(desc, srcId, rtgSvc, stat, mtypeseed)
102         tc.xid_seq = 1
103         tc.SetCheckXid(false)
104         return tc
105 }
106
107 //-----------------------------------------------------------------------------
108 //
109 //-----------------------------------------------------------------------------
110 func (tc *E2Stub) NewRmrTransactionId(xid string, ranname string) *RmrTransactionId {
111         trans := &RmrTransactionId{}
112         if len(xid) == 0 {
113                 trans.xid = tc.GetDesc() + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
114                 tc.xid_seq++
115         } else {
116                 trans.xid = xid
117         }
118         trans.meid = &xapp.RMRMeid{RanName: ranname}
119         tc.Info("New test %s", trans.String())
120         return trans
121 }
122
123 //-----------------------------------------------------------------------------
124 //
125 //-----------------------------------------------------------------------------
126 type E2StubSubsReqParams struct {
127         Req *e2ap.E2APSubscriptionRequest
128 }
129
130 func (p *E2StubSubsReqParams) Init() {
131         p.Req = &e2ap.E2APSubscriptionRequest{}
132
133         p.Req.RequestId.Id = 1
134         p.Req.RequestId.InstanceId = 0
135         p.Req.FunctionId = 1
136
137         // gnb -> enb outgoing
138         // enb -> gnb incoming
139         // X2 36423-f40.doc
140         p.Req.EventTriggerDefinition.Data.Length = 1
141         p.Req.EventTriggerDefinition.Data.Data = make([]uint8, p.Req.EventTriggerDefinition.Data.Length)
142         p.Req.EventTriggerDefinition.Data.Data[0] = 1
143
144         p.Req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
145
146         p.Req.ActionSetups[0].ActionId = 0
147         p.Req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
148         p.Req.ActionSetups[0].RicActionDefinitionPresent = true
149
150         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Length = 1
151         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Data = make([]uint8, p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Length)
152         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Data[0] = 1
153
154         p.Req.ActionSetups[0].SubsequentAction.Present = true
155         p.Req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
156         p.Req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
157 }
158
159 //-----------------------------------------------------------------------------
160 //
161 //-----------------------------------------------------------------------------
162
163 type E2StubSubsFailParams struct {
164         Req  *e2ap.E2APSubscriptionRequest
165         Fail *e2ap.E2APSubscriptionFailure
166 }
167
168 func (p *E2StubSubsFailParams) Set(req *e2ap.E2APSubscriptionRequest) {
169         p.Req = req
170
171         p.Fail = &e2ap.E2APSubscriptionFailure{}
172         p.Fail.RequestId.Id = p.Req.RequestId.Id
173         p.Fail.RequestId.InstanceId = p.Req.RequestId.InstanceId
174         p.Fail.FunctionId = p.Req.FunctionId
175         p.Fail.ActionNotAdmittedList.Items = make([]e2ap.ActionNotAdmittedItem, len(p.Req.ActionSetups))
176         for index := int(0); index < len(p.Fail.ActionNotAdmittedList.Items); index++ {
177                 p.Fail.ActionNotAdmittedList.Items[index].ActionId = p.Req.ActionSetups[index].ActionId
178                 p.SetCauseVal(index, 5, 1)
179         }
180 }
181
182 func (p *E2StubSubsFailParams) SetCauseVal(ind int, content uint8, causeval uint8) {
183
184         if ind < 0 {
185                 for index := int(0); index < len(p.Fail.ActionNotAdmittedList.Items); index++ {
186                         p.Fail.ActionNotAdmittedList.Items[index].Cause.Content = content
187                         p.Fail.ActionNotAdmittedList.Items[index].Cause.Value = causeval
188                 }
189                 return
190         }
191         p.Fail.ActionNotAdmittedList.Items[ind].Cause.Content = content
192         p.Fail.ActionNotAdmittedList.Items[ind].Cause.Value = causeval
193 }
194
195 //-----------------------------------------------------------------------------
196 //
197 //-----------------------------------------------------------------------------
198
199 func (tc *E2Stub) SendSubsReq(t *testing.T, rparams *E2StubSubsReqParams, oldTrans *RmrTransactionId) *RmrTransactionId {
200
201         trans := oldTrans
202         if oldTrans == nil {
203                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
204         }
205
206         tc.Info("SendSubsReq %s", trans.String())
207         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
208
209         //---------------------------------
210         // xapp activity: Send Subs Req
211         //---------------------------------
212         myparams := rparams
213
214         if myparams == nil {
215                 myparams = &E2StubSubsReqParams{}
216                 myparams.Init()
217         }
218
219         err, packedMsg := e2SubsReq.Pack(myparams.Req)
220         if err != nil {
221                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
222                 return nil
223         }
224         tc.Debug("%s %s", trans.String(), e2SubsReq.String())
225
226         params := &xapp.RMRParams{}
227         params.Mtype = xapp.RIC_SUB_REQ
228         params.SubId = -1
229         params.Payload = packedMsg.Buf
230         params.PayloadLen = len(packedMsg.Buf)
231         params.Meid = trans.meid
232         params.Xid = trans.xid
233         params.Mbuf = nil
234
235         tc.Info("SEND SUB REQ: %s", params.String())
236         snderr := tc.SendWithRetry(params, false, 5)
237         if snderr != nil {
238                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
239                 return nil
240         }
241         return trans
242 }
243
244 //-----------------------------------------------------------------------------
245 //
246 //-----------------------------------------------------------------------------
247 func (tc *E2Stub) RecvSubsReq(t *testing.T) (*e2ap.E2APSubscriptionRequest, *xapp.RMRParams) {
248         tc.Info("RecvSubsReq")
249         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
250
251         //---------------------------------
252         // e2term activity: Recv Subs Req
253         //---------------------------------
254         msg := tc.WaitMsg(15)
255         if msg != nil {
256                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
257                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype])
258                 } else {
259                         tc.Info("Recv Subs Req")
260                         packedData := &e2ap.PackedData{}
261                         packedData.Buf = msg.Payload
262                         unpackerr, req := e2SubsReq.UnPack(packedData)
263                         if unpackerr != nil {
264                                 tc.TestError(t, "RIC_SUB_REQ unpack failed err: %s", unpackerr.Error())
265                         }
266                         return req, msg
267                 }
268         } else {
269                 tc.TestError(t, "Not Received msg within %d secs", 15)
270         }
271
272         return nil, nil
273 }
274
275 //-----------------------------------------------------------------------------
276 //
277 //-----------------------------------------------------------------------------
278 func (tc *E2Stub) SendSubsResp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *xapp.RMRParams) {
279         tc.Info("SendSubsResp")
280         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
281
282         //---------------------------------
283         // e2term activity: Send Subs Resp
284         //---------------------------------
285         resp := &e2ap.E2APSubscriptionResponse{}
286
287         resp.RequestId.Id = req.RequestId.Id
288         resp.RequestId.InstanceId = req.RequestId.InstanceId
289         resp.FunctionId = req.FunctionId
290
291         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
292         for index := int(0); index < len(req.ActionSetups); index++ {
293                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
294         }
295
296         for index := uint64(0); index < 1; index++ {
297                 item := e2ap.ActionNotAdmittedItem{}
298                 item.ActionId = index
299                 item.Cause.Content = 1
300                 item.Cause.Value = 1
301                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
302         }
303
304         packerr, packedMsg := e2SubsResp.Pack(resp)
305         if packerr != nil {
306                 tc.TestError(t, "pack NOK %s", packerr.Error())
307         }
308         tc.Debug("%s", e2SubsResp.String())
309
310         params := &xapp.RMRParams{}
311         params.Mtype = xapp.RIC_SUB_RESP
312         //params.SubId = msg.SubId
313         params.SubId = -1
314         params.Payload = packedMsg.Buf
315         params.PayloadLen = len(packedMsg.Buf)
316         params.Meid = msg.Meid
317         //params.Xid = msg.Xid
318         params.Mbuf = nil
319
320         tc.Info("SEND SUB RESP: %s", params.String())
321         snderr := tc.SendWithRetry(params, false, 5)
322         if snderr != nil {
323                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
324         }
325 }
326
327 //-----------------------------------------------------------------------------
328 //
329 //-----------------------------------------------------------------------------
330 func (tc *E2Stub) RecvSubsResp(t *testing.T, trans *RmrTransactionId) uint32 {
331         tc.Info("RecvSubsResp")
332         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
333         var e2SubsId uint32
334
335         //---------------------------------
336         // xapp activity: Recv Subs Resp
337         //---------------------------------
338         msg := tc.WaitMsg(15)
339         if msg != nil {
340                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
341                         tc.TestError(t, "Received RIC_SUB_RESP wrong mtype expected %s got %s, error", "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
342                         return 0
343                 } else if msg.Xid != trans.xid {
344                         tc.TestError(t, "Received RIC_SUB_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
345                         return 0
346                 } else {
347                         packedData := &e2ap.PackedData{}
348                         packedData.Buf = msg.Payload
349                         if msg.SubId > 0 {
350                                 e2SubsId = uint32(msg.SubId)
351                         } else {
352                                 e2SubsId = 0
353                         }
354                         unpackerr, resp := e2SubsResp.UnPack(packedData)
355                         if unpackerr != nil {
356                                 tc.TestError(t, "RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
357                         }
358                         tc.Info("Recv Subs Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
359                         return e2SubsId
360                 }
361         } else {
362                 tc.TestError(t, "Not Received msg within %d secs", 15)
363         }
364         return 0
365 }
366
367 //-----------------------------------------------------------------------------
368 //
369 //-----------------------------------------------------------------------------
370
371 func (tc *E2Stub) SendSubsFail(t *testing.T, fparams *E2StubSubsFailParams, msg *xapp.RMRParams) {
372         tc.Info("SendSubsFail")
373         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
374
375         //---------------------------------
376         // e2term activity: Send Subs Fail
377         //---------------------------------
378         packerr, packedMsg := e2SubsFail.Pack(fparams.Fail)
379         if packerr != nil {
380                 tc.TestError(t, "pack NOK %s", packerr.Error())
381         }
382         tc.Debug("%s", e2SubsFail.String())
383
384         params := &xapp.RMRParams{}
385         params.Mtype = xapp.RIC_SUB_FAILURE
386         params.SubId = msg.SubId
387         params.Payload = packedMsg.Buf
388         params.PayloadLen = len(packedMsg.Buf)
389         params.Meid = msg.Meid
390         params.Xid = msg.Xid
391         params.Mbuf = nil
392
393         tc.Info("SEND SUB FAIL: %s", params.String())
394         snderr := tc.SendWithRetry(params, false, 5)
395         if snderr != nil {
396                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
397         }
398 }
399
400 //-----------------------------------------------------------------------------
401 //
402 //-----------------------------------------------------------------------------
403 func (tc *E2Stub) RecvSubsFail(t *testing.T, trans *RmrTransactionId) uint32 {
404         tc.Info("RecvSubsFail")
405         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
406         var e2SubsId uint32
407
408         //-------------------------------
409         // xapp activity: Recv Subs Fail
410         //-------------------------------
411         msg := tc.WaitMsg(15)
412         if msg != nil {
413                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
414                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
415                         return 0
416                 } else if msg.Xid != trans.xid {
417                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", trans.xid, msg.Xid)
418                         return 0
419                 } else {
420                         packedData := &e2ap.PackedData{}
421                         packedData.Buf = msg.Payload
422                         if msg.SubId > 0 {
423                                 e2SubsId = uint32(msg.SubId)
424                         } else {
425                                 e2SubsId = 0
426                         }
427                         unpackerr, resp := e2SubsFail.UnPack(packedData)
428                         if unpackerr != nil {
429                                 tc.TestError(t, "RIC_SUB_FAILURE unpack failed err: %s", unpackerr.Error())
430                         }
431                         tc.Info("Recv Subs Fail rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
432                         return e2SubsId
433                 }
434         } else {
435                 tc.TestError(t, "Not Received msg within %d secs", 15)
436         }
437         return 0
438 }
439
440 //-----------------------------------------------------------------------------
441 //
442 //-----------------------------------------------------------------------------
443 func (tc *E2Stub) SendSubsDelReq(t *testing.T, oldTrans *RmrTransactionId, e2SubsId uint32) *RmrTransactionId {
444
445         trans := oldTrans
446         if oldTrans == nil {
447                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
448         }
449
450         tc.Info("SendSubsDelReq %s", trans.String())
451         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
452         //---------------------------------
453         // xapp activity: Send Subs Del Req
454         //---------------------------------
455         req := &e2ap.E2APSubscriptionDeleteRequest{}
456         req.RequestId.Id = 1
457         req.RequestId.InstanceId = e2SubsId
458         req.FunctionId = 1
459
460         err, packedMsg := e2SubsDelReq.Pack(req)
461         if err != nil {
462                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
463                 return nil
464         }
465         tc.Debug("%s %s", trans.String(), e2SubsDelReq.String())
466
467         params := &xapp.RMRParams{}
468         params.Mtype = xapp.RIC_SUB_DEL_REQ
469         params.SubId = int(e2SubsId)
470         params.Payload = packedMsg.Buf
471         params.PayloadLen = len(packedMsg.Buf)
472         params.Meid = trans.meid
473         params.Xid = trans.xid
474         params.Mbuf = nil
475
476         tc.Info("SEND SUB DEL REQ: %s", params.String())
477         snderr := tc.SendWithRetry(params, false, 5)
478         if snderr != nil {
479                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
480                 return nil
481         }
482         return trans
483 }
484
485 //-----------------------------------------------------------------------------
486 //
487 //-----------------------------------------------------------------------------
488 func (tc *E2Stub) RecvSubsDelReq(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *xapp.RMRParams) {
489         tc.Info("RecvSubsDelReq")
490         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
491
492         //---------------------------------
493         // e2term activity: Recv Subs Del Req
494         //---------------------------------
495         msg := tc.WaitMsg(15)
496         if msg != nil {
497                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
498                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
499                 } else {
500                         tc.Info("Recv Subs Del Req")
501
502                         packedData := &e2ap.PackedData{}
503                         packedData.Buf = msg.Payload
504                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
505                         if unpackerr != nil {
506                                 tc.TestError(t, "RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
507                         }
508                         return req, msg
509                 }
510         } else {
511                 tc.TestError(t, "Not Received msg within %d secs", 15)
512         }
513         return nil, nil
514 }
515
516 //-----------------------------------------------------------------------------
517 //
518 //-----------------------------------------------------------------------------
519 func (tc *E2Stub) SendSubsDelResp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
520         tc.Info("SendSubsDelResp")
521         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
522
523         //---------------------------------
524         // e2term activity: Send Subs Del Resp
525         //---------------------------------
526         resp := &e2ap.E2APSubscriptionDeleteResponse{}
527         resp.RequestId.Id = req.RequestId.Id
528         resp.RequestId.InstanceId = req.RequestId.InstanceId
529         resp.FunctionId = req.FunctionId
530
531         packerr, packedMsg := e2SubsDelResp.Pack(resp)
532         if packerr != nil {
533                 tc.TestError(t, "pack NOK %s", packerr.Error())
534         }
535         tc.Debug("%s", e2SubsDelResp.String())
536
537         params := &xapp.RMRParams{}
538         params.Mtype = xapp.RIC_SUB_DEL_RESP
539         params.SubId = msg.SubId
540         params.Payload = packedMsg.Buf
541         params.PayloadLen = len(packedMsg.Buf)
542         params.Meid = msg.Meid
543         params.Xid = msg.Xid
544         params.Mbuf = nil
545
546         tc.Info("SEND SUB DEL RESP: %s", params.String())
547         snderr := tc.SendWithRetry(params, false, 5)
548         if snderr != nil {
549                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
550         }
551 }
552
553 //-----------------------------------------------------------------------------
554 //
555 //-----------------------------------------------------------------------------
556 func (tc *E2Stub) RecvSubsDelResp(t *testing.T, trans *RmrTransactionId) {
557         tc.Info("RecvSubsDelResp")
558         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
559
560         //---------------------------------
561         // xapp activity: Recv Subs Del Resp
562         //---------------------------------
563         msg := tc.WaitMsg(15)
564         if msg != nil {
565                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
566                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype])
567                         return
568                 } else if trans != nil && msg.Xid != trans.xid {
569                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
570                         return
571                 } else {
572                         packedData := &e2ap.PackedData{}
573                         packedData.Buf = msg.Payload
574                         unpackerr, resp := e2SubsDelResp.UnPack(packedData)
575                         if unpackerr != nil {
576                                 tc.TestError(t, "RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
577                         }
578                         tc.Info("Recv Subs Del Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
579                         return
580                 }
581         } else {
582                 tc.TestError(t, "Not Received msg within %d secs", 15)
583         }
584 }
585
586 //-----------------------------------------------------------------------------
587 //
588 //-----------------------------------------------------------------------------
589 func (tc *E2Stub) SendSubsDelFail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
590         tc.Info("SendSubsDelFail")
591         e2SubsDelFail := e2asnpacker.NewPackerSubscriptionDeleteFailure()
592
593         //---------------------------------
594         // e2term activity: Send Subs Del Fail
595         //---------------------------------
596         resp := &e2ap.E2APSubscriptionDeleteFailure{}
597         resp.RequestId.Id = req.RequestId.Id
598         resp.RequestId.InstanceId = req.RequestId.InstanceId
599         resp.FunctionId = req.FunctionId
600         resp.Cause.Content = 4 // CauseMisc
601         resp.Cause.Value = 3   // unspecified
602
603         packerr, packedMsg := e2SubsDelFail.Pack(resp)
604         if packerr != nil {
605                 tc.TestError(t, "pack NOK %s", packerr.Error())
606         }
607         tc.Debug("%s", e2SubsDelFail.String())
608
609         params := &xapp.RMRParams{}
610         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
611         params.SubId = msg.SubId
612         params.Payload = packedMsg.Buf
613         params.PayloadLen = len(packedMsg.Buf)
614         params.Meid = msg.Meid
615         params.Xid = msg.Xid
616         params.Mbuf = nil
617
618         tc.Info("SEND SUB DEL FAIL: %s", params.String())
619         snderr := tc.SendWithRetry(params, false, 5)
620         if snderr != nil {
621                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
622         }
623 }
624
625 // REST code below all
626
627 /*****************************************************************************/
628 // REST interface specific functions are below
629
630 //-----------------------------------------------------------------------------
631 // Callback handler for subscription response notifications
632 //-----------------------------------------------------------------------------
633 func (tc *E2Stub) SubscriptionRespHandler(resp *clientmodel.SubscriptionResponse) {
634         if tc.subscriptionId == *resp.SubscriptionID {
635                 tc.Info("REST notification received SubscriptionID=%s, InstanceID=%v, RequestorID=%v (%v)",
636                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
637                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].InstanceID
638         } else {
639                 tc.Info("MISMATCHING REST notification received SubscriptionID=%s<>%s, InstanceID=%v, RequestorID=%v (%v)",
640                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
641         }
642 }
643
644 //-----------------------------------------------------------------------------
645 //
646 //-----------------------------------------------------------------------------
647 func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
648
649         tc.Info("### Started to wait REST notification for %v on port %v f(RMR port %v), %v responses expected", restSubsId, *tc.clientEndpoint.HTTPPort, *tc.clientEndpoint.RMRPort, tc.requestCount)
650         tc.restSubsIdList = []string{restSubsId}
651         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
652         if tc.requestCount == 0 {
653                 tc.TestError(t, "### NO REST notifications SET received for endpoint=%s, request zount ZERO! (%v)", tc.clientEndpoint, tc)
654         }
655         go func() {
656                 select {
657                 case e2Ids := <-tc.CallBackListedNotifications:
658                         if tc.requestCount == 0 {
659                                 tc.TestError(t, "### REST notification count unexpectedly ZERO for %s (%v)", restSubsId, tc)
660                         } else if e2Ids.RestSubsId != restSubsId {
661                                 tc.TestError(t, "### Unexpected REST notifications received |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
662                         } else {
663                                 tc.requestCount--
664                                 if tc.requestCount == 0 {
665                                         tc.Info("### All expected REST notifications received for %s (%v)", e2Ids.RestSubsId, tc)
666                                 } else {
667                                         tc.Info("### Expected REST notifications received for %s, (%v)", e2Ids.RestSubsId, tc)
668                                 }
669                                 tc.Info("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
670                                 tc.ListedRESTNotifications <- e2Ids
671                         }
672                 case <-time.After(15 * time.Second):
673                         err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
674                         tc.TestError(t, "%s", err.Error())
675                 }
676         }()
677 }
678
679 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
680         select {
681         case e2SubsId := <-tc.ListedRESTNotifications:
682                 if e2SubsId.RestSubsId == restSubsId {
683                         tc.Info("### Expected REST notifications received %s, e2SubsId %v for endpoint=%s, (%v)", e2SubsId.RestSubsId, e2SubsId.E2SubsId, tc.clientEndpoint, tc)
684                         return e2SubsId.E2SubsId
685                 } else {
686                         tc.TestError(t, "### Unexpected REST notifications received %s, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
687                         return 0
688                 }
689         case <-time.After(15 * time.Second):
690                 err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
691                 tc.TestError(t, "%s", err.Error())
692                 panic("WaitRESTNotification - timeout error")
693         }
694         return 0
695 }
696
697 func (tc *E2Stub) WaitRESTNotificationForAnySubscriptionId(t *testing.T) {
698         go func() {
699                 tc.Info("### REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
700                 select {
701                 case e2SubsId := <-tc.CallBackNotification:
702                         tc.Info("### REST notifications received e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
703                         tc.RESTNotification <- (uint32)(e2SubsId)
704                 case <-time.After(15 * time.Second):
705                         err := fmt.Errorf("### Timeout 15s expired while waiting REST notification")
706                         tc.TestError(t, "%s", err.Error())
707                         tc.RESTNotification <- 0
708                 }
709         }()
710 }
711
712 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
713
714         if len(tc.restSubsIdList) == 0 {
715                 tc.Error("Unexpected listed REST notifications received for endpoint=%s, expected restSubsId list size was ZERO!", tc.clientEndpoint)
716         } else {
717                 for i, subsId := range tc.restSubsIdList {
718                         if *resp.SubscriptionID == subsId {
719                                 //tc.Info("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, RequestorID=%v",
720                                 //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID)
721
722                                 tc.restSubsIdList = append(tc.restSubsIdList[:i], tc.restSubsIdList[i+1:]...)
723                                 //tc.Info("Removed %s from Listed REST notifications, %v entries left", *resp.SubscriptionID, len(tc.restSubsIdList))
724
725                                 tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].InstanceID)}
726
727                                 if len(tc.restSubsIdList) == 0 {
728                                         //tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
729                                 }
730
731                                 return
732                         }
733                 }
734                 tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, RequestorID=%v (%v)",
735                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].RequestorID, tc)
736         }
737 }
738
739 //-----------------------------------------------------------------------------
740 //
741 //-----------------------------------------------------------------------------
742 func (tc *E2Stub) WaitListedRestNotifications(t *testing.T, restSubsIds []string) {
743         tc.Info("Started to wait REST notifications %v on port %v f(RMR port %v)", restSubsIds, *tc.clientEndpoint.HTTPPort, *tc.clientEndpoint.RMRPort)
744
745         tc.restSubsIdList = restSubsIds
746         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
747
748         for i := 0; i < len(restSubsIds); i++ {
749                 go func() {
750                         select {
751                         case e2Ids := <-tc.CallBackListedNotifications:
752                                 tc.Info("Listed Notification waiter received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
753                                 tc.ListedRESTNotifications <- e2Ids
754                         case <-time.After(15 * time.Second):
755                                 err := fmt.Errorf("Timeout 15s expired while waiting Listed REST notification")
756                                 tc.TestError(t, "%s", err.Error())
757                                 tc.RESTNotification <- 0
758                         }
759                 }()
760         }
761 }
762
763 //-----------------------------------------------------------------------------
764 //
765 //-----------------------------------------------------------------------------
766 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string { // This need to be edited according to new specification
767         tc.Info("Posting REST Report subscriptions to Submgr")
768
769         if params == nil {
770                 tc.Info("SendRESTReportSubsReq: params == nil")
771                 return ""
772         }
773
774         resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
775         if err != nil {
776                 // Swagger generated code makes checks for the values that are inserted the subscription function
777                 // If error cause is unknown and POST is not done, the problem is in the inserted values
778                 tc.Error("REST report subscriptions failed %s", err.Error())
779         }
780         tc.subscriptionId = *resp.SubscriptionID
781         tc.Info("REST report subscriptions pushed successfully. SubscriptionID = %s, RequestCount = %v", *resp.SubscriptionID, tc.requestCount)
782         return *resp.SubscriptionID
783 }
784
785 //-----------------------------------------------------------------------------
786 //
787 //-----------------------------------------------------------------------------
788 func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int, parameterSet int, actionDefinitionPresent bool, actionParamCount int) *RESTSubsReqParams {
789
790         reportParams := RESTSubsReqParams{}
791         if parameterSet == 1 {
792                 reportParams.GetRESTSubsReqReportParams1(subReqCount, actionDefinitionPresent, actionParamCount, &tc.clientEndpoint, &tc.meid)
793         } else if parameterSet == 2 {
794
795         } else {
796                 tc.Error("Invalid parameterSet=%v", parameterSet)
797         }
798         tc.requestCount = subReqCount
799         return &reportParams
800 }
801
802 //-----------------------------------------------------------------------------
803 //
804 //-----------------------------------------------------------------------------
805 func (tc *E2Stub) GetRESTSubsReqReportParams1(subReqCount int, actionDefinitionPresent bool, actionParamCount int) *RESTSubsReqParams {
806
807         reportParams := RESTSubsReqParams{}
808         reportParams.GetRESTSubsReqReportParams1(subReqCount, actionDefinitionPresent, actionParamCount, &tc.clientEndpoint, &tc.meid)
809         tc.requestCount = subReqCount
810         return &reportParams
811 }
812
813 //-----------------------------------------------------------------------------
814 //
815 //-----------------------------------------------------------------------------
816 type RESTSubsReqParams struct {
817         SubsReqParams clientmodel.SubscriptionParams
818 }
819
820 func (p *RESTSubsReqParams) GetRESTSubsReqReportParams1(subReqCount int, actionDefinitionPresent bool, actionParamCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
821
822         // E2SM-gNB-X2
823         p.SubsReqParams.ClientEndpoint = clientEndpoint
824         p.SubsReqParams.Meid = meid
825         var rANFunctionID int64 = 33
826         p.SubsReqParams.RANFunctionID = &rANFunctionID
827
828         //      reqId := int64(1)
829         //seqId := int64(1)
830         actionId := int64(1)
831         actionType := "report"
832         subsequestActioType := "continue"
833         timeToWait := "w10ms"
834
835         for requestCount := 0; requestCount < subReqCount; requestCount++ {
836                 reqId := int64(requestCount) + 1
837                 seqId := int64(requestCount) + 1
838                 subscriptionDetail := &clientmodel.SubscriptionDetail{
839                         RequestorID: &reqId,
840                         InstanceID:  &seqId,
841                         EventTriggers: &clientmodel.EventTriggerDefinition{
842                                 OctetString: "1234" + strconv.Itoa(requestCount),
843                         },
844                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
845                                 &clientmodel.ActionToBeSetup{
846                                         ActionID:   &actionId,
847                                         ActionType: &actionType,
848                                         ActionDefinition: &clientmodel.ActionDefinition{
849                                                 OctetString: "5678" + strconv.Itoa(requestCount),
850                                         },
851                                         SubsequentAction: &clientmodel.SubsequentAction{
852                                                 SubsequentActionType: &subsequestActioType,
853                                                 TimeToWait:           &timeToWait,
854                                         },
855                                 },
856                         },
857                 }
858                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
859         }
860
861 }
862
863 func (p *RESTSubsReqParams) SetMeid(MEID string) {
864         p.SubsReqParams.Meid = &MEID
865 }
866
867 func (p *RESTSubsReqParams) SetEndpoint(HTTP_port int64, RMR_port int64, host string) {
868         var endpoint clientmodel.SubscriptionParamsClientEndpoint
869         endpoint.HTTPPort = &HTTP_port
870         endpoint.RMRPort = &RMR_port
871         endpoint.Host = host
872         p.SubsReqParams.ClientEndpoint = &endpoint
873 }
874
875 func (p *RESTSubsReqParams) SetEndpointHost(host string) {
876
877         if p.SubsReqParams.ClientEndpoint.Host != "" {
878                 if p.SubsReqParams.ClientEndpoint.Host != host {
879                         // Renaming toto, print something tc.Info("Posting REST subscription request to Submgr")
880                         err := fmt.Errorf("hostname change attempt: %s -> %s", p.SubsReqParams.ClientEndpoint.Host, host)
881                         panic(err)
882                 }
883         }
884         p.SubsReqParams.ClientEndpoint.Host = host
885 }
886
887 func (p *RESTSubsReqParams) SetHTTPEndpoint(HTTP_port int64, host string) {
888
889         p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
890
891         p.SetEndpointHost(host)
892
893         if p.SubsReqParams.ClientEndpoint.RMRPort == nil {
894                 var RMR_port int64
895                 p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
896         }
897 }
898
899 func (p *RESTSubsReqParams) SetSubActionTypes(actionType string) {
900
901         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
902                 for _, action := range subDetail.ActionToBeSetupList {
903                         if action != nil {
904                                 action.ActionType = &actionType
905                         }
906                 }
907         }
908 }
909
910 func (p *RESTSubsReqParams) SetRMREndpoint(RMR_port int64, host string) {
911
912         p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
913
914         p.SetEndpointHost(host)
915
916         if p.SubsReqParams.ClientEndpoint.HTTPPort == nil {
917                 var HTTP_port int64
918                 p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
919         }
920 }
921
922 func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
923
924         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
925                 for _, action := range subDetail.ActionToBeSetupList {
926                         if action != nil && action.SubsequentAction != nil {
927                                 action.SubsequentAction.TimeToWait = &timeToWait
928                         }
929                 }
930         }
931 }
932
933 //-----------------------------------------------------------------------------
934 //
935 //-----------------------------------------------------------------------------
936 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
937
938         if *subscriptionID == "" {
939                 tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
940         }
941         tc.Info("REST deleting E2 subscriptions. SubscriptionID = %s", *subscriptionID)
942
943         err := xapp.Subscription.Unsubscribe(*subscriptionID)
944         if err != nil {
945                 tc.Error("REST Delete subscription failed %s", err.Error())
946         }
947         tc.Info("REST delete subscription pushed to submgr successfully. SubscriptionID = %s", *subscriptionID)
948 }
949
950 //-----------------------------------------------------------------------------
951 //
952 //-----------------------------------------------------------------------------
953 func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int, actionDefinitionPresent bool, policyParamCount int) *RESTSubsReqParams {
954
955         policyParams := RESTSubsReqParams{}
956         policyParams.GetRESTSubsReqPolicyParams(subReqCount, actionDefinitionPresent, policyParamCount, &tc.clientEndpoint, &tc.meid)
957         tc.requestCount = subReqCount
958         return &policyParams
959 }
960
961 //-----------------------------------------------------------------------------
962 //
963 //-----------------------------------------------------------------------------
964 func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, actionDefinitionPresent bool, policyParamCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
965
966         p.SubsReqParams.ClientEndpoint = clientEndpoint
967         p.SubsReqParams.Meid = meid
968         var rANFunctionID int64 = 33
969         p.SubsReqParams.RANFunctionID = &rANFunctionID
970
971         //      reqId := int64(1)
972         //seqId := int64(1)
973         actionId := int64(1)
974         actionType := "policy"
975         subsequestActioType := "continue"
976         timeToWait := "w10ms"
977
978         for requestCount := 0; requestCount < subReqCount; requestCount++ {
979                 reqId := int64(requestCount) + 1
980                 seqId := int64(0)
981                 subscriptionDetail := &clientmodel.SubscriptionDetail{
982                         RequestorID: &reqId,
983                         InstanceID:  &seqId,
984                         EventTriggers: &clientmodel.EventTriggerDefinition{
985                                 OctetString: "1234" + strconv.Itoa(requestCount),
986                         },
987                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
988                                 &clientmodel.ActionToBeSetup{
989                                         ActionID:   &actionId,
990                                         ActionType: &actionType,
991                                         ActionDefinition: &clientmodel.ActionDefinition{
992                                                 OctetString: "5678" + strconv.Itoa(requestCount),
993                                         },
994                                         SubsequentAction: &clientmodel.SubsequentAction{
995                                                 SubsequentActionType: &subsequestActioType,
996                                                 TimeToWait:           &timeToWait,
997                                         },
998                                 },
999                         },
1000                 }
1001                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1002         }
1003
1004 }