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