RIC-867: Submgr adaptations to E2APv2.0
[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         "runtime/debug"
25         "strconv"
26         "testing"
27         "time"
28
29         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
30         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
31         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
32         clientmodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
33         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
34 )
35
36 //-----------------------------------------------------------------------------
37 //
38 //-----------------------------------------------------------------------------
39 var e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
40
41 //-----------------------------------------------------------------------------
42 //
43 //-----------------------------------------------------------------------------
44 type RmrTransactionId struct {
45         xid  string
46         meid *xapp.RMRMeid
47 }
48
49 type E2RestIds struct {
50         RestSubsId  string
51         E2SubsId    uint32
52         ErrorCause  string
53         ErrorSource string
54         TimeoutType string
55 }
56
57 func (trans *RmrTransactionId) String() string {
58         meidstr := "N/A"
59         if trans.meid != nil {
60                 meidstr = trans.meid.String()
61         }
62         return "trans(" + trans.xid + "/" + meidstr + ")"
63 }
64
65 type E2Stub struct {
66         teststub.RmrStubControl
67         xid_seq                     uint64
68         subscriptionId              string
69         requestCount                int
70         CallBackNotification        chan int64
71         RESTNotification            chan uint32
72         CallBackListedNotifications chan E2RestIds
73         ListedRESTNotifications     chan E2RestIds
74         clientEndpoint              clientmodel.SubscriptionParamsClientEndpoint
75         meid                        string
76         restSubsIdList              []string
77 }
78
79 //-----------------------------------------------------------------------------
80 //
81 //-----------------------------------------------------------------------------
82 func CreateNewE2Stub(desc string, srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc, stat string, mtypeseed int, ranName string, host string, RMRPort int64, HTTPPort int64) *E2Stub {
83         tc := &E2Stub{}
84         tc.RmrStubControl.Init(desc, srcId, rtgSvc, stat, mtypeseed)
85         tc.xid_seq = 1
86         tc.SetCheckXid(true)
87         tc.CallBackNotification = make(chan int64)
88         tc.RESTNotification = make(chan uint32)
89         tc.CallBackListedNotifications = make(chan E2RestIds)
90         tc.ListedRESTNotifications = make(chan E2RestIds, 2)
91         var endPoint clientmodel.SubscriptionParamsClientEndpoint
92         endPoint.Host = host
93         endPoint.HTTPPort = &HTTPPort
94         endPoint.RMRPort = &RMRPort
95         tc.clientEndpoint = endPoint
96         tc.meid = ranName
97         return tc
98 }
99
100 //-----------------------------------------------------------------------------
101 //
102 //-----------------------------------------------------------------------------
103 func CreateNewE2termStub(desc string, srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc, stat string, mtypeseed int) *E2Stub {
104         tc := &E2Stub{}
105         tc.RmrStubControl.Init(desc, srcId, rtgSvc, stat, mtypeseed)
106         tc.xid_seq = 1
107         tc.SetCheckXid(false)
108         return tc
109 }
110
111 //-----------------------------------------------------------------------------
112 //
113 //-----------------------------------------------------------------------------
114 func (tc *E2Stub) NewRmrTransactionId(xid string, ranname string) *RmrTransactionId {
115         trans := &RmrTransactionId{}
116         if len(xid) == 0 {
117                 trans.xid = tc.GetDesc() + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
118                 tc.xid_seq++
119         } else {
120                 trans.xid = xid
121         }
122         trans.meid = &xapp.RMRMeid{RanName: ranname}
123         tc.Debug("New test %s", trans.String())
124         return trans
125 }
126
127 //-----------------------------------------------------------------------------
128 //
129 //-----------------------------------------------------------------------------
130 type E2StubSubsReqParams struct {
131         Req *e2ap.E2APSubscriptionRequest
132 }
133
134 func (p *E2StubSubsReqParams) Init() {
135         p.Req = &e2ap.E2APSubscriptionRequest{}
136
137         p.Req.RequestId.Id = 1
138         p.Req.RequestId.InstanceId = 0
139         p.Req.FunctionId = 1
140
141         // gnb -> enb outgoing
142         // enb -> gnb incoming
143         // X2 36423-f40.doc
144         p.Req.EventTriggerDefinition.Data.Length = 1
145         p.Req.EventTriggerDefinition.Data.Data = make([]uint8, p.Req.EventTriggerDefinition.Data.Length)
146         p.Req.EventTriggerDefinition.Data.Data[0] = 1
147
148         p.Req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
149
150         p.Req.ActionSetups[0].ActionId = 0
151         p.Req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
152         p.Req.ActionSetups[0].RicActionDefinitionPresent = true
153
154         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Length = 1
155         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Data = make([]uint8, p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Length)
156         p.Req.ActionSetups[0].ActionDefinitionChoice.Data.Data[0] = 1
157
158         p.Req.ActionSetups[0].SubsequentAction.Present = true
159         p.Req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
160         p.Req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
161 }
162
163 //-----------------------------------------------------------------------------
164 //
165 //-----------------------------------------------------------------------------
166
167 type E2StubSubsFailParams struct {
168         Req  *e2ap.E2APSubscriptionRequest
169         Fail *e2ap.E2APSubscriptionFailure
170 }
171
172 func (p *E2StubSubsFailParams) Set(req *e2ap.E2APSubscriptionRequest) {
173         p.Req = req
174
175         p.Fail = &e2ap.E2APSubscriptionFailure{}
176         p.Fail.RequestId.Id = p.Req.RequestId.Id
177         p.Fail.RequestId.InstanceId = p.Req.RequestId.InstanceId
178         p.Fail.FunctionId = p.Req.FunctionId
179         p.Fail.Cause.Content = e2ap.E2AP_CauseContent_RICrequest
180         p.Fail.Cause.Value = e2ap.E2AP_CauseValue_RICrequest_control_message_invalid
181 }
182
183 func (p *E2StubSubsFailParams) SetCauseVal(ind int, content uint8, causeval uint8) {
184
185         p.Fail.Cause.Content = content
186         p.Fail.Cause.Value = causeval
187 }
188
189 //-----------------------------------------------------------------------------
190 //
191 //-----------------------------------------------------------------------------
192
193 func (tc *E2Stub) SendSubsReq(t *testing.T, rparams *E2StubSubsReqParams, oldTrans *RmrTransactionId) *RmrTransactionId {
194
195         trans := oldTrans
196         if oldTrans == nil {
197                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
198         }
199
200         tc.Debug("SendSubsReq %s", trans.String())
201         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
202
203         //---------------------------------
204         // xapp activity: Send Subs Req
205         //---------------------------------
206         myparams := rparams
207
208         if myparams == nil {
209                 myparams = &E2StubSubsReqParams{}
210                 myparams.Init()
211         }
212
213         err, packedMsg := e2SubsReq.Pack(myparams.Req)
214         if err != nil {
215                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
216                 return nil
217         }
218         tc.Debug("%s %s", trans.String(), e2SubsReq.String())
219
220         params := &xapp.RMRParams{}
221         params.Mtype = xapp.RIC_SUB_REQ
222         params.SubId = -1
223         params.Payload = packedMsg.Buf
224         params.PayloadLen = len(packedMsg.Buf)
225         params.Meid = trans.meid
226         params.Xid = trans.xid
227         params.Mbuf = nil
228
229         tc.Debug("SEND SUB REQ: %s", params.String())
230         snderr := tc.SendWithRetry(params, false, 5)
231         if snderr != nil {
232                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
233                 return nil
234         }
235         return trans
236 }
237
238 //-----------------------------------------------------------------------------
239 //
240 //-----------------------------------------------------------------------------
241 func (tc *E2Stub) RecvSubsReq(t *testing.T) (*e2ap.E2APSubscriptionRequest, *xapp.RMRParams) {
242         tc.Debug("RecvSubsReq")
243         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
244
245         //---------------------------------
246         // e2term activity: Recv Subs Req
247         //---------------------------------
248         msg := tc.WaitMsg(15)
249         if msg != nil {
250                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
251                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype])
252                 } else {
253                         tc.Debug("Recv Subs Req")
254                         packedData := &e2ap.PackedData{}
255                         packedData.Buf = msg.Payload
256                         unpackerr, req := e2SubsReq.UnPack(packedData)
257                         if unpackerr != nil {
258                                 tc.TestError(t, "RIC_SUB_REQ unpack failed err: %s", unpackerr.Error())
259                         }
260                         return req, msg
261                 }
262         } else {
263                 tc.TestError(t, "Not Received msg within %d secs", 15)
264         }
265
266         return nil, nil
267 }
268
269 //-----------------------------------------------------------------------------
270 //
271 //-----------------------------------------------------------------------------
272 func (tc *E2Stub) SendSubsResp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *xapp.RMRParams) {
273         tc.Debug("SendSubsResp")
274         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
275
276         //---------------------------------
277         // e2term activity: Send Subs Resp
278         //---------------------------------
279         resp := &e2ap.E2APSubscriptionResponse{}
280
281         resp.RequestId.Id = req.RequestId.Id
282         resp.RequestId.InstanceId = req.RequestId.InstanceId
283         resp.FunctionId = req.FunctionId
284
285         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
286         for index := int(0); index < len(req.ActionSetups); index++ {
287                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
288         }
289
290         packerr, packedMsg := e2SubsResp.Pack(resp)
291         if packerr != nil {
292                 tc.TestError(t, "pack NOK %s", packerr.Error())
293         }
294         tc.Debug("%s", e2SubsResp.String())
295
296         params := &xapp.RMRParams{}
297         params.Mtype = xapp.RIC_SUB_RESP
298         //params.SubId = msg.SubId
299         params.SubId = -1
300         params.Payload = packedMsg.Buf
301         params.PayloadLen = len(packedMsg.Buf)
302         params.Meid = msg.Meid
303         //params.Xid = msg.Xid
304         params.Mbuf = nil
305
306         tc.Debug("SEND SUB RESP: %s", params.String())
307         snderr := tc.SendWithRetry(params, false, 5)
308         if snderr != nil {
309                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
310         }
311 }
312
313 //-----------------------------------------------------------------------------
314 //
315 //-----------------------------------------------------------------------------
316 func (tc *E2Stub) SendPartialSubsResp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *xapp.RMRParams, actionNotAdmittedList e2ap.ActionNotAdmittedList) {
317         tc.Debug("SendPartialSubsResp")
318
319         if len(actionNotAdmittedList.Items) == 0 {
320                 tc.TestError(t, "SendPartialSubsResp() Empty actionNotAdmittedList.Items")
321                 return
322         }
323
324         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
325
326         //---------------------------------
327         // e2term activity: Send Subs Resp
328         //---------------------------------
329         resp := &e2ap.E2APSubscriptionResponse{}
330
331         resp.RequestId.Id = req.RequestId.Id
332         resp.RequestId.InstanceId = req.RequestId.InstanceId
333         resp.FunctionId = req.FunctionId
334
335         for index, actionNotAdmittedItem := range actionNotAdmittedList.Items {
336                 for _, ActionToBeSetupItem := range req.ActionSetups {
337                         if ActionToBeSetupItem.ActionId == actionNotAdmittedItem.ActionId {
338                                 actionNotAdmittedItem := e2ap.ActionNotAdmittedItem{}
339                                 actionNotAdmittedItem.ActionId = ActionToBeSetupItem.ActionId
340                                 actionNotAdmittedItem.Cause.Content = 1
341                                 actionNotAdmittedItem.Cause.Value = 8
342                                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, actionNotAdmittedItem)
343                                 // Remove the element
344                                 req.ActionSetups = append(req.ActionSetups[:index], req.ActionSetups[index+1:]...)
345
346                         }
347                 }
348         }
349         for _, ActionToBeSetupItem := range req.ActionSetups {
350                 actionAdmittedItem := e2ap.ActionAdmittedItem{}
351                 actionAdmittedItem.ActionId = ActionToBeSetupItem.ActionId
352                 resp.ActionAdmittedList.Items = append(resp.ActionAdmittedList.Items, actionAdmittedItem)
353         }
354
355         packerr, packedMsg := e2SubsResp.Pack(resp)
356         if packerr != nil {
357                 tc.TestError(t, "pack NOK %s", packerr.Error())
358         }
359         tc.Debug("%s", e2SubsResp.String())
360
361         params := &xapp.RMRParams{}
362         params.Mtype = xapp.RIC_SUB_RESP
363         //params.SubId = msg.SubId
364         params.SubId = -1
365         params.Payload = packedMsg.Buf
366         params.PayloadLen = len(packedMsg.Buf)
367         params.Meid = msg.Meid
368         //params.Xid = msg.Xid
369         params.Mbuf = nil
370
371         tc.Debug("SEND SUB RESP: %s", params.String())
372         snderr := tc.SendWithRetry(params, false, 5)
373         if snderr != nil {
374                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
375         }
376 }
377
378 //-----------------------------------------------------------------------------
379 //
380 //-----------------------------------------------------------------------------
381 func (tc *E2Stub) SendInvalidE2Asn1Resp(t *testing.T, msg *xapp.RMRParams, msgType int) {
382
383         params := &xapp.RMRParams{}
384         params.Mtype = msgType
385         params.SubId = -1
386         params.Payload = []byte{1, 2, 3, 4, 5}
387         params.PayloadLen = 5
388         params.Meid = msg.Meid
389         params.Xid = ""
390         params.Mbuf = nil
391
392         if params.Mtype == xapp.RIC_SUB_RESP {
393                 tc.Debug("SEND INVALID ASN.1 SUB RESP")
394
395         } else if params.Mtype == xapp.RIC_SUB_FAILURE {
396                 tc.Debug("SEND INVALID ASN.1 SUB FAILURE")
397
398         } else if params.Mtype == xapp.RIC_SUB_DEL_RESP {
399                 tc.Debug("SEND INVALID ASN.1 SUB DEL RESP")
400
401         } else if params.Mtype == xapp.RIC_SUB_DEL_FAILURE {
402                 tc.Debug("SEND INVALID ASN.1 SUB DEL FAILURE")
403         }
404         snderr := tc.SendWithRetry(params, false, 5)
405         if snderr != nil {
406                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
407         }
408 }
409
410 //-----------------------------------------------------------------------------
411 //
412 //-----------------------------------------------------------------------------
413 func (tc *E2Stub) RecvSubsResp(t *testing.T, trans *RmrTransactionId) uint32 {
414         tc.Debug("RecvSubsResp")
415         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
416         var e2SubsId uint32
417
418         //---------------------------------
419         // xapp activity: Recv Subs Resp
420         //---------------------------------
421         msg := tc.WaitMsg(15)
422         if msg != nil {
423                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
424                         tc.TestError(t, "Received RIC_SUB_RESP wrong mtype expected %s got %s, error", "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
425                         return 0
426                 } else if msg.Xid != trans.xid {
427                         tc.TestError(t, "Received RIC_SUB_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
428                         return 0
429                 } else {
430                         packedData := &e2ap.PackedData{}
431                         packedData.Buf = msg.Payload
432                         if msg.SubId > 0 {
433                                 e2SubsId = uint32(msg.SubId)
434                         } else {
435                                 e2SubsId = 0
436                         }
437                         unpackerr, resp := e2SubsResp.UnPack(packedData)
438                         if unpackerr != nil {
439                                 tc.TestError(t, "RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
440                         }
441                         tc.Debug("Recv Subs Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
442                         return e2SubsId
443                 }
444         } else {
445                 tc.TestError(t, "Not Received msg within %d secs", 15)
446         }
447         return 0
448 }
449
450 //-----------------------------------------------------------------------------
451 //
452 //-----------------------------------------------------------------------------
453
454 func (tc *E2Stub) SendSubsFail(t *testing.T, fparams *E2StubSubsFailParams, msg *xapp.RMRParams) {
455         tc.Debug("SendSubsFail")
456         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
457
458         //---------------------------------
459         // e2term activity: Send Subs Fail
460         //---------------------------------
461         packerr, packedMsg := e2SubsFail.Pack(fparams.Fail)
462         if packerr != nil {
463                 tc.TestError(t, "pack NOK %s", packerr.Error())
464         }
465         tc.Debug("%s", e2SubsFail.String())
466
467         params := &xapp.RMRParams{}
468         params.Mtype = xapp.RIC_SUB_FAILURE
469         params.SubId = msg.SubId
470         params.Payload = packedMsg.Buf
471         params.PayloadLen = len(packedMsg.Buf)
472         params.Meid = msg.Meid
473         params.Xid = msg.Xid
474         params.Mbuf = nil
475
476         tc.Debug("SEND SUB FAIL: %s", params.String())
477         snderr := tc.SendWithRetry(params, false, 5)
478         if snderr != nil {
479                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
480         }
481 }
482
483 //-----------------------------------------------------------------------------
484 //
485 //-----------------------------------------------------------------------------
486 func (tc *E2Stub) RecvSubsFail(t *testing.T, trans *RmrTransactionId) uint32 {
487         tc.Debug("RecvSubsFail")
488         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
489         var e2SubsId uint32
490
491         //-------------------------------
492         // xapp activity: Recv Subs Fail
493         //-------------------------------
494         msg := tc.WaitMsg(15)
495         if msg != nil {
496                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
497                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
498                         return 0
499                 } else if msg.Xid != trans.xid {
500                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", trans.xid, msg.Xid)
501                         return 0
502                 } else {
503                         packedData := &e2ap.PackedData{}
504                         packedData.Buf = msg.Payload
505                         if msg.SubId > 0 {
506                                 e2SubsId = uint32(msg.SubId)
507                         } else {
508                                 e2SubsId = 0
509                         }
510                         unpackerr, resp := e2SubsFail.UnPack(packedData)
511                         if unpackerr != nil {
512                                 tc.TestError(t, "RIC_SUB_FAILURE unpack failed err: %s", unpackerr.Error())
513                         }
514                         tc.Debug("Recv Subs Fail rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
515                         return e2SubsId
516                 }
517         } else {
518                 tc.TestError(t, "Not Received msg within %d secs", 15)
519         }
520         return 0
521 }
522
523 //-----------------------------------------------------------------------------
524 //
525 //-----------------------------------------------------------------------------
526 func (tc *E2Stub) SendSubsDelReq(t *testing.T, oldTrans *RmrTransactionId, e2SubsId uint32) *RmrTransactionId {
527
528         trans := oldTrans
529         if oldTrans == nil {
530                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
531         }
532
533         tc.Debug("SendSubsDelReq %s", trans.String())
534         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
535         //---------------------------------
536         // xapp activity: Send Subs Del Req
537         //---------------------------------
538         req := &e2ap.E2APSubscriptionDeleteRequest{}
539         req.RequestId.Id = 1
540         req.RequestId.InstanceId = e2SubsId
541         req.FunctionId = 1
542
543         err, packedMsg := e2SubsDelReq.Pack(req)
544         if err != nil {
545                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
546                 return nil
547         }
548         tc.Debug("%s %s", trans.String(), e2SubsDelReq.String())
549
550         params := &xapp.RMRParams{}
551         params.Mtype = xapp.RIC_SUB_DEL_REQ
552         params.SubId = int(e2SubsId)
553         params.Payload = packedMsg.Buf
554         params.PayloadLen = len(packedMsg.Buf)
555         params.Meid = trans.meid
556         params.Xid = trans.xid
557         params.Mbuf = nil
558
559         tc.Debug("SEND SUB DEL REQ: %s", params.String())
560         snderr := tc.SendWithRetry(params, false, 5)
561         if snderr != nil {
562                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
563                 return nil
564         }
565         return trans
566 }
567
568 //-----------------------------------------------------------------------------
569 //
570 //-----------------------------------------------------------------------------
571 func (tc *E2Stub) RecvSubsDelReq(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *xapp.RMRParams) {
572         tc.Debug("RecvSubsDelReq")
573         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
574
575         //---------------------------------
576         // e2term activity: Recv Subs Del Req
577         //---------------------------------
578         msg := tc.WaitMsg(15)
579         if msg != nil {
580                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
581                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
582                 } else {
583                         tc.Debug("Recv Subs Del Req")
584
585                         packedData := &e2ap.PackedData{}
586                         packedData.Buf = msg.Payload
587                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
588                         if unpackerr != nil {
589                                 tc.TestError(t, "RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
590                         }
591                         return req, msg
592                 }
593         } else {
594                 tc.TestError(t, "Not Received msg within %d secs", 15)
595         }
596         return nil, nil
597 }
598
599 //-----------------------------------------------------------------------------
600 //
601 //-----------------------------------------------------------------------------
602 func (tc *E2Stub) SendSubsDelResp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
603         tc.Debug("SendSubsDelResp")
604         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
605
606         //---------------------------------
607         // e2term activity: Send Subs Del Resp
608         //---------------------------------
609         resp := &e2ap.E2APSubscriptionDeleteResponse{}
610         resp.RequestId.Id = req.RequestId.Id
611         resp.RequestId.InstanceId = req.RequestId.InstanceId
612         resp.FunctionId = req.FunctionId
613
614         packerr, packedMsg := e2SubsDelResp.Pack(resp)
615         if packerr != nil {
616                 tc.TestError(t, "pack NOK %s", packerr.Error())
617         }
618         tc.Debug("%s", e2SubsDelResp.String())
619
620         params := &xapp.RMRParams{}
621         params.Mtype = xapp.RIC_SUB_DEL_RESP
622         params.SubId = msg.SubId
623         params.Payload = packedMsg.Buf
624         params.PayloadLen = len(packedMsg.Buf)
625         params.Meid = msg.Meid
626         params.Xid = msg.Xid
627         params.Mbuf = nil
628
629         tc.Debug("SEND SUB DEL RESP: %s", params.String())
630         snderr := tc.SendWithRetry(params, false, 5)
631         if snderr != nil {
632                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
633         }
634 }
635
636 //-----------------------------------------------------------------------------
637 //
638 //-----------------------------------------------------------------------------
639 func (tc *E2Stub) RecvSubsDelResp(t *testing.T, trans *RmrTransactionId) {
640         tc.Debug("RecvSubsDelResp")
641         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
642
643         //---------------------------------
644         // xapp activity: Recv Subs Del Resp
645         //---------------------------------
646         msg := tc.WaitMsg(15)
647         if msg != nil {
648                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
649                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype])
650                         return
651                 } else if trans != nil && msg.Xid != trans.xid {
652                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
653                         return
654                 } else {
655                         packedData := &e2ap.PackedData{}
656                         packedData.Buf = msg.Payload
657                         unpackerr, resp := e2SubsDelResp.UnPack(packedData)
658                         if unpackerr != nil {
659                                 tc.TestError(t, "RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
660                         }
661                         tc.Debug("Recv Subs Del Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
662                         return
663                 }
664         } else {
665                 tc.TestError(t, "Not Received msg within %d secs", 15)
666         }
667 }
668
669 //-----------------------------------------------------------------------------
670 //
671 //-----------------------------------------------------------------------------
672 func (tc *E2Stub) SendSubsDelFail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
673         tc.Debug("SendSubsDelFail")
674         e2SubsDelFail := e2asnpacker.NewPackerSubscriptionDeleteFailure()
675
676         //---------------------------------
677         // e2term activity: Send Subs Del Fail
678         //---------------------------------
679         resp := &e2ap.E2APSubscriptionDeleteFailure{}
680         resp.RequestId.Id = req.RequestId.Id
681         resp.RequestId.InstanceId = req.RequestId.InstanceId
682         resp.FunctionId = req.FunctionId
683         resp.Cause.Content = 5 // CauseMisc
684         resp.Cause.Value = 3   // unspecified
685
686         packerr, packedMsg := e2SubsDelFail.Pack(resp)
687         if packerr != nil {
688                 tc.TestError(t, "pack NOK %s", packerr.Error())
689         }
690         tc.Debug("%s", e2SubsDelFail.String())
691
692         params := &xapp.RMRParams{}
693         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
694         params.SubId = msg.SubId
695         params.Payload = packedMsg.Buf
696         params.PayloadLen = len(packedMsg.Buf)
697         params.Meid = msg.Meid
698         params.Xid = msg.Xid
699         params.Mbuf = nil
700
701         tc.Debug("SEND SUB DEL FAIL: %s", params.String())
702         snderr := tc.SendWithRetry(params, false, 5)
703         if snderr != nil {
704                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
705         }
706 }
707
708 // REST code below all
709
710 /*****************************************************************************/
711 // REST interface specific functions are below
712
713 //-----------------------------------------------------------------------------
714 // Callback handler for subscription response notifications
715 //-----------------------------------------------------------------------------
716 func (tc *E2Stub) SubscriptionRespHandler(resp *clientmodel.SubscriptionResponse) {
717
718         if tc.subscriptionId == "SUBSCRIPTIONID NOT SET" {
719                 tc.Debug("REST notification received for %v while no SubscriptionID was not set for E2EventInstanceID=%v, XappEventInstanceID=%v",
720                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
721                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
722         } else if tc.subscriptionId == *resp.SubscriptionID {
723                 tc.Debug("REST notification received SubscriptionID=%s, E2EventInstanceID=%v, RequestorID=%v",
724                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
725                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
726         } else {
727                 tc.Debug("MISMATCHING REST notification received SubscriptionID=%s<>%s, E2EventInstanceID=%v, XappEventInstanceID=%v",
728                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
729         }
730 }
731
732 //-----------------------------------------------------------------------------
733 //
734 //-----------------------------------------------------------------------------
735
736 func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
737         tc.expectNotification(t, restSubsId, "")
738 }
739
740 func (tc *E2Stub) ExpectRESTNotificationOk(t *testing.T, restSubsId string) {
741         tc.expectNotification(t, restSubsId, "allOk")
742 }
743
744 func (tc *E2Stub) ExpectRESTNotificationNok(t *testing.T, restSubsId string, expectError string) {
745         tc.expectNotification(t, restSubsId, expectError)
746 }
747
748 func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectError string) {
749
750         tc.Debug("### Started to wait REST notification for restSubsId= %v, RMRPort=%v, requestCount=%v responses expected", restSubsId, *tc.clientEndpoint.RMRPort, tc.requestCount)
751         tc.restSubsIdList = []string{restSubsId}
752         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
753         if tc.requestCount == 0 {
754                 tc.TestError(t, "### NO REST notifications SET received for restSubsId= %v, RMRPort=%v, request count ZERO!", restSubsId, *tc.clientEndpoint.RMRPort)
755         }
756         go func() {
757                 select {
758                 case e2Ids := <-tc.CallBackListedNotifications:
759                         if tc.requestCount == 0 {
760                                 tc.TestError(t, "### REST notification count unexpectedly ZERO for restSubsId= %v", restSubsId)
761                         } else if e2Ids.RestSubsId != restSubsId {
762                                 tc.TestError(t, "### Unexpected REST notifications received, expected %s but got %s instead", e2Ids.RestSubsId, restSubsId)
763                         } else if e2Ids.ErrorCause == "" && expectError == "allFail" {
764                                 tc.TestError(t, "### Unexpected ok cause received from REST notifications |%s:%s|", e2Ids.RestSubsId, restSubsId)
765                         } else if e2Ids.ErrorCause != "" && expectError == "allOk" {
766                                 tc.TestError(t, "### Unexpected ErrorCause: (%s), ErrorSource: (%s), TimeoutType: (%s) received from REST notifications |%s:%s|", e2Ids.ErrorCause, e2Ids.ErrorSource, e2Ids.TimeoutType, e2Ids.RestSubsId, restSubsId)
767                         } else {
768                                 tc.requestCount--
769                                 if tc.requestCount == 0 {
770                                         tc.Debug("### All expected REST notifications received for restSubsId= %v)", e2Ids.RestSubsId)
771                                 } else {
772                                         tc.Debug("### Expected REST notifications received for restSubsId= %v", e2Ids.RestSubsId)
773                                 }
774                                 if e2Ids.ErrorCause != "" && expectError == "allFail" {
775                                         tc.Debug("### REST Notification: RestSubsId: %s, ErrorCause: %s, ErrorSource: (%s), TimeoutType: (%s)", e2Ids.RestSubsId, e2Ids.ErrorCause, e2Ids.ErrorSource, e2Ids.TimeoutType)
776                                 }
777                                 tc.Debug("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
778                                 tc.ListedRESTNotifications <- e2Ids
779                                 if len(tc.ListedRESTNotifications) > 1 {
780                                         panic("expectNotification - ListedRESTNotifications stacking up")
781                                 }
782                         }
783                 case <-time.After(15 * time.Second):
784                         err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
785                         tc.TestError(t, "%s", err.Error())
786                 }
787         }()
788 }
789
790 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
791
792         stack := string(debug.Stack())
793
794         select {
795         case e2SubsId := <-tc.ListedRESTNotifications:
796                 if e2SubsId.RestSubsId == restSubsId {
797                         tc.Debug("### Expected REST notifications received %s, e2SubsId %v", e2SubsId.RestSubsId, e2SubsId.E2SubsId)
798                         return e2SubsId.E2SubsId
799                 } else {
800                         tc.TestError(t, "### Unexpected REST notification %s received, expected %v", e2SubsId.RestSubsId, restSubsId)
801                         xapp.Logger.Debug("CALL STACK:\n %s", stack)
802                         return 0
803                 }
804         case <-time.After(15 * time.Second):
805                 err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
806                 tc.TestError(t, "%s", err.Error())
807                 xapp.Logger.Debug("CALL STACK:\n %s", stack)
808                 panic("WaitRESTNotification - timeout error")
809         }
810 }
811
812 // Note, this function should be followed by a handling of <-xappConn1.RESTNotification.
813 func (tc *E2Stub) ExpectAnyNotification(t *testing.T) {
814         go func() {
815                 tc.Debug("### Started waiting ANY REST notifications received for RMRPort=%v", *tc.clientEndpoint.RMRPort)
816                 select {
817                 case e2SubsId := <-tc.CallBackNotification:
818                         tc.Debug("### ANY REST notifications received for e2SubsId %v RMRPort=%v", e2SubsId, *tc.clientEndpoint.RMRPort)
819                         tc.RESTNotification <- (uint32)(e2SubsId)
820                 case <-time.After(15 * time.Second):
821                         err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
822                         tc.TestError(t, "%s", err.Error())
823                         tc.RESTNotification <- 0
824                 }
825         }()
826 }
827
828 func (tc *E2Stub) WaitAnyRESTNotification(t *testing.T) uint32 {
829
830         select {
831         case e2SubsId := <-tc.RESTNotification:
832                 tc.Debug("### Expected ANY REST notification received for e2SubsId %v", e2SubsId)
833                 return e2SubsId
834
835         case <-time.After(15 * time.Second):
836                 err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
837                 tc.TestError(t, "%s", err.Error())
838                 panic("WaitRESTNotification - timeout error")
839         }
840 }
841
842 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
843
844         if len(tc.restSubsIdList) == 0 {
845                 tc.Error("Unexpected listed REST notifications received for SubscriptionID=%s, expected restSubsId list size was ZERO!", *resp.SubscriptionID)
846         } else {
847                 for i, subsId := range tc.restSubsIdList {
848                         if *resp.SubscriptionID == subsId {
849                                 //tc.Debug("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, XappEventInstanceID=%v",
850                                 //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
851
852                                 tc.restSubsIdList = append(tc.restSubsIdList[:i], tc.restSubsIdList[i+1:]...)
853                                 //tc.Debug("Removed %s from Listed REST notifications, %v entries left", *resp.SubscriptionID, len(tc.restSubsIdList))
854
855                                 //                              if resp.SubscriptionInstances[0].ErrorCause != nil {
856                                 tc.CallBackListedNotifications <- *tc.GetE2RestIds(resp)
857                                 //                              } else {
858                                 //                                      tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].E2EventInstanceID), "", "", ""}
859                                 //                              }
860
861                                 if len(tc.restSubsIdList) == 0 {
862                                         tc.Debug("All listed REST notifications received for SubscriptionID=%s", *resp.SubscriptionID)
863                                 }
864
865                                 return
866                         }
867                 }
868                 tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, XappEventInstanceID=%v",
869                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
870         }
871 }
872
873 //-----------------------------------------------------------------------------
874 //
875 //-----------------------------------------------------------------------------
876 func (tc *E2Stub) GetE2RestIds(resp *clientmodel.SubscriptionResponse) *E2RestIds {
877
878         e2RestIds := &E2RestIds{}
879         if resp != nil {
880                 if resp.SubscriptionID != nil {
881                         e2RestIds.RestSubsId = *resp.SubscriptionID
882                 }
883                 if resp.SubscriptionInstances != nil {
884                         if resp.SubscriptionInstances[0].E2EventInstanceID != nil {
885                                 e2RestIds.E2SubsId = uint32(*resp.SubscriptionInstances[0].E2EventInstanceID)
886                         }
887                         e2RestIds.ErrorCause = resp.SubscriptionInstances[0].ErrorCause
888                         e2RestIds.ErrorSource = resp.SubscriptionInstances[0].ErrorSource
889                         e2RestIds.TimeoutType = resp.SubscriptionInstances[0].TimeoutType
890                 }
891         }
892         return e2RestIds
893 }
894
895 //-----------------------------------------------------------------------------
896 //
897 //-----------------------------------------------------------------------------
898 func (tc *E2Stub) WaitListedRestNotifications(t *testing.T, restSubsIds []string) {
899         tc.Debug("Started to wait REST notifications for restSubsIds=%v, RMRPort=%v", restSubsIds, *tc.clientEndpoint.RMRPort)
900
901         tc.restSubsIdList = restSubsIds
902         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
903
904         for i := 0; i < len(restSubsIds); i++ {
905                 go func() {
906                         select {
907                         case e2Ids := <-tc.CallBackListedNotifications:
908                                 tc.Debug("Listed Notification waiter received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
909                                 tc.ListedRESTNotifications <- e2Ids
910                         case <-time.After(15 * time.Second):
911                                 err := fmt.Errorf("Timeout 15s expired while waiting Listed REST notification")
912                                 tc.TestError(t, "%s", err.Error())
913                                 tc.RESTNotification <- 0
914                         }
915                 }()
916         }
917 }
918
919 //-----------------------------------------------------------------------------
920 //
921 //-----------------------------------------------------------------------------
922 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string {
923         tc.Debug("======== Posting REST subscriptions to Submgr ======")
924
925         if params == nil {
926                 tc.Debug("SendRESTReportSubsReq: params == nil")
927                 return ""
928         }
929
930         tc.subscriptionId = "SUBSCRIPTIONID NOT SET"
931
932         resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
933         if err != nil {
934                 // Swagger generated code makes checks for the values that are inserted the subscription function
935                 // If error cause is unknown and POST is not done, the problem is in the inserted values
936                 tc.Error("======== REST subscription request failed %s ========", err.Error())
937                 return ""
938         }
939         tc.subscriptionId = *resp.SubscriptionID
940         tc.Debug("======== REST subscriptions posted successfully. SubscriptionID = %s, RequestCount = %v ========", *resp.SubscriptionID, tc.requestCount)
941         return *resp.SubscriptionID
942 }
943
944 //-----------------------------------------------------------------------------
945 //
946 //-----------------------------------------------------------------------------
947 func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int) *RESTSubsReqParams {
948
949         reportParams := RESTSubsReqParams{}
950         reportParams.GetRESTSubsReqReportParams(subReqCount, &tc.clientEndpoint, &tc.meid)
951         tc.requestCount = subReqCount
952         return &reportParams
953 }
954
955 //-----------------------------------------------------------------------------
956 //
957 //-----------------------------------------------------------------------------
958 type RESTSubsReqParams struct {
959         SubsReqParams clientmodel.SubscriptionParams
960 }
961
962 func (p *RESTSubsReqParams) GetRESTSubsReqReportParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
963
964         // E2SM-gNB-X2
965         p.SubsReqParams.ClientEndpoint = clientEndpoint
966         p.SubsReqParams.Meid = meid
967         var rANFunctionID int64 = 33
968         p.SubsReqParams.RANFunctionID = &rANFunctionID
969
970         actionId := int64(1)
971         actionType := "report"
972         subsequestActioType := "continue"
973         timeToWait := "w10ms"
974
975         for requestCount := 0; requestCount < subReqCount; requestCount++ {
976                 reqId := int64(requestCount) + 1
977                 subscriptionDetail := &clientmodel.SubscriptionDetail{
978                         XappEventInstanceID: &reqId,
979                         EventTriggers: clientmodel.EventTriggerDefinition{
980                                 int64(1234 + requestCount),
981                         },
982                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
983                                 &clientmodel.ActionToBeSetup{
984                                         ActionID:   &actionId,
985                                         ActionType: &actionType,
986                                         ActionDefinition: clientmodel.ActionDefinition{
987                                                 int64(5678 + requestCount),
988                                         },
989                                         SubsequentAction: &clientmodel.SubsequentAction{
990                                                 SubsequentActionType: &subsequestActioType,
991                                                 TimeToWait:           &timeToWait,
992                                         },
993                                 },
994                         },
995                 }
996                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
997         }
998
999 }
1000
1001 func (p *RESTSubsReqParams) SetMeid(MEID string) {
1002         p.SubsReqParams.Meid = &MEID
1003 }
1004
1005 func (p *RESTSubsReqParams) SetEndpoint(HTTP_port int64, RMR_port int64, host string) {
1006         var endpoint clientmodel.SubscriptionParamsClientEndpoint
1007         endpoint.HTTPPort = &HTTP_port
1008         endpoint.RMRPort = &RMR_port
1009         endpoint.Host = host
1010         p.SubsReqParams.ClientEndpoint = &endpoint
1011 }
1012
1013 func (p *RESTSubsReqParams) SetEndpointHost(host string) {
1014
1015         if p.SubsReqParams.ClientEndpoint.Host != "" {
1016                 if p.SubsReqParams.ClientEndpoint.Host != host {
1017                         // Renaming toto, print something tc.Debug("Posting REST subscription request to Submgr")
1018                         err := fmt.Errorf("hostname change attempt: %s -> %s", p.SubsReqParams.ClientEndpoint.Host, host)
1019                         panic(err)
1020                 }
1021         }
1022         p.SubsReqParams.ClientEndpoint.Host = host
1023 }
1024
1025 func (p *RESTSubsReqParams) SetHTTPEndpoint(HTTP_port int64, host string) {
1026
1027         p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1028
1029         p.SetEndpointHost(host)
1030
1031         if p.SubsReqParams.ClientEndpoint.RMRPort == nil {
1032                 var RMR_port int64
1033                 p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1034         }
1035 }
1036
1037 func (p *RESTSubsReqParams) SetE2SubscriptionDirectives(E2RetryCount int64, E2TimeoutTimerValue int64, RMRRoutingNeeded bool) {
1038
1039         E2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1040         p.SubsReqParams.E2SubscriptionDirectives = E2SubscriptionDirectives
1041         p.SubsReqParams.E2SubscriptionDirectives.E2RetryCount = &E2RetryCount
1042         p.SubsReqParams.E2SubscriptionDirectives.E2TimeoutTimerValue = E2TimeoutTimerValue
1043         p.SubsReqParams.E2SubscriptionDirectives.RMRRoutingNeeded = RMRRoutingNeeded
1044 }
1045
1046 func (p *RESTSubsReqParams) RemoveE2SubscriptionDirectives() {
1047
1048         p.SubsReqParams.E2SubscriptionDirectives = nil
1049 }
1050
1051 func (p *RESTSubsReqParams) SetSubActionTypes(actionType string) {
1052
1053         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1054                 for _, action := range subDetail.ActionToBeSetupList {
1055                         if action != nil {
1056                                 action.ActionType = &actionType
1057                         }
1058                 }
1059         }
1060 }
1061
1062 func (p *RESTSubsReqParams) SetSubActionIDs(actionId int64) {
1063
1064         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1065                 for _, action := range subDetail.ActionToBeSetupList {
1066                         if action != nil {
1067                                 action.ActionID = &actionId
1068                         }
1069                 }
1070         }
1071 }
1072
1073 func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition []int64) {
1074
1075         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1076                 for _, action := range subDetail.ActionToBeSetupList {
1077                         if action != nil {
1078                                 action.ActionDefinition = actionDefinition
1079                         }
1080                 }
1081         }
1082 }
1083
1084 func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition []int64) {
1085
1086         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1087                 if subDetail != nil {
1088                         subDetail.EventTriggers = eventTriggerDefinition
1089                 }
1090         }
1091 }
1092
1093 func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition []int64, subsequentActionType string, timeToWait string) {
1094
1095         actionToBeSetup := &clientmodel.ActionToBeSetup{
1096                 ActionID:         &actionId,
1097                 ActionType:       &actionType,
1098                 ActionDefinition: clientmodel.ActionDefinition(actionDefinition),
1099                 SubsequentAction: &clientmodel.SubsequentAction{
1100                         SubsequentActionType: &subsequentActionType,
1101                         TimeToWait:           &timeToWait,
1102                 },
1103         }
1104
1105         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1106                 if subDetail != nil {
1107                         subDetail.ActionToBeSetupList = append(subDetail.ActionToBeSetupList, actionToBeSetup)
1108                 }
1109         }
1110 }
1111
1112 func (p *RESTSubsReqParams) SetRMREndpoint(RMR_port int64, host string) {
1113
1114         p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1115
1116         p.SetEndpointHost(host)
1117
1118         if p.SubsReqParams.ClientEndpoint.HTTPPort == nil {
1119                 var HTTP_port int64
1120                 p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1121         }
1122 }
1123
1124 func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
1125
1126         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1127                 for _, action := range subDetail.ActionToBeSetupList {
1128                         if action != nil && action.SubsequentAction != nil {
1129                                 action.SubsequentAction.TimeToWait = &timeToWait
1130                         }
1131                 }
1132         }
1133 }
1134
1135 func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
1136
1137         if *SubscriptionID == "" {
1138                 return
1139         }
1140         p.SubsReqParams.SubscriptionID = *SubscriptionID
1141 }
1142
1143 func (p *RESTSubsReqParams) SetXappEventInstanceID(xappEventInstanceId int64) {
1144
1145         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1146                 if subDetail != nil {
1147                         subDetail.XappEventInstanceID = &xappEventInstanceId
1148                 }
1149         }
1150 }
1151
1152 //-----------------------------------------------------------------------------
1153 //
1154 //-----------------------------------------------------------------------------
1155 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
1156
1157         tc.Debug("======== Posting REST DELETE subscription(s) to Submgr ======")
1158
1159         if *subscriptionID == "" {
1160                 tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
1161         }
1162         tc.Debug("REST deleting E2 subscriptions. SubscriptionID = %s", *subscriptionID)
1163
1164         err := xapp.Subscription.Unsubscribe(*subscriptionID)
1165         if err != nil {
1166                 tc.Error("REST Delete subscription failed %s", err.Error())
1167         }
1168         tc.Debug("REST delete subscription pushed to submgr successfully. SubscriptionID = %s", *subscriptionID)
1169 }
1170
1171 //-----------------------------------------------------------------------------
1172 //
1173 //-----------------------------------------------------------------------------
1174 func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams {
1175
1176         policyParams := RESTSubsReqParams{}
1177         policyParams.GetRESTSubsReqPolicyParams(subReqCount, &tc.clientEndpoint, &tc.meid)
1178         tc.requestCount = subReqCount
1179         return &policyParams
1180 }
1181
1182 //-----------------------------------------------------------------------------
1183 //
1184 //-----------------------------------------------------------------------------
1185 func (tc *E2Stub) DecrementRequestCount() {
1186         if tc.requestCount > 0 {
1187                 tc.requestCount -= 1
1188         } else {
1189                 tc.Error("FAILED to decrement request count, count already ZERO")
1190         }
1191 }
1192
1193 //-----------------------------------------------------------------------------
1194 //
1195 //-----------------------------------------------------------------------------
1196 func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
1197
1198         p.SubsReqParams.ClientEndpoint = clientEndpoint
1199         p.SubsReqParams.Meid = meid
1200         var rANFunctionID int64 = 33
1201         p.SubsReqParams.RANFunctionID = &rANFunctionID
1202         actionId := int64(1)
1203         actionType := "policy"
1204         subsequestActioType := "continue"
1205         timeToWait := "w10ms"
1206
1207         for requestCount := 0; requestCount < subReqCount; requestCount++ {
1208                 reqId := int64(requestCount) + 1
1209                 subscriptionDetail := &clientmodel.SubscriptionDetail{
1210                         XappEventInstanceID: &reqId,
1211                         EventTriggers: clientmodel.EventTriggerDefinition{
1212                                 int64(1234 + requestCount),
1213                         },
1214                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
1215                                 &clientmodel.ActionToBeSetup{
1216                                         ActionID:   &actionId,
1217                                         ActionType: &actionType,
1218                                         ActionDefinition: clientmodel.ActionDefinition{
1219                                                 int64(5678 + requestCount),
1220                                         },
1221                                         SubsequentAction: &clientmodel.SubsequentAction{
1222                                                 SubsequentActionType: &subsequestActioType,
1223                                                 TimeToWait:           &timeToWait,
1224                                         },
1225                                 },
1226                         },
1227                 }
1228                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1229         }
1230 }
1231
1232 func (p *RESTSubsReqParams) SetSubscriptionDirectives(e2Timeout int64, e2RetryCount int64, routingNeeded bool) {
1233
1234         e2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1235         e2SubscriptionDirectives.E2TimeoutTimerValue = e2Timeout
1236         e2SubscriptionDirectives.E2RetryCount = &e2RetryCount
1237         e2SubscriptionDirectives.RMRRoutingNeeded = routingNeeded
1238         p.SubsReqParams.E2SubscriptionDirectives = e2SubscriptionDirectives
1239
1240 }