J release: Release container Image
[ric-plt/submgr.git] / 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) SendInvalidE2Asn1Resp(t *testing.T, msg *xapp.RMRParams, msgType int) {
331
332         params := &xapp.RMRParams{}
333         params.Mtype = msgType
334         params.SubId = -1
335         params.Payload = []byte{1, 2, 3, 4, 5}
336         params.PayloadLen = 5
337         params.Meid = msg.Meid
338         params.Xid = ""
339         params.Mbuf = nil
340
341         if params.Mtype == xapp.RIC_SUB_RESP {
342                 tc.Info("SEND INVALID ASN.1 SUB RESP")
343
344         } else if params.Mtype == xapp.RIC_SUB_FAILURE {
345                 tc.Info("SEND INVALID ASN.1 SUB FAILURE")
346
347         } else if params.Mtype == xapp.RIC_SUB_DEL_RESP {
348                 tc.Info("SEND INVALID ASN.1 SUB DEL RESP")
349
350         } else if params.Mtype == xapp.RIC_SUB_DEL_FAILURE {
351                 tc.Info("SEND INVALID ASN.1 SUB DEL FAILURE")
352         }
353         snderr := tc.SendWithRetry(params, false, 5)
354         if snderr != nil {
355                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
356         }
357 }
358
359 //-----------------------------------------------------------------------------
360 //
361 //-----------------------------------------------------------------------------
362 func (tc *E2Stub) RecvSubsResp(t *testing.T, trans *RmrTransactionId) uint32 {
363         tc.Info("RecvSubsResp")
364         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
365         var e2SubsId uint32
366
367         //---------------------------------
368         // xapp activity: Recv Subs Resp
369         //---------------------------------
370         msg := tc.WaitMsg(15)
371         if msg != nil {
372                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
373                         tc.TestError(t, "Received RIC_SUB_RESP wrong mtype expected %s got %s, error", "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
374                         return 0
375                 } else if msg.Xid != trans.xid {
376                         tc.TestError(t, "Received RIC_SUB_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
377                         return 0
378                 } else {
379                         packedData := &e2ap.PackedData{}
380                         packedData.Buf = msg.Payload
381                         if msg.SubId > 0 {
382                                 e2SubsId = uint32(msg.SubId)
383                         } else {
384                                 e2SubsId = 0
385                         }
386                         unpackerr, resp := e2SubsResp.UnPack(packedData)
387                         if unpackerr != nil {
388                                 tc.TestError(t, "RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
389                         }
390                         tc.Info("Recv Subs Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
391                         return e2SubsId
392                 }
393         } else {
394                 tc.TestError(t, "Not Received msg within %d secs", 15)
395         }
396         return 0
397 }
398
399 //-----------------------------------------------------------------------------
400 //
401 //-----------------------------------------------------------------------------
402
403 func (tc *E2Stub) SendSubsFail(t *testing.T, fparams *E2StubSubsFailParams, msg *xapp.RMRParams) {
404         tc.Info("SendSubsFail")
405         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
406
407         //---------------------------------
408         // e2term activity: Send Subs Fail
409         //---------------------------------
410         packerr, packedMsg := e2SubsFail.Pack(fparams.Fail)
411         if packerr != nil {
412                 tc.TestError(t, "pack NOK %s", packerr.Error())
413         }
414         tc.Debug("%s", e2SubsFail.String())
415
416         params := &xapp.RMRParams{}
417         params.Mtype = xapp.RIC_SUB_FAILURE
418         params.SubId = msg.SubId
419         params.Payload = packedMsg.Buf
420         params.PayloadLen = len(packedMsg.Buf)
421         params.Meid = msg.Meid
422         params.Xid = msg.Xid
423         params.Mbuf = nil
424
425         tc.Info("SEND SUB FAIL: %s", params.String())
426         snderr := tc.SendWithRetry(params, false, 5)
427         if snderr != nil {
428                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
429         }
430 }
431
432 //-----------------------------------------------------------------------------
433 //
434 //-----------------------------------------------------------------------------
435 func (tc *E2Stub) RecvSubsFail(t *testing.T, trans *RmrTransactionId) uint32 {
436         tc.Info("RecvSubsFail")
437         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
438         var e2SubsId uint32
439
440         //-------------------------------
441         // xapp activity: Recv Subs Fail
442         //-------------------------------
443         msg := tc.WaitMsg(15)
444         if msg != nil {
445                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
446                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
447                         return 0
448                 } else if msg.Xid != trans.xid {
449                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", trans.xid, msg.Xid)
450                         return 0
451                 } else {
452                         packedData := &e2ap.PackedData{}
453                         packedData.Buf = msg.Payload
454                         if msg.SubId > 0 {
455                                 e2SubsId = uint32(msg.SubId)
456                         } else {
457                                 e2SubsId = 0
458                         }
459                         unpackerr, resp := e2SubsFail.UnPack(packedData)
460                         if unpackerr != nil {
461                                 tc.TestError(t, "RIC_SUB_FAILURE unpack failed err: %s", unpackerr.Error())
462                         }
463                         tc.Info("Recv Subs Fail rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
464                         return e2SubsId
465                 }
466         } else {
467                 tc.TestError(t, "Not Received msg within %d secs", 15)
468         }
469         return 0
470 }
471
472 //-----------------------------------------------------------------------------
473 //
474 //-----------------------------------------------------------------------------
475 func (tc *E2Stub) SendSubsDelReq(t *testing.T, oldTrans *RmrTransactionId, e2SubsId uint32) *RmrTransactionId {
476
477         trans := oldTrans
478         if oldTrans == nil {
479                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
480         }
481
482         tc.Info("SendSubsDelReq %s", trans.String())
483         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
484         //---------------------------------
485         // xapp activity: Send Subs Del Req
486         //---------------------------------
487         req := &e2ap.E2APSubscriptionDeleteRequest{}
488         req.RequestId.Id = 1
489         req.RequestId.InstanceId = e2SubsId
490         req.FunctionId = 1
491
492         err, packedMsg := e2SubsDelReq.Pack(req)
493         if err != nil {
494                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
495                 return nil
496         }
497         tc.Debug("%s %s", trans.String(), e2SubsDelReq.String())
498
499         params := &xapp.RMRParams{}
500         params.Mtype = xapp.RIC_SUB_DEL_REQ
501         params.SubId = int(e2SubsId)
502         params.Payload = packedMsg.Buf
503         params.PayloadLen = len(packedMsg.Buf)
504         params.Meid = trans.meid
505         params.Xid = trans.xid
506         params.Mbuf = nil
507
508         tc.Info("SEND SUB DEL REQ: %s", params.String())
509         snderr := tc.SendWithRetry(params, false, 5)
510         if snderr != nil {
511                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
512                 return nil
513         }
514         return trans
515 }
516
517 //-----------------------------------------------------------------------------
518 //
519 //-----------------------------------------------------------------------------
520 func (tc *E2Stub) RecvSubsDelReq(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *xapp.RMRParams) {
521         tc.Info("RecvSubsDelReq")
522         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
523
524         //---------------------------------
525         // e2term activity: Recv Subs Del Req
526         //---------------------------------
527         msg := tc.WaitMsg(15)
528         if msg != nil {
529                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
530                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
531                 } else {
532                         tc.Info("Recv Subs Del Req")
533
534                         packedData := &e2ap.PackedData{}
535                         packedData.Buf = msg.Payload
536                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
537                         if unpackerr != nil {
538                                 tc.TestError(t, "RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
539                         }
540                         return req, msg
541                 }
542         } else {
543                 tc.TestError(t, "Not Received msg within %d secs", 15)
544         }
545         return nil, nil
546 }
547
548 //-----------------------------------------------------------------------------
549 //
550 //-----------------------------------------------------------------------------
551 func (tc *E2Stub) SendSubsDelResp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
552         tc.Info("SendSubsDelResp")
553         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
554
555         //---------------------------------
556         // e2term activity: Send Subs Del Resp
557         //---------------------------------
558         resp := &e2ap.E2APSubscriptionDeleteResponse{}
559         resp.RequestId.Id = req.RequestId.Id
560         resp.RequestId.InstanceId = req.RequestId.InstanceId
561         resp.FunctionId = req.FunctionId
562
563         packerr, packedMsg := e2SubsDelResp.Pack(resp)
564         if packerr != nil {
565                 tc.TestError(t, "pack NOK %s", packerr.Error())
566         }
567         tc.Debug("%s", e2SubsDelResp.String())
568
569         params := &xapp.RMRParams{}
570         params.Mtype = xapp.RIC_SUB_DEL_RESP
571         params.SubId = msg.SubId
572         params.Payload = packedMsg.Buf
573         params.PayloadLen = len(packedMsg.Buf)
574         params.Meid = msg.Meid
575         params.Xid = msg.Xid
576         params.Mbuf = nil
577
578         tc.Info("SEND SUB DEL RESP: %s", params.String())
579         snderr := tc.SendWithRetry(params, false, 5)
580         if snderr != nil {
581                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
582         }
583 }
584
585 //-----------------------------------------------------------------------------
586 //
587 //-----------------------------------------------------------------------------
588 func (tc *E2Stub) RecvSubsDelResp(t *testing.T, trans *RmrTransactionId) {
589         tc.Info("RecvSubsDelResp")
590         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
591
592         //---------------------------------
593         // xapp activity: Recv Subs Del Resp
594         //---------------------------------
595         msg := tc.WaitMsg(15)
596         if msg != nil {
597                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
598                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype])
599                         return
600                 } else if trans != nil && msg.Xid != trans.xid {
601                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
602                         return
603                 } else {
604                         packedData := &e2ap.PackedData{}
605                         packedData.Buf = msg.Payload
606                         unpackerr, resp := e2SubsDelResp.UnPack(packedData)
607                         if unpackerr != nil {
608                                 tc.TestError(t, "RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
609                         }
610                         tc.Info("Recv Subs Del Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
611                         return
612                 }
613         } else {
614                 tc.TestError(t, "Not Received msg within %d secs", 15)
615         }
616 }
617
618 //-----------------------------------------------------------------------------
619 //
620 //-----------------------------------------------------------------------------
621 func (tc *E2Stub) SendSubsDelFail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
622         tc.Info("SendSubsDelFail")
623         e2SubsDelFail := e2asnpacker.NewPackerSubscriptionDeleteFailure()
624
625         //---------------------------------
626         // e2term activity: Send Subs Del Fail
627         //---------------------------------
628         resp := &e2ap.E2APSubscriptionDeleteFailure{}
629         resp.RequestId.Id = req.RequestId.Id
630         resp.RequestId.InstanceId = req.RequestId.InstanceId
631         resp.FunctionId = req.FunctionId
632         resp.Cause.Content = 5 // CauseMisc
633         resp.Cause.Value = 3   // unspecified
634
635         packerr, packedMsg := e2SubsDelFail.Pack(resp)
636         if packerr != nil {
637                 tc.TestError(t, "pack NOK %s", packerr.Error())
638         }
639         tc.Debug("%s", e2SubsDelFail.String())
640
641         params := &xapp.RMRParams{}
642         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
643         params.SubId = msg.SubId
644         params.Payload = packedMsg.Buf
645         params.PayloadLen = len(packedMsg.Buf)
646         params.Meid = msg.Meid
647         params.Xid = msg.Xid
648         params.Mbuf = nil
649
650         tc.Info("SEND SUB DEL FAIL: %s", params.String())
651         snderr := tc.SendWithRetry(params, false, 5)
652         if snderr != nil {
653                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
654         }
655 }
656
657 // REST code below all
658
659 /*****************************************************************************/
660 // REST interface specific functions are below
661
662 //-----------------------------------------------------------------------------
663 // Callback handler for subscription response notifications
664 //-----------------------------------------------------------------------------
665 func (tc *E2Stub) SubscriptionRespHandler(resp *clientmodel.SubscriptionResponse) {
666         if tc.subscriptionId == "SUBSCRIPTIONID NOT SET" {
667                 tc.Info("REST notification received for %v while no SubscriptionID was not set for E2EventInstanceID=%v, XappEventInstanceID=%v (%v)",
668                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
669                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
670         } else if tc.subscriptionId == *resp.SubscriptionID {
671                 tc.Info("REST notification received SubscriptionID=%s, E2EventInstanceID=%v, RequestorID=%v (%v)",
672                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
673                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
674         } else {
675                 tc.Info("MISMATCHING REST notification received SubscriptionID=%s<>%s, E2EventInstanceID=%v, XappEventInstanceID=%v (%v)",
676                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
677         }
678 }
679
680 //-----------------------------------------------------------------------------
681 //
682 //-----------------------------------------------------------------------------
683 func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
684
685         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)
686         tc.restSubsIdList = []string{restSubsId}
687         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
688         if tc.requestCount == 0 {
689                 tc.TestError(t, "### NO REST notifications SET received for endpoint=%s, request zount ZERO! (%v)", tc.clientEndpoint, tc)
690         }
691         go func() {
692                 select {
693                 case e2Ids := <-tc.CallBackListedNotifications:
694                         if tc.requestCount == 0 {
695                                 tc.TestError(t, "### REST notification count unexpectedly ZERO for %s (%v)", restSubsId, tc)
696                         } else if e2Ids.RestSubsId != restSubsId {
697                                 tc.TestError(t, "### Unexpected REST notifications received |%s:%s| (%v)", e2Ids.RestSubsId, restSubsId, tc)
698                         } else {
699                                 tc.requestCount--
700                                 if tc.requestCount == 0 {
701                                         tc.Info("### All expected REST notifications received for %s (%v)", e2Ids.RestSubsId, tc)
702                                 } else {
703                                         tc.Info("### Expected REST notifications received for %s, (%v)", e2Ids.RestSubsId, tc)
704                                 }
705                                 tc.Info("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
706                                 tc.ListedRESTNotifications <- e2Ids
707                         }
708                 case <-time.After(15 * time.Second):
709                         err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
710                         tc.TestError(t, "%s", err.Error())
711                 }
712         }()
713 }
714
715 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
716         select {
717         case e2SubsId := <-tc.ListedRESTNotifications:
718                 if e2SubsId.RestSubsId == restSubsId {
719                         tc.Info("### Expected REST notifications received %s, e2SubsId %v for endpoint=%s, (%v)", e2SubsId.RestSubsId, e2SubsId.E2SubsId, tc.clientEndpoint, tc)
720                         return e2SubsId.E2SubsId
721                 } else {
722                         tc.TestError(t, "### Unexpected REST notifications received %s, expected %s for endpoint=%s, (%v)", e2SubsId.RestSubsId, restSubsId, tc.clientEndpoint, tc)
723                         return 0
724                 }
725         case <-time.After(15 * time.Second):
726                 err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
727                 tc.TestError(t, "%s", err.Error())
728                 panic("WaitRESTNotification - timeout error")
729         }
730         return 0
731 }
732
733 func (tc *E2Stub) WaitRESTNotificationForAnySubscriptionId(t *testing.T) {
734         go func() {
735                 tc.Info("### REST notifications received for endpoint=%s, (%v)", tc.clientEndpoint, tc)
736                 select {
737                 case e2SubsId := <-tc.CallBackNotification:
738                         tc.Info("### REST notifications received e2SubsId %v for endpoint=%s, (%v)", e2SubsId, tc.clientEndpoint, tc)
739                         tc.RESTNotification <- (uint32)(e2SubsId)
740                 case <-time.After(15 * time.Second):
741                         err := fmt.Errorf("### Timeout 15s expired while waiting REST notification")
742                         tc.TestError(t, "%s", err.Error())
743                         tc.RESTNotification <- 0
744                 }
745         }()
746 }
747
748 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
749
750         if len(tc.restSubsIdList) == 0 {
751                 tc.Error("Unexpected listed REST notifications received for endpoint=%s, expected restSubsId list size was ZERO!", tc.clientEndpoint)
752         } else {
753                 for i, subsId := range tc.restSubsIdList {
754                         if *resp.SubscriptionID == subsId {
755                                 //tc.Info("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, XappEventInstanceID=%v",
756                                 //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
757
758                                 tc.restSubsIdList = append(tc.restSubsIdList[:i], tc.restSubsIdList[i+1:]...)
759                                 //tc.Info("Removed %s from Listed REST notifications, %v entries left", *resp.SubscriptionID, len(tc.restSubsIdList))
760
761                                 tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].E2EventInstanceID)}
762
763                                 if len(tc.restSubsIdList) == 0 {
764                                         //tc.Info("All listed REST notifications received for endpoint=%s", tc.clientEndpoint)
765                                 }
766
767                                 return
768                         }
769                 }
770                 tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, XappEventInstanceID=%v (%v)",
771                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID, tc)
772         }
773 }
774
775 //-----------------------------------------------------------------------------
776 //
777 //-----------------------------------------------------------------------------
778 func (tc *E2Stub) WaitListedRestNotifications(t *testing.T, restSubsIds []string) {
779         tc.Info("Started to wait REST notifications %v on port %v f(RMR port %v)", restSubsIds, *tc.clientEndpoint.HTTPPort, *tc.clientEndpoint.RMRPort)
780
781         tc.restSubsIdList = restSubsIds
782         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
783
784         for i := 0; i < len(restSubsIds); i++ {
785                 go func() {
786                         select {
787                         case e2Ids := <-tc.CallBackListedNotifications:
788                                 tc.Info("Listed Notification waiter received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
789                                 tc.ListedRESTNotifications <- e2Ids
790                         case <-time.After(15 * time.Second):
791                                 err := fmt.Errorf("Timeout 15s expired while waiting Listed REST notification")
792                                 tc.TestError(t, "%s", err.Error())
793                                 tc.RESTNotification <- 0
794                         }
795                 }()
796         }
797 }
798
799 //-----------------------------------------------------------------------------
800 //
801 //-----------------------------------------------------------------------------
802 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string { // This need to be edited according to new specification
803         tc.Info("======== Posting REST Report subscriptions to Submgr ======")
804
805         if params == nil {
806                 tc.Info("SendRESTReportSubsReq: params == nil")
807                 return ""
808         }
809
810         tc.subscriptionId = "SUBSCIPTIONID NOT SET"
811
812         resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
813         if err != nil {
814                 // Swagger generated code makes checks for the values that are inserted the subscription function
815                 // If error cause is unknown and POST is not done, the problem is in the inserted values
816                 tc.Error("======== REST report subscriptions failed %s ========", err.Error())
817                 return ""
818         }
819         tc.subscriptionId = *resp.SubscriptionID
820         tc.Info("======== REST report subscriptions posted successfully. SubscriptionID = %s, RequestCount = %v ========", *resp.SubscriptionID, tc.requestCount)
821         return *resp.SubscriptionID
822 }
823
824 //-----------------------------------------------------------------------------
825 //
826 //-----------------------------------------------------------------------------
827 func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int) *RESTSubsReqParams {
828
829         reportParams := RESTSubsReqParams{}
830         reportParams.GetRESTSubsReqReportParams(subReqCount, &tc.clientEndpoint, &tc.meid)
831         tc.requestCount = subReqCount
832         return &reportParams
833 }
834
835 //-----------------------------------------------------------------------------
836 //
837 //-----------------------------------------------------------------------------
838 type RESTSubsReqParams struct {
839         SubsReqParams clientmodel.SubscriptionParams
840 }
841
842 func (p *RESTSubsReqParams) GetRESTSubsReqReportParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
843
844         // E2SM-gNB-X2
845         p.SubsReqParams.ClientEndpoint = clientEndpoint
846         p.SubsReqParams.Meid = meid
847         var rANFunctionID int64 = 33
848         p.SubsReqParams.RANFunctionID = &rANFunctionID
849
850         //      reqId := int64(1)
851         //seqId := int64(1)
852         actionId := int64(1)
853         actionType := "report"
854         subsequestActioType := "continue"
855         timeToWait := "w10ms"
856
857         for requestCount := 0; requestCount < subReqCount; requestCount++ {
858                 reqId := int64(requestCount) + 1
859                 subscriptionDetail := &clientmodel.SubscriptionDetail{
860                         XappEventInstanceID: &reqId,
861                         EventTriggers: &clientmodel.EventTriggerDefinition{
862                                 OctetString: "1234" + strconv.Itoa(requestCount),
863                         },
864                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
865                                 &clientmodel.ActionToBeSetup{
866                                         ActionID:   &actionId,
867                                         ActionType: &actionType,
868                                         ActionDefinition: &clientmodel.ActionDefinition{
869                                                 OctetString: "5678" + strconv.Itoa(requestCount),
870                                         },
871                                         SubsequentAction: &clientmodel.SubsequentAction{
872                                                 SubsequentActionType: &subsequestActioType,
873                                                 TimeToWait:           &timeToWait,
874                                         },
875                                 },
876                         },
877                 }
878                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
879         }
880
881 }
882
883 func (p *RESTSubsReqParams) SetMeid(MEID string) {
884         p.SubsReqParams.Meid = &MEID
885 }
886
887 func (p *RESTSubsReqParams) SetEndpoint(HTTP_port int64, RMR_port int64, host string) {
888         var endpoint clientmodel.SubscriptionParamsClientEndpoint
889         endpoint.HTTPPort = &HTTP_port
890         endpoint.RMRPort = &RMR_port
891         endpoint.Host = host
892         p.SubsReqParams.ClientEndpoint = &endpoint
893 }
894
895 func (p *RESTSubsReqParams) SetEndpointHost(host string) {
896
897         if p.SubsReqParams.ClientEndpoint.Host != "" {
898                 if p.SubsReqParams.ClientEndpoint.Host != host {
899                         // Renaming toto, print something tc.Info("Posting REST subscription request to Submgr")
900                         err := fmt.Errorf("hostname change attempt: %s -> %s", p.SubsReqParams.ClientEndpoint.Host, host)
901                         panic(err)
902                 }
903         }
904         p.SubsReqParams.ClientEndpoint.Host = host
905 }
906
907 func (p *RESTSubsReqParams) SetHTTPEndpoint(HTTP_port int64, host string) {
908
909         p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
910
911         p.SetEndpointHost(host)
912
913         if p.SubsReqParams.ClientEndpoint.RMRPort == nil {
914                 var RMR_port int64
915                 p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
916         }
917 }
918
919 func (p *RESTSubsReqParams) SetSubActionTypes(actionType string) {
920
921         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
922                 for _, action := range subDetail.ActionToBeSetupList {
923                         if action != nil {
924                                 action.ActionType = &actionType
925                         }
926                 }
927         }
928 }
929
930 func (p *RESTSubsReqParams) SetSubActionIDs(actionId int64) {
931
932         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
933                 for _, action := range subDetail.ActionToBeSetupList {
934                         if action != nil {
935                                 action.ActionID = &actionId
936                         }
937                 }
938         }
939 }
940
941 func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition string) {
942
943         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
944                 for _, action := range subDetail.ActionToBeSetupList {
945                         if action != nil {
946                                 action.ActionDefinition.OctetString = actionDefinition
947                         }
948                 }
949         }
950 }
951
952 func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition string) {
953
954         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
955                 if subDetail != nil {
956                         subDetail.EventTriggers.OctetString = eventTriggerDefinition
957                 }
958         }
959 }
960
961 func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition string, subsequentActionType string, timeToWait string) {
962
963         actionToBeSetup := &clientmodel.ActionToBeSetup{
964                 ActionID:   &actionId,
965                 ActionType: &actionType,
966                 ActionDefinition: &clientmodel.ActionDefinition{
967                         OctetString: actionDefinition,
968                 },
969                 SubsequentAction: &clientmodel.SubsequentAction{
970                         SubsequentActionType: &subsequentActionType,
971                         TimeToWait:           &timeToWait,
972                 },
973         }
974
975         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
976                 if subDetail != nil {
977                         subDetail.ActionToBeSetupList = append(subDetail.ActionToBeSetupList, actionToBeSetup)
978                 }
979         }
980 }
981
982 func (p *RESTSubsReqParams) SetRMREndpoint(RMR_port int64, host string) {
983
984         p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
985
986         p.SetEndpointHost(host)
987
988         if p.SubsReqParams.ClientEndpoint.HTTPPort == nil {
989                 var HTTP_port int64
990                 p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
991         }
992 }
993
994 func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
995
996         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
997                 for _, action := range subDetail.ActionToBeSetupList {
998                         if action != nil && action.SubsequentAction != nil {
999                                 action.SubsequentAction.TimeToWait = &timeToWait
1000                         }
1001                 }
1002         }
1003 }
1004
1005 func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
1006
1007         if *SubscriptionID == "" {
1008                 return
1009         }
1010         p.SubsReqParams.SubscriptionID = *SubscriptionID
1011 }
1012
1013 //-----------------------------------------------------------------------------
1014 //
1015 //-----------------------------------------------------------------------------
1016 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
1017
1018         if *subscriptionID == "" {
1019                 tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
1020         }
1021         tc.Info("REST deleting E2 subscriptions. SubscriptionID = %s", *subscriptionID)
1022
1023         err := xapp.Subscription.Unsubscribe(*subscriptionID)
1024         if err != nil {
1025                 tc.Error("REST Delete subscription failed %s", err.Error())
1026         }
1027         tc.Info("REST delete subscription pushed to submgr successfully. SubscriptionID = %s", *subscriptionID)
1028 }
1029
1030 //-----------------------------------------------------------------------------
1031 //
1032 //-----------------------------------------------------------------------------
1033 func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams {
1034
1035         policyParams := RESTSubsReqParams{}
1036         policyParams.GetRESTSubsReqPolicyParams(subReqCount, &tc.clientEndpoint, &tc.meid)
1037         tc.requestCount = subReqCount
1038         return &policyParams
1039 }
1040
1041 //-----------------------------------------------------------------------------
1042 //
1043 //-----------------------------------------------------------------------------
1044 func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
1045
1046         p.SubsReqParams.ClientEndpoint = clientEndpoint
1047         p.SubsReqParams.Meid = meid
1048         var rANFunctionID int64 = 33
1049         p.SubsReqParams.RANFunctionID = &rANFunctionID
1050
1051         //      reqId := int64(1)
1052         //seqId := int64(1)
1053         actionId := int64(1)
1054         actionType := "policy"
1055         subsequestActioType := "continue"
1056         timeToWait := "w10ms"
1057
1058         for requestCount := 0; requestCount < subReqCount; requestCount++ {
1059                 reqId := int64(requestCount) + 1
1060                 subscriptionDetail := &clientmodel.SubscriptionDetail{
1061                         XappEventInstanceID: &reqId,
1062                         EventTriggers: &clientmodel.EventTriggerDefinition{
1063                                 OctetString: "1234" + strconv.Itoa(requestCount),
1064                         },
1065                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
1066                                 &clientmodel.ActionToBeSetup{
1067                                         ActionID:   &actionId,
1068                                         ActionType: &actionType,
1069                                         ActionDefinition: &clientmodel.ActionDefinition{
1070                                                 OctetString: "5678" + strconv.Itoa(requestCount),
1071                                         },
1072                                         SubsequentAction: &clientmodel.SubsequentAction{
1073                                                 SubsequentActionType: &subsequestActioType,
1074                                                 TimeToWait:           &timeToWait,
1075                                         },
1076                                 },
1077                         },
1078                 }
1079                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1080         }
1081
1082 }