a021cd06790679b1fc5206852cfcd6c41e102bcc
[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                         if resp.SubscriptionInstances[0].ErrorCause != nil {
842                                 e2RestIds.ErrorCause = *resp.SubscriptionInstances[0].ErrorCause
843                         } else {
844                                 e2RestIds.ErrorCause = "nil"
845                         }
846                         if resp.SubscriptionInstances[0].ErrorSource != nil {
847                                 e2RestIds.ErrorSource = *resp.SubscriptionInstances[0].ErrorSource
848                         } else {
849                                 e2RestIds.ErrorSource = "nil"
850                         }
851                         if resp.SubscriptionInstances[0].TimeoutType != nil {
852                                 e2RestIds.TimeoutType = *resp.SubscriptionInstances[0].TimeoutType
853                         } else {
854                                 e2RestIds.TimeoutType = "nil"
855                         }
856                 }
857         }
858         return e2RestIds
859 }
860
861 //-----------------------------------------------------------------------------
862 //
863 //-----------------------------------------------------------------------------
864 func (tc *E2Stub) WaitListedRestNotifications(t *testing.T, restSubsIds []string) {
865         tc.Debug("Started to wait REST notifications %v on port %v f(RMR port %v)", restSubsIds, *tc.clientEndpoint.HTTPPort, *tc.clientEndpoint.RMRPort)
866
867         tc.restSubsIdList = restSubsIds
868         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
869
870         for i := 0; i < len(restSubsIds); i++ {
871                 go func() {
872                         select {
873                         case e2Ids := <-tc.CallBackListedNotifications:
874                                 tc.Debug("Listed Notification waiter received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
875                                 tc.ListedRESTNotifications <- e2Ids
876                         case <-time.After(15 * time.Second):
877                                 err := fmt.Errorf("Timeout 15s expired while waiting Listed REST notification")
878                                 tc.TestError(t, "%s", err.Error())
879                                 tc.RESTNotification <- 0
880                         }
881                 }()
882         }
883 }
884
885 //-----------------------------------------------------------------------------
886 //
887 //-----------------------------------------------------------------------------
888 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string {
889         tc.Debug("======== Posting REST subscriptions to Submgr ======")
890
891         if params == nil {
892                 tc.Debug("SendRESTReportSubsReq: params == nil")
893                 return ""
894         }
895
896         tc.subscriptionId = "SUBSCRIPTIONID NOT SET"
897
898         resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
899         if err != nil {
900                 // Swagger generated code makes checks for the values that are inserted the subscription function
901                 // If error cause is unknown and POST is not done, the problem is in the inserted values
902                 tc.Error("======== REST subscription request failed %s ========", err.Error())
903                 if resp != nil {
904                         tc.PrintSubscriptionInsctanceErrorInfo(resp)
905                 } else {
906                         tc.Error("Subscribe resp == nil")
907                 }
908                 return ""
909         }
910         tc.subscriptionId = *resp.SubscriptionID
911         tc.Debug("======== REST subscriptions posted successfully. SubscriptionID = %s, RequestCount = %v ========", *resp.SubscriptionID, tc.requestCount)
912         return *resp.SubscriptionID
913 }
914
915 //-----------------------------------------------------------------------------
916 //
917 //-----------------------------------------------------------------------------
918 func (tc *E2Stub) PrintSubscriptionInsctanceErrorInfo(resp *clientmodel.SubscriptionResponse) {
919         for _, subscriptionInstance := range resp.SubscriptionInstances {
920                 if subscriptionInstance != nil {
921                         if subscriptionInstance.RejectCause != nil {
922                                 tc.Error("subscriptionInstance.RejectCause= %s", *subscriptionInstance.RejectCause)
923                         }
924                         if subscriptionInstance.ErrorCause != nil {
925                                 tc.Error("subscriptionInstance.ErrorCause= %s", *subscriptionInstance.ErrorCause)
926                         }
927                         if subscriptionInstance.ErrorSource != nil {
928                                 tc.Error("subscriptionInstance.ErrorSource= %s", *subscriptionInstance.ErrorSource)
929                         }
930                         if subscriptionInstance.TimeoutType != nil {
931                                 tc.Error("subscriptionInstance.TimeoutType = %s", *subscriptionInstance.TimeoutType)
932                         }
933                 }
934         }
935 }
936
937 //-----------------------------------------------------------------------------
938 //
939 //-----------------------------------------------------------------------------
940 func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int) *RESTSubsReqParams {
941
942         reportParams := RESTSubsReqParams{}
943         reportParams.GetRESTSubsReqReportParams(subReqCount, &tc.clientEndpoint, &tc.meid)
944         tc.requestCount = subReqCount
945         return &reportParams
946 }
947
948 //-----------------------------------------------------------------------------
949 //
950 //-----------------------------------------------------------------------------
951 type RESTSubsReqParams struct {
952         SubsReqParams clientmodel.SubscriptionParams
953 }
954
955 func (p *RESTSubsReqParams) GetRESTSubsReqReportParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
956
957         // E2SM-gNB-X2
958         p.SubsReqParams.ClientEndpoint = clientEndpoint
959         p.SubsReqParams.Meid = meid
960         var rANFunctionID int64 = 33
961         p.SubsReqParams.RANFunctionID = &rANFunctionID
962
963         actionId := int64(1)
964         actionType := "report"
965         subsequestActioType := "continue"
966         timeToWait := "w10ms"
967
968         for requestCount := 0; requestCount < subReqCount; requestCount++ {
969                 reqId := int64(requestCount) + 1
970                 subscriptionDetail := &clientmodel.SubscriptionDetail{
971                         XappEventInstanceID: &reqId,
972                         EventTriggers: clientmodel.EventTriggerDefinition{
973                                 int64(1234 + requestCount),
974                         },
975                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
976                                 &clientmodel.ActionToBeSetup{
977                                         ActionID:   &actionId,
978                                         ActionType: &actionType,
979                                         ActionDefinition: clientmodel.ActionDefinition{
980                                                 int64(5678 + requestCount),
981                                         },
982                                         SubsequentAction: &clientmodel.SubsequentAction{
983                                                 SubsequentActionType: &subsequestActioType,
984                                                 TimeToWait:           &timeToWait,
985                                         },
986                                 },
987                         },
988                 }
989                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
990         }
991
992 }
993
994 func (p *RESTSubsReqParams) SetMeid(MEID string) {
995         p.SubsReqParams.Meid = &MEID
996 }
997
998 func (p *RESTSubsReqParams) SetEndpoint(HTTP_port int64, RMR_port int64, host string) {
999         var endpoint clientmodel.SubscriptionParamsClientEndpoint
1000         endpoint.HTTPPort = &HTTP_port
1001         endpoint.RMRPort = &RMR_port
1002         endpoint.Host = host
1003         p.SubsReqParams.ClientEndpoint = &endpoint
1004 }
1005
1006 func (p *RESTSubsReqParams) SetEndpointHost(host string) {
1007
1008         if p.SubsReqParams.ClientEndpoint.Host != "" {
1009                 if p.SubsReqParams.ClientEndpoint.Host != host {
1010                         // Renaming toto, print something tc.Debug("Posting REST subscription request to Submgr")
1011                         err := fmt.Errorf("hostname change attempt: %s -> %s", p.SubsReqParams.ClientEndpoint.Host, host)
1012                         panic(err)
1013                 }
1014         }
1015         p.SubsReqParams.ClientEndpoint.Host = host
1016 }
1017
1018 func (p *RESTSubsReqParams) SetHTTPEndpoint(HTTP_port int64, host string) {
1019
1020         p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1021
1022         p.SetEndpointHost(host)
1023
1024         if p.SubsReqParams.ClientEndpoint.RMRPort == nil {
1025                 var RMR_port int64
1026                 p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1027         }
1028 }
1029
1030 func (p *RESTSubsReqParams) SetE2SubscriptionDirectives(E2RetryCount int64, E2TimeoutTimerValue int64, RMRRoutingNeeded bool) {
1031
1032         E2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1033         p.SubsReqParams.E2SubscriptionDirectives = E2SubscriptionDirectives
1034         p.SubsReqParams.E2SubscriptionDirectives.E2RetryCount = &E2RetryCount
1035         p.SubsReqParams.E2SubscriptionDirectives.E2TimeoutTimerValue = E2TimeoutTimerValue
1036         p.SubsReqParams.E2SubscriptionDirectives.RMRRoutingNeeded = &RMRRoutingNeeded
1037 }
1038
1039 func (p *RESTSubsReqParams) RemoveE2SubscriptionDirectives() {
1040
1041         p.SubsReqParams.E2SubscriptionDirectives = nil
1042 }
1043
1044 func (p *RESTSubsReqParams) SetSubActionTypes(actionType string) {
1045
1046         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1047                 for _, action := range subDetail.ActionToBeSetupList {
1048                         if action != nil {
1049                                 action.ActionType = &actionType
1050                         }
1051                 }
1052         }
1053 }
1054
1055 func (p *RESTSubsReqParams) SetSubActionIDs(actionId int64) {
1056
1057         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1058                 for _, action := range subDetail.ActionToBeSetupList {
1059                         if action != nil {
1060                                 action.ActionID = &actionId
1061                         }
1062                 }
1063         }
1064 }
1065
1066 func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition []int64) {
1067
1068         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1069                 for _, action := range subDetail.ActionToBeSetupList {
1070                         if action != nil {
1071                                 action.ActionDefinition = actionDefinition
1072                         }
1073                 }
1074         }
1075 }
1076
1077 func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition []int64) {
1078
1079         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1080                 if subDetail != nil {
1081                         subDetail.EventTriggers = eventTriggerDefinition
1082                 }
1083         }
1084 }
1085
1086 func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition []int64, subsequentActionType string, timeToWait string) {
1087
1088         actionToBeSetup := &clientmodel.ActionToBeSetup{
1089                 ActionID:         &actionId,
1090                 ActionType:       &actionType,
1091                 ActionDefinition: clientmodel.ActionDefinition(actionDefinition),
1092                 SubsequentAction: &clientmodel.SubsequentAction{
1093                         SubsequentActionType: &subsequentActionType,
1094                         TimeToWait:           &timeToWait,
1095                 },
1096         }
1097
1098         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1099                 if subDetail != nil {
1100                         subDetail.ActionToBeSetupList = append(subDetail.ActionToBeSetupList, actionToBeSetup)
1101                 }
1102         }
1103 }
1104
1105 func (p *RESTSubsReqParams) SetRMREndpoint(RMR_port int64, host string) {
1106
1107         p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1108
1109         p.SetEndpointHost(host)
1110
1111         if p.SubsReqParams.ClientEndpoint.HTTPPort == nil {
1112                 var HTTP_port int64
1113                 p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1114         }
1115 }
1116
1117 func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
1118
1119         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1120                 for _, action := range subDetail.ActionToBeSetupList {
1121                         if action != nil && action.SubsequentAction != nil {
1122                                 action.SubsequentAction.TimeToWait = &timeToWait
1123                         }
1124                 }
1125         }
1126 }
1127
1128 func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
1129
1130         if *SubscriptionID == "" {
1131                 return
1132         }
1133         p.SubsReqParams.SubscriptionID = *SubscriptionID
1134 }
1135
1136 func (p *RESTSubsReqParams) SetXappEventInstanceID(xappEventInstanceId int64) {
1137
1138         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1139                 if subDetail != nil {
1140                         subDetail.XappEventInstanceID = &xappEventInstanceId
1141                 }
1142         }
1143 }
1144
1145 //-----------------------------------------------------------------------------
1146 //
1147 //-----------------------------------------------------------------------------
1148 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
1149
1150         tc.Debug("======== Posting REST DELETE subscription(s) to Submgr ======")
1151
1152         if *subscriptionID == "" {
1153                 tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
1154         }
1155         tc.Debug("REST deleting E2 subscriptions. SubscriptionID = %s", *subscriptionID)
1156
1157         err := xapp.Subscription.Unsubscribe(*subscriptionID)
1158         if err != nil {
1159                 tc.Error("REST Delete subscription failed %s", err.Error())
1160         }
1161         tc.Debug("REST delete subscription pushed to submgr successfully. SubscriptionID = %s", *subscriptionID)
1162 }
1163
1164 //-----------------------------------------------------------------------------
1165 //
1166 //-----------------------------------------------------------------------------
1167 func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams {
1168
1169         policyParams := RESTSubsReqParams{}
1170         policyParams.GetRESTSubsReqPolicyParams(subReqCount, &tc.clientEndpoint, &tc.meid)
1171         tc.requestCount = subReqCount
1172         return &policyParams
1173 }
1174
1175 //-----------------------------------------------------------------------------
1176 //
1177 //-----------------------------------------------------------------------------
1178 func (tc *E2Stub) DecrementRequestCount() {
1179         if tc.requestCount > 0 {
1180                 tc.requestCount -= 1
1181         } else {
1182                 tc.Error("FAILED to decrement request count, count already ZERO")
1183         }
1184 }
1185
1186 //-----------------------------------------------------------------------------
1187 //
1188 //-----------------------------------------------------------------------------
1189 func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
1190
1191         p.SubsReqParams.ClientEndpoint = clientEndpoint
1192         p.SubsReqParams.Meid = meid
1193         var rANFunctionID int64 = 33
1194         p.SubsReqParams.RANFunctionID = &rANFunctionID
1195         actionId := int64(1)
1196         actionType := "policy"
1197         subsequestActioType := "continue"
1198         timeToWait := "w10ms"
1199
1200         for requestCount := 0; requestCount < subReqCount; requestCount++ {
1201                 reqId := int64(requestCount) + 1
1202                 subscriptionDetail := &clientmodel.SubscriptionDetail{
1203                         XappEventInstanceID: &reqId,
1204                         EventTriggers: clientmodel.EventTriggerDefinition{
1205                                 int64(1234 + requestCount),
1206                         },
1207                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
1208                                 &clientmodel.ActionToBeSetup{
1209                                         ActionID:   &actionId,
1210                                         ActionType: &actionType,
1211                                         ActionDefinition: clientmodel.ActionDefinition{
1212                                                 int64(5678 + requestCount),
1213                                         },
1214                                         SubsequentAction: &clientmodel.SubsequentAction{
1215                                                 SubsequentActionType: &subsequestActioType,
1216                                                 TimeToWait:           &timeToWait,
1217                                         },
1218                                 },
1219                         },
1220                 }
1221                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1222         }
1223 }
1224
1225 func (p *RESTSubsReqParams) SetSubscriptionDirectives(e2Timeout int64, e2RetryCount int64, routingNeeded bool) {
1226
1227         e2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1228         e2SubscriptionDirectives.E2TimeoutTimerValue = e2Timeout
1229         e2SubscriptionDirectives.E2RetryCount = &e2RetryCount
1230         e2SubscriptionDirectives.RMRRoutingNeeded = &routingNeeded
1231         p.SubsReqParams.E2SubscriptionDirectives = e2SubscriptionDirectives
1232
1233 }