602708f9259982256b1aa5acae6dfb7f5a18920f
[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         packerr, packedMsg := e2SubsResp.Pack(resp)
302         if packerr != nil {
303                 tc.TestError(t, "pack NOK %s", packerr.Error())
304         }
305         tc.Debug("%s", e2SubsResp.String())
306
307         params := &xapp.RMRParams{}
308         params.Mtype = xapp.RIC_SUB_RESP
309         //params.SubId = msg.SubId
310         params.SubId = -1
311         params.Payload = packedMsg.Buf
312         params.PayloadLen = len(packedMsg.Buf)
313         params.Meid = msg.Meid
314         //params.Xid = msg.Xid
315         params.Mbuf = nil
316
317         tc.Debug("SEND SUB RESP: %s", params.String())
318         snderr := tc.SendWithRetry(params, false, 5)
319         if snderr != nil {
320                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
321         }
322 }
323
324 //-----------------------------------------------------------------------------
325 //
326 //-----------------------------------------------------------------------------
327 func (tc *E2Stub) SendPartialSubsResp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *xapp.RMRParams, actionNotAdmittedList e2ap.ActionNotAdmittedList) {
328         tc.Debug("SendPartialSubsResp")
329
330         if len(actionNotAdmittedList.Items) == 0 {
331                 tc.TestError(t, "SendPartialSubsResp() Empty actionNotAdmittedList.Items")
332                 return
333         }
334
335         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
336
337         //---------------------------------
338         // e2term activity: Send Subs Resp
339         //---------------------------------
340         resp := &e2ap.E2APSubscriptionResponse{}
341
342         resp.RequestId.Id = req.RequestId.Id
343         resp.RequestId.InstanceId = req.RequestId.InstanceId
344         resp.FunctionId = req.FunctionId
345
346         for index, actionNotAdmittedItem := range actionNotAdmittedList.Items {
347                 for _, ActionToBeSetupItem := range req.ActionSetups {
348                         if ActionToBeSetupItem.ActionId == actionNotAdmittedItem.ActionId {
349                                 actionNotAdmittedItem := e2ap.ActionNotAdmittedItem{}
350                                 actionNotAdmittedItem.ActionId = ActionToBeSetupItem.ActionId
351                                 actionNotAdmittedItem.Cause.Content = 1
352                                 actionNotAdmittedItem.Cause.Value = 8
353                                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, actionNotAdmittedItem)
354                                 // Remove the element
355                                 req.ActionSetups = append(req.ActionSetups[:index], req.ActionSetups[index+1:]...)
356
357                         }
358                 }
359         }
360         for _, ActionToBeSetupItem := range req.ActionSetups {
361                 actionAdmittedItem := e2ap.ActionAdmittedItem{}
362                 actionAdmittedItem.ActionId = ActionToBeSetupItem.ActionId
363                 resp.ActionAdmittedList.Items = append(resp.ActionAdmittedList.Items, actionAdmittedItem)
364         }
365
366         packerr, packedMsg := e2SubsResp.Pack(resp)
367         if packerr != nil {
368                 tc.TestError(t, "pack NOK %s", packerr.Error())
369         }
370         tc.Debug("%s", e2SubsResp.String())
371
372         params := &xapp.RMRParams{}
373         params.Mtype = xapp.RIC_SUB_RESP
374         //params.SubId = msg.SubId
375         params.SubId = -1
376         params.Payload = packedMsg.Buf
377         params.PayloadLen = len(packedMsg.Buf)
378         params.Meid = msg.Meid
379         //params.Xid = msg.Xid
380         params.Mbuf = nil
381
382         tc.Debug("SEND SUB RESP: %s", params.String())
383         snderr := tc.SendWithRetry(params, false, 5)
384         if snderr != nil {
385                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
386         }
387 }
388
389 //-----------------------------------------------------------------------------
390 //
391 //-----------------------------------------------------------------------------
392 func (tc *E2Stub) SendInvalidE2Asn1Resp(t *testing.T, msg *xapp.RMRParams, msgType int) {
393
394         params := &xapp.RMRParams{}
395         params.Mtype = msgType
396         params.SubId = -1
397         params.Payload = []byte{1, 2, 3, 4, 5}
398         params.PayloadLen = 5
399         params.Meid = msg.Meid
400         params.Xid = ""
401         params.Mbuf = nil
402
403         if params.Mtype == xapp.RIC_SUB_RESP {
404                 tc.Debug("SEND INVALID ASN.1 SUB RESP")
405
406         } else if params.Mtype == xapp.RIC_SUB_FAILURE {
407                 tc.Debug("SEND INVALID ASN.1 SUB FAILURE")
408
409         } else if params.Mtype == xapp.RIC_SUB_DEL_RESP {
410                 tc.Debug("SEND INVALID ASN.1 SUB DEL RESP")
411
412         } else if params.Mtype == xapp.RIC_SUB_DEL_FAILURE {
413                 tc.Debug("SEND INVALID ASN.1 SUB DEL FAILURE")
414         }
415         snderr := tc.SendWithRetry(params, false, 5)
416         if snderr != nil {
417                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
418         }
419 }
420
421 //-----------------------------------------------------------------------------
422 //
423 //-----------------------------------------------------------------------------
424 func (tc *E2Stub) RecvSubsResp(t *testing.T, trans *RmrTransactionId) uint32 {
425         tc.Debug("RecvSubsResp")
426         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
427         var e2SubsId uint32
428
429         //---------------------------------
430         // xapp activity: Recv Subs Resp
431         //---------------------------------
432         msg := tc.WaitMsg(15)
433         if msg != nil {
434                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
435                         tc.TestError(t, "Received RIC_SUB_RESP wrong mtype expected %s got %s, error", "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
436                         return 0
437                 } else if msg.Xid != trans.xid {
438                         tc.TestError(t, "Received RIC_SUB_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
439                         return 0
440                 } else {
441                         packedData := &e2ap.PackedData{}
442                         packedData.Buf = msg.Payload
443                         if msg.SubId > 0 {
444                                 e2SubsId = uint32(msg.SubId)
445                         } else {
446                                 e2SubsId = 0
447                         }
448                         unpackerr, resp := e2SubsResp.UnPack(packedData)
449                         if unpackerr != nil {
450                                 tc.TestError(t, "RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
451                         }
452                         tc.Debug("Recv Subs Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
453                         return e2SubsId
454                 }
455         } else {
456                 tc.TestError(t, "Not Received msg within %d secs", 15)
457         }
458         return 0
459 }
460
461 //-----------------------------------------------------------------------------
462 //
463 //-----------------------------------------------------------------------------
464
465 func (tc *E2Stub) SendSubsFail(t *testing.T, fparams *E2StubSubsFailParams, msg *xapp.RMRParams) {
466         tc.Debug("SendSubsFail")
467         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
468
469         //---------------------------------
470         // e2term activity: Send Subs Fail
471         //---------------------------------
472         packerr, packedMsg := e2SubsFail.Pack(fparams.Fail)
473         if packerr != nil {
474                 tc.TestError(t, "pack NOK %s", packerr.Error())
475         }
476         tc.Debug("%s", e2SubsFail.String())
477
478         params := &xapp.RMRParams{}
479         params.Mtype = xapp.RIC_SUB_FAILURE
480         params.SubId = msg.SubId
481         params.Payload = packedMsg.Buf
482         params.PayloadLen = len(packedMsg.Buf)
483         params.Meid = msg.Meid
484         params.Xid = msg.Xid
485         params.Mbuf = nil
486
487         tc.Debug("SEND SUB FAIL: %s", params.String())
488         snderr := tc.SendWithRetry(params, false, 5)
489         if snderr != nil {
490                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
491         }
492 }
493
494 //-----------------------------------------------------------------------------
495 //
496 //-----------------------------------------------------------------------------
497 func (tc *E2Stub) RecvSubsFail(t *testing.T, trans *RmrTransactionId) uint32 {
498         tc.Debug("RecvSubsFail")
499         e2SubsFail := e2asnpacker.NewPackerSubscriptionFailure()
500         var e2SubsId uint32
501
502         //-------------------------------
503         // xapp activity: Recv Subs Fail
504         //-------------------------------
505         msg := tc.WaitMsg(15)
506         if msg != nil {
507                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
508                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
509                         return 0
510                 } else if msg.Xid != trans.xid {
511                         tc.TestError(t, "Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", trans.xid, msg.Xid)
512                         return 0
513                 } else {
514                         packedData := &e2ap.PackedData{}
515                         packedData.Buf = msg.Payload
516                         if msg.SubId > 0 {
517                                 e2SubsId = uint32(msg.SubId)
518                         } else {
519                                 e2SubsId = 0
520                         }
521                         unpackerr, resp := e2SubsFail.UnPack(packedData)
522                         if unpackerr != nil {
523                                 tc.TestError(t, "RIC_SUB_FAILURE unpack failed err: %s", unpackerr.Error())
524                         }
525                         tc.Debug("Recv Subs Fail rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
526                         return e2SubsId
527                 }
528         } else {
529                 tc.TestError(t, "Not Received msg within %d secs", 15)
530         }
531         return 0
532 }
533
534 //-----------------------------------------------------------------------------
535 //
536 //-----------------------------------------------------------------------------
537 func (tc *E2Stub) SendSubsDelReq(t *testing.T, oldTrans *RmrTransactionId, e2SubsId uint32) *RmrTransactionId {
538
539         trans := oldTrans
540         if oldTrans == nil {
541                 trans = tc.NewRmrTransactionId("", "RAN_NAME_1")
542         }
543
544         tc.Debug("SendSubsDelReq %s", trans.String())
545         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
546         //---------------------------------
547         // xapp activity: Send Subs Del Req
548         //---------------------------------
549         req := &e2ap.E2APSubscriptionDeleteRequest{}
550         req.RequestId.Id = 1
551         req.RequestId.InstanceId = e2SubsId
552         req.FunctionId = 1
553
554         err, packedMsg := e2SubsDelReq.Pack(req)
555         if err != nil {
556                 tc.TestError(t, "pack NOK %s %s", trans.String(), err.Error())
557                 return nil
558         }
559         tc.Debug("%s %s", trans.String(), e2SubsDelReq.String())
560
561         params := &xapp.RMRParams{}
562         params.Mtype = xapp.RIC_SUB_DEL_REQ
563         params.SubId = int(e2SubsId)
564         params.Payload = packedMsg.Buf
565         params.PayloadLen = len(packedMsg.Buf)
566         params.Meid = trans.meid
567         params.Xid = trans.xid
568         params.Mbuf = nil
569
570         tc.Debug("SEND SUB DEL REQ: %s", params.String())
571         snderr := tc.SendWithRetry(params, false, 5)
572         if snderr != nil {
573                 tc.TestError(t, "RMR SEND FAILED: %s %s", trans.String(), snderr.Error())
574                 return nil
575         }
576         return trans
577 }
578
579 //-----------------------------------------------------------------------------
580 //
581 //-----------------------------------------------------------------------------
582 func (tc *E2Stub) RecvSubsDelReq(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *xapp.RMRParams) {
583         tc.Debug("RecvSubsDelReq")
584         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
585
586         //---------------------------------
587         // e2term activity: Recv Subs Del Req
588         //---------------------------------
589         msg := tc.WaitMsg(15)
590         if msg != nil {
591                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
592                         tc.TestError(t, "Received wrong mtype expected %s got %s, error", "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
593                 } else {
594                         tc.Debug("Recv Subs Del Req")
595
596                         packedData := &e2ap.PackedData{}
597                         packedData.Buf = msg.Payload
598                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
599                         if unpackerr != nil {
600                                 tc.TestError(t, "RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
601                         }
602                         return req, msg
603                 }
604         } else {
605                 tc.TestError(t, "Not Received msg within %d secs", 15)
606         }
607         return nil, nil
608 }
609
610 //-----------------------------------------------------------------------------
611 //
612 //-----------------------------------------------------------------------------
613 func (tc *E2Stub) SendSubsDelResp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
614         tc.Debug("SendSubsDelResp")
615         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
616
617         //---------------------------------
618         // e2term activity: Send Subs Del Resp
619         //---------------------------------
620         resp := &e2ap.E2APSubscriptionDeleteResponse{}
621         resp.RequestId.Id = req.RequestId.Id
622         resp.RequestId.InstanceId = req.RequestId.InstanceId
623         resp.FunctionId = req.FunctionId
624
625         packerr, packedMsg := e2SubsDelResp.Pack(resp)
626         if packerr != nil {
627                 tc.TestError(t, "pack NOK %s", packerr.Error())
628         }
629         tc.Debug("%s", e2SubsDelResp.String())
630
631         params := &xapp.RMRParams{}
632         params.Mtype = xapp.RIC_SUB_DEL_RESP
633         params.SubId = msg.SubId
634         params.Payload = packedMsg.Buf
635         params.PayloadLen = len(packedMsg.Buf)
636         params.Meid = msg.Meid
637         params.Xid = msg.Xid
638         params.Mbuf = nil
639
640         tc.Debug("SEND SUB DEL RESP: %s", params.String())
641         snderr := tc.SendWithRetry(params, false, 5)
642         if snderr != nil {
643                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
644         }
645 }
646
647 //-----------------------------------------------------------------------------
648 //
649 //-----------------------------------------------------------------------------
650 func (tc *E2Stub) RecvSubsDelResp(t *testing.T, trans *RmrTransactionId) {
651         tc.Debug("RecvSubsDelResp")
652         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
653
654         //---------------------------------
655         // xapp activity: Recv Subs Del Resp
656         //---------------------------------
657         msg := tc.WaitMsg(15)
658         if msg != nil {
659                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
660                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype])
661                         return
662                 } else if trans != nil && msg.Xid != trans.xid {
663                         tc.TestError(t, "Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", trans.xid, msg.Xid)
664                         return
665                 } else {
666                         packedData := &e2ap.PackedData{}
667                         packedData.Buf = msg.Payload
668                         unpackerr, resp := e2SubsDelResp.UnPack(packedData)
669                         if unpackerr != nil {
670                                 tc.TestError(t, "RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
671                         }
672                         tc.Debug("Recv Subs Del Resp rmr: xid=%s subid=%d, asn: instanceid=%d", msg.Xid, msg.SubId, resp.RequestId.InstanceId)
673                         return
674                 }
675         } else {
676                 tc.TestError(t, "Not Received msg within %d secs", 15)
677         }
678 }
679
680 //-----------------------------------------------------------------------------
681 //
682 //-----------------------------------------------------------------------------
683 func (tc *E2Stub) SendSubsDelFail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
684         tc.Debug("SendSubsDelFail")
685         e2SubsDelFail := e2asnpacker.NewPackerSubscriptionDeleteFailure()
686
687         //---------------------------------
688         // e2term activity: Send Subs Del Fail
689         //---------------------------------
690         resp := &e2ap.E2APSubscriptionDeleteFailure{}
691         resp.RequestId.Id = req.RequestId.Id
692         resp.RequestId.InstanceId = req.RequestId.InstanceId
693         resp.FunctionId = req.FunctionId
694         resp.Cause.Content = 5 // CauseMisc
695         resp.Cause.Value = 3   // unspecified
696
697         packerr, packedMsg := e2SubsDelFail.Pack(resp)
698         if packerr != nil {
699                 tc.TestError(t, "pack NOK %s", packerr.Error())
700         }
701         tc.Debug("%s", e2SubsDelFail.String())
702
703         params := &xapp.RMRParams{}
704         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
705         params.SubId = msg.SubId
706         params.Payload = packedMsg.Buf
707         params.PayloadLen = len(packedMsg.Buf)
708         params.Meid = msg.Meid
709         params.Xid = msg.Xid
710         params.Mbuf = nil
711
712         tc.Debug("SEND SUB DEL FAIL: %s", params.String())
713         snderr := tc.SendWithRetry(params, false, 5)
714         if snderr != nil {
715                 tc.TestError(t, "RMR SEND FAILED: %s", snderr.Error())
716         }
717 }
718
719 // REST code below all
720
721 /*****************************************************************************/
722 // REST interface specific functions are below
723
724 //-----------------------------------------------------------------------------
725 // Callback handler for subscription response notifications
726 //-----------------------------------------------------------------------------
727 func (tc *E2Stub) SubscriptionRespHandler(resp *clientmodel.SubscriptionResponse) {
728
729         if tc.subscriptionId == "SUBSCRIPTIONID NOT SET" {
730                 tc.Debug("REST notification received for %v while no SubscriptionID was not set for E2EventInstanceID=%v, XappEventInstanceID=%v",
731                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
732                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
733         } else if tc.subscriptionId == *resp.SubscriptionID {
734                 tc.Debug("REST notification received SubscriptionID=%s, E2EventInstanceID=%v, RequestorID=%v",
735                         *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
736                 tc.CallBackNotification <- *resp.SubscriptionInstances[0].E2EventInstanceID
737         } else {
738                 tc.Debug("MISMATCHING REST notification received SubscriptionID=%s<>%s, E2EventInstanceID=%v, XappEventInstanceID=%v",
739                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
740         }
741 }
742
743 //-----------------------------------------------------------------------------
744 //
745 //-----------------------------------------------------------------------------
746
747 func (tc *E2Stub) ExpectRESTNotification(t *testing.T, restSubsId string) {
748         tc.expectNotification(t, restSubsId, "")
749 }
750
751 func (tc *E2Stub) ExpectRESTNotificationOk(t *testing.T, restSubsId string) {
752         tc.expectNotification(t, restSubsId, "allOk")
753 }
754
755 func (tc *E2Stub) ExpectRESTNotificationNok(t *testing.T, restSubsId string, expectError string) {
756         tc.expectNotification(t, restSubsId, expectError)
757 }
758
759 func (tc *E2Stub) expectNotification(t *testing.T, restSubsId string, expectError string) {
760
761         tc.Debug("### Started to wait REST notification for restSubsId= %v, RMRPort=%v, requestCount=%v responses expected", restSubsId, *tc.clientEndpoint.RMRPort, tc.requestCount)
762         tc.restSubsIdList = []string{restSubsId}
763         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
764         if tc.requestCount == 0 {
765                 tc.TestError(t, "### NO REST notifications SET received for restSubsId= %v, RMRPort=%v, request count ZERO!", restSubsId, *tc.clientEndpoint.RMRPort)
766         }
767         go func() {
768                 select {
769                 case e2Ids := <-tc.CallBackListedNotifications:
770                         if tc.requestCount == 0 {
771                                 tc.TestError(t, "### REST notification count unexpectedly ZERO for restSubsId= %v", restSubsId)
772                         } else if e2Ids.RestSubsId != restSubsId {
773                                 tc.TestError(t, "### Unexpected REST notifications received, expected %s but got %s instead", e2Ids.RestSubsId, restSubsId)
774                         } else if e2Ids.ErrorCause == "" && expectError == "allFail" {
775                                 tc.TestError(t, "### Unexpected ok cause received from REST notifications |%s:%s|", e2Ids.RestSubsId, restSubsId)
776                         } else if e2Ids.ErrorCause != "" && expectError == "allOk" {
777                                 tc.TestError(t, "### Unexpected ErrorCause: (%s), ErrorSource: (%s), TimeoutType: (%s) received from REST notifications |%s:%s|", e2Ids.ErrorCause, e2Ids.ErrorSource, e2Ids.TimeoutType, e2Ids.RestSubsId, restSubsId)
778                         } else {
779                                 tc.requestCount--
780                                 if tc.requestCount == 0 {
781                                         tc.Debug("### All expected REST notifications received for restSubsId= %v)", e2Ids.RestSubsId)
782                                 } else {
783                                         tc.Debug("### Expected REST notifications received for restSubsId= %v", e2Ids.RestSubsId)
784                                 }
785                                 if e2Ids.ErrorCause != "" && expectError == "allFail" {
786                                         tc.Debug("### REST Notification: RestSubsId: %s, ErrorCause: %s, ErrorSource: (%s), TimeoutType: (%s)", e2Ids.RestSubsId, e2Ids.ErrorCause, e2Ids.ErrorSource, e2Ids.TimeoutType)
787                                 }
788                                 tc.Debug("### REST Notification received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
789                                 tc.ListedRESTNotifications <- e2Ids
790                                 if len(tc.ListedRESTNotifications) > 1 {
791                                         panic("expectNotification - ListedRESTNotifications stacking up")
792                                 }
793                         }
794                 case <-time.After(15 * time.Second):
795                         err := fmt.Errorf("### Timeout 15s expired while expecting REST notification for subsId: %v", restSubsId)
796                         tc.TestError(t, "%s", err.Error())
797                 }
798         }()
799 }
800
801 func (tc *E2Stub) WaitRESTNotification(t *testing.T, restSubsId string) uint32 {
802
803         stack := string(debug.Stack())
804
805         select {
806         case e2SubsId := <-tc.ListedRESTNotifications:
807                 if e2SubsId.RestSubsId == restSubsId {
808                         tc.Debug("### Expected REST notifications received %s, e2SubsId %v", e2SubsId.RestSubsId, e2SubsId.E2SubsId)
809                         return e2SubsId.E2SubsId
810                 } else {
811                         tc.TestError(t, "### Unexpected REST notification %s received, expected %v", e2SubsId.RestSubsId, restSubsId)
812                         xapp.Logger.Debug("CALL STACK:\n %s", stack)
813                         return 0
814                 }
815         case <-time.After(15 * time.Second):
816                 err := fmt.Errorf("### Timeout 15s expired while waiting REST notification for subsId: %v", restSubsId)
817                 tc.TestError(t, "%s", err.Error())
818                 xapp.Logger.Debug("CALL STACK:\n %s", stack)
819                 panic("WaitRESTNotification - timeout error")
820         }
821 }
822
823 // Note, this function should be followed by a handling of <-xappConn1.RESTNotification.
824 func (tc *E2Stub) ExpectAnyNotification(t *testing.T) {
825         go func() {
826                 tc.Debug("### Started waiting ANY REST notifications received for RMRPort=%v", *tc.clientEndpoint.RMRPort)
827                 select {
828                 case e2SubsId := <-tc.CallBackNotification:
829                         tc.Debug("### ANY REST notifications received for e2SubsId %v RMRPort=%v", e2SubsId, *tc.clientEndpoint.RMRPort)
830                         tc.RESTNotification <- (uint32)(e2SubsId)
831                 case <-time.After(15 * time.Second):
832                         err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
833                         tc.TestError(t, "%s", err.Error())
834                         tc.RESTNotification <- 0
835                 }
836         }()
837 }
838
839 func (tc *E2Stub) WaitAnyRESTNotification(t *testing.T) uint32 {
840
841         select {
842         case e2SubsId := <-tc.RESTNotification:
843                 tc.Debug("### Expected ANY REST notification received for e2SubsId %v", e2SubsId)
844                 return e2SubsId
845
846         case <-time.After(15 * time.Second):
847                 err := fmt.Errorf("### Timeout 15s expired while waiting ANY REST notification")
848                 tc.TestError(t, "%s", err.Error())
849                 panic("WaitRESTNotification - timeout error")
850         }
851 }
852
853 func (tc *E2Stub) ListedRestNotifHandler(resp *clientmodel.SubscriptionResponse) {
854
855         if len(tc.restSubsIdList) == 0 {
856                 tc.Error("Unexpected listed REST notifications received for SubscriptionID=%s, expected restSubsId list size was ZERO!", *resp.SubscriptionID)
857         } else {
858                 for i, subsId := range tc.restSubsIdList {
859                         if *resp.SubscriptionID == subsId {
860                                 //tc.Debug("Listed REST notifications received SubscriptionID=%s, InstanceID=%v, XappEventInstanceID=%v",
861                                 //      *resp.SubscriptionID, *resp.SubscriptionInstances[0].InstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
862
863                                 tc.restSubsIdList = append(tc.restSubsIdList[:i], tc.restSubsIdList[i+1:]...)
864                                 //tc.Debug("Removed %s from Listed REST notifications, %v entries left", *resp.SubscriptionID, len(tc.restSubsIdList))
865
866                                 //                              if resp.SubscriptionInstances[0].ErrorCause != nil {
867                                 tc.CallBackListedNotifications <- *tc.GetE2RestIds(resp)
868                                 //                              } else {
869                                 //                                      tc.CallBackListedNotifications <- E2RestIds{*resp.SubscriptionID, uint32(*resp.SubscriptionInstances[0].E2EventInstanceID), "", "", ""}
870                                 //                              }
871
872                                 if len(tc.restSubsIdList) == 0 {
873                                         tc.Debug("All listed REST notifications received for SubscriptionID=%s", *resp.SubscriptionID)
874                                 }
875
876                                 return
877                         }
878                 }
879                 tc.Error("UNKONWN REST notification received SubscriptionID=%s<>%s, InstanceID=%v, XappEventInstanceID=%v",
880                         tc.subscriptionId, *resp.SubscriptionID, *resp.SubscriptionInstances[0].E2EventInstanceID, *resp.SubscriptionInstances[0].XappEventInstanceID)
881         }
882 }
883
884 //-----------------------------------------------------------------------------
885 //
886 //-----------------------------------------------------------------------------
887 func (tc *E2Stub) GetE2RestIds(resp *clientmodel.SubscriptionResponse) *E2RestIds {
888
889         e2RestIds := &E2RestIds{}
890         if resp != nil {
891                 if resp.SubscriptionID != nil {
892                         e2RestIds.RestSubsId = *resp.SubscriptionID
893                 }
894                 if resp.SubscriptionInstances != nil {
895                         if resp.SubscriptionInstances[0].E2EventInstanceID != nil {
896                                 e2RestIds.E2SubsId = uint32(*resp.SubscriptionInstances[0].E2EventInstanceID)
897                         }
898                         e2RestIds.ErrorCause = resp.SubscriptionInstances[0].ErrorCause
899                         e2RestIds.ErrorSource = resp.SubscriptionInstances[0].ErrorSource
900                         e2RestIds.TimeoutType = resp.SubscriptionInstances[0].TimeoutType
901                 }
902         }
903         return e2RestIds
904 }
905
906 //-----------------------------------------------------------------------------
907 //
908 //-----------------------------------------------------------------------------
909 func (tc *E2Stub) WaitListedRestNotifications(t *testing.T, restSubsIds []string) {
910         tc.Debug("Started to wait REST notifications for restSubsIds=%v, RMRPort=%v", restSubsIds, *tc.clientEndpoint.RMRPort)
911
912         tc.restSubsIdList = restSubsIds
913         xapp.Subscription.SetResponseCB(tc.ListedRestNotifHandler)
914
915         for i := 0; i < len(restSubsIds); i++ {
916                 go func() {
917                         select {
918                         case e2Ids := <-tc.CallBackListedNotifications:
919                                 tc.Debug("Listed Notification waiter received Notif for %s : %v", e2Ids.RestSubsId, e2Ids.E2SubsId)
920                                 tc.ListedRESTNotifications <- e2Ids
921                         case <-time.After(15 * time.Second):
922                                 err := fmt.Errorf("Timeout 15s expired while waiting Listed REST notification")
923                                 tc.TestError(t, "%s", err.Error())
924                                 tc.RESTNotification <- 0
925                         }
926                 }()
927         }
928 }
929
930 //-----------------------------------------------------------------------------
931 //
932 //-----------------------------------------------------------------------------
933 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string {
934         tc.Debug("======== Posting REST subscriptions to Submgr ======")
935
936         if params == nil {
937                 tc.Debug("SendRESTReportSubsReq: params == nil")
938                 return ""
939         }
940
941         tc.subscriptionId = "SUBSCRIPTIONID NOT SET"
942
943         resp, err := xapp.Subscription.Subscribe(&params.SubsReqParams)
944         if err != nil {
945                 // Swagger generated code makes checks for the values that are inserted the subscription function
946                 // If error cause is unknown and POST is not done, the problem is in the inserted values
947                 tc.Error("======== REST subscription request failed %s ========", err.Error())
948                 return ""
949         }
950         tc.subscriptionId = *resp.SubscriptionID
951         tc.Debug("======== REST subscriptions posted successfully. SubscriptionID = %s, RequestCount = %v ========", *resp.SubscriptionID, tc.requestCount)
952         return *resp.SubscriptionID
953 }
954
955 //-----------------------------------------------------------------------------
956 //
957 //-----------------------------------------------------------------------------
958 func (tc *E2Stub) GetRESTSubsReqReportParams(subReqCount int) *RESTSubsReqParams {
959
960         reportParams := RESTSubsReqParams{}
961         reportParams.GetRESTSubsReqReportParams(subReqCount, &tc.clientEndpoint, &tc.meid)
962         tc.requestCount = subReqCount
963         return &reportParams
964 }
965
966 //-----------------------------------------------------------------------------
967 //
968 //-----------------------------------------------------------------------------
969 type RESTSubsReqParams struct {
970         SubsReqParams clientmodel.SubscriptionParams
971 }
972
973 func (p *RESTSubsReqParams) GetRESTSubsReqReportParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
974
975         // E2SM-gNB-X2
976         p.SubsReqParams.ClientEndpoint = clientEndpoint
977         p.SubsReqParams.Meid = meid
978         var rANFunctionID int64 = 33
979         p.SubsReqParams.RANFunctionID = &rANFunctionID
980
981         actionId := int64(1)
982         actionType := "report"
983         subsequestActioType := "continue"
984         timeToWait := "w10ms"
985
986         for requestCount := 0; requestCount < subReqCount; requestCount++ {
987                 reqId := int64(requestCount) + 1
988                 subscriptionDetail := &clientmodel.SubscriptionDetail{
989                         XappEventInstanceID: &reqId,
990                         EventTriggers: clientmodel.EventTriggerDefinition{
991                                 int64(1234 + requestCount),
992                         },
993                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
994                                 &clientmodel.ActionToBeSetup{
995                                         ActionID:   &actionId,
996                                         ActionType: &actionType,
997                                         ActionDefinition: clientmodel.ActionDefinition{
998                                                 int64(5678 + requestCount),
999                                         },
1000                                         SubsequentAction: &clientmodel.SubsequentAction{
1001                                                 SubsequentActionType: &subsequestActioType,
1002                                                 TimeToWait:           &timeToWait,
1003                                         },
1004                                 },
1005                         },
1006                 }
1007                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1008         }
1009
1010 }
1011
1012 func (p *RESTSubsReqParams) SetMeid(MEID string) {
1013         p.SubsReqParams.Meid = &MEID
1014 }
1015
1016 func (p *RESTSubsReqParams) SetEndpoint(HTTP_port int64, RMR_port int64, host string) {
1017         var endpoint clientmodel.SubscriptionParamsClientEndpoint
1018         endpoint.HTTPPort = &HTTP_port
1019         endpoint.RMRPort = &RMR_port
1020         endpoint.Host = host
1021         p.SubsReqParams.ClientEndpoint = &endpoint
1022 }
1023
1024 func (p *RESTSubsReqParams) SetEndpointHost(host string) {
1025
1026         if p.SubsReqParams.ClientEndpoint.Host != "" {
1027                 if p.SubsReqParams.ClientEndpoint.Host != host {
1028                         // Renaming toto, print something tc.Debug("Posting REST subscription request to Submgr")
1029                         err := fmt.Errorf("hostname change attempt: %s -> %s", p.SubsReqParams.ClientEndpoint.Host, host)
1030                         panic(err)
1031                 }
1032         }
1033         p.SubsReqParams.ClientEndpoint.Host = host
1034 }
1035
1036 func (p *RESTSubsReqParams) SetHTTPEndpoint(HTTP_port int64, host string) {
1037
1038         p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1039
1040         p.SetEndpointHost(host)
1041
1042         if p.SubsReqParams.ClientEndpoint.RMRPort == nil {
1043                 var RMR_port int64
1044                 p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1045         }
1046 }
1047
1048 func (p *RESTSubsReqParams) SetE2SubscriptionDirectives(E2RetryCount int64, E2TimeoutTimerValue int64, RMRRoutingNeeded bool) {
1049
1050         E2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1051         p.SubsReqParams.E2SubscriptionDirectives = E2SubscriptionDirectives
1052         p.SubsReqParams.E2SubscriptionDirectives.E2RetryCount = &E2RetryCount
1053         p.SubsReqParams.E2SubscriptionDirectives.E2TimeoutTimerValue = E2TimeoutTimerValue
1054         p.SubsReqParams.E2SubscriptionDirectives.RMRRoutingNeeded = RMRRoutingNeeded
1055 }
1056
1057 func (p *RESTSubsReqParams) RemoveE2SubscriptionDirectives() {
1058
1059         p.SubsReqParams.E2SubscriptionDirectives = nil
1060 }
1061
1062 func (p *RESTSubsReqParams) SetSubActionTypes(actionType string) {
1063
1064         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1065                 for _, action := range subDetail.ActionToBeSetupList {
1066                         if action != nil {
1067                                 action.ActionType = &actionType
1068                         }
1069                 }
1070         }
1071 }
1072
1073 func (p *RESTSubsReqParams) SetSubActionIDs(actionId int64) {
1074
1075         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1076                 for _, action := range subDetail.ActionToBeSetupList {
1077                         if action != nil {
1078                                 action.ActionID = &actionId
1079                         }
1080                 }
1081         }
1082 }
1083
1084 func (p *RESTSubsReqParams) SetSubActionDefinition(actionDefinition []int64) {
1085
1086         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1087                 for _, action := range subDetail.ActionToBeSetupList {
1088                         if action != nil {
1089                                 action.ActionDefinition = actionDefinition
1090                         }
1091                 }
1092         }
1093 }
1094
1095 func (p *RESTSubsReqParams) SetSubEventTriggerDefinition(eventTriggerDefinition []int64) {
1096
1097         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1098                 if subDetail != nil {
1099                         subDetail.EventTriggers = eventTriggerDefinition
1100                 }
1101         }
1102 }
1103
1104 func (p *RESTSubsReqParams) AppendActionToActionToBeSetupList(actionId int64, actionType string, actionDefinition []int64, subsequentActionType string, timeToWait string) {
1105
1106         actionToBeSetup := &clientmodel.ActionToBeSetup{
1107                 ActionID:         &actionId,
1108                 ActionType:       &actionType,
1109                 ActionDefinition: clientmodel.ActionDefinition(actionDefinition),
1110                 SubsequentAction: &clientmodel.SubsequentAction{
1111                         SubsequentActionType: &subsequentActionType,
1112                         TimeToWait:           &timeToWait,
1113                 },
1114         }
1115
1116         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1117                 if subDetail != nil {
1118                         subDetail.ActionToBeSetupList = append(subDetail.ActionToBeSetupList, actionToBeSetup)
1119                 }
1120         }
1121 }
1122
1123 func (p *RESTSubsReqParams) SetRMREndpoint(RMR_port int64, host string) {
1124
1125         p.SubsReqParams.ClientEndpoint.RMRPort = &RMR_port
1126
1127         p.SetEndpointHost(host)
1128
1129         if p.SubsReqParams.ClientEndpoint.HTTPPort == nil {
1130                 var HTTP_port int64
1131                 p.SubsReqParams.ClientEndpoint.HTTPPort = &HTTP_port
1132         }
1133 }
1134
1135 func (p *RESTSubsReqParams) SetTimeToWait(timeToWait string) {
1136
1137         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1138                 for _, action := range subDetail.ActionToBeSetupList {
1139                         if action != nil && action.SubsequentAction != nil {
1140                                 action.SubsequentAction.TimeToWait = &timeToWait
1141                         }
1142                 }
1143         }
1144 }
1145
1146 func (p *RESTSubsReqParams) SetSubscriptionID(SubscriptionID *string) {
1147
1148         if *SubscriptionID == "" {
1149                 return
1150         }
1151         p.SubsReqParams.SubscriptionID = *SubscriptionID
1152 }
1153
1154 func (p *RESTSubsReqParams) SetXappEventInstanceID(xappEventInstanceId int64) {
1155
1156         for _, subDetail := range p.SubsReqParams.SubscriptionDetails {
1157                 if subDetail != nil {
1158                         subDetail.XappEventInstanceID = &xappEventInstanceId
1159                 }
1160         }
1161 }
1162
1163 //-----------------------------------------------------------------------------
1164 //
1165 //-----------------------------------------------------------------------------
1166 func (tc *E2Stub) SendRESTSubsDelReq(t *testing.T, subscriptionID *string) {
1167
1168         tc.Debug("======== Posting REST DELETE subscription(s) to Submgr ======")
1169
1170         if *subscriptionID == "" {
1171                 tc.Error("REST error in deleting subscriptions. Empty SubscriptionID = %s", *subscriptionID)
1172         }
1173         tc.Debug("REST deleting E2 subscriptions. SubscriptionID = %s", *subscriptionID)
1174
1175         err := xapp.Subscription.Unsubscribe(*subscriptionID)
1176         if err != nil {
1177                 tc.Error("REST Delete subscription failed %s", err.Error())
1178         }
1179         tc.Debug("REST delete subscription pushed to submgr successfully. SubscriptionID = %s", *subscriptionID)
1180 }
1181
1182 //-----------------------------------------------------------------------------
1183 //
1184 //-----------------------------------------------------------------------------
1185 func (tc *E2Stub) GetRESTSubsReqPolicyParams(subReqCount int) *RESTSubsReqParams {
1186
1187         policyParams := RESTSubsReqParams{}
1188         policyParams.GetRESTSubsReqPolicyParams(subReqCount, &tc.clientEndpoint, &tc.meid)
1189         tc.requestCount = subReqCount
1190         return &policyParams
1191 }
1192
1193 //-----------------------------------------------------------------------------
1194 //
1195 //-----------------------------------------------------------------------------
1196 func (tc *E2Stub) DecrementRequestCount() {
1197         if tc.requestCount > 0 {
1198                 tc.requestCount -= 1
1199         } else {
1200                 tc.Error("FAILED to decrement request count, count already ZERO")
1201         }
1202 }
1203
1204 //-----------------------------------------------------------------------------
1205 //
1206 //-----------------------------------------------------------------------------
1207 func (p *RESTSubsReqParams) GetRESTSubsReqPolicyParams(subReqCount int, clientEndpoint *clientmodel.SubscriptionParamsClientEndpoint, meid *string) {
1208
1209         p.SubsReqParams.ClientEndpoint = clientEndpoint
1210         p.SubsReqParams.Meid = meid
1211         var rANFunctionID int64 = 33
1212         p.SubsReqParams.RANFunctionID = &rANFunctionID
1213         actionId := int64(1)
1214         actionType := "policy"
1215         subsequestActioType := "continue"
1216         timeToWait := "w10ms"
1217
1218         for requestCount := 0; requestCount < subReqCount; requestCount++ {
1219                 reqId := int64(requestCount) + 1
1220                 subscriptionDetail := &clientmodel.SubscriptionDetail{
1221                         XappEventInstanceID: &reqId,
1222                         EventTriggers: clientmodel.EventTriggerDefinition{
1223                                 int64(1234 + requestCount),
1224                         },
1225                         ActionToBeSetupList: clientmodel.ActionsToBeSetup{
1226                                 &clientmodel.ActionToBeSetup{
1227                                         ActionID:   &actionId,
1228                                         ActionType: &actionType,
1229                                         ActionDefinition: clientmodel.ActionDefinition{
1230                                                 int64(5678 + requestCount),
1231                                         },
1232                                         SubsequentAction: &clientmodel.SubsequentAction{
1233                                                 SubsequentActionType: &subsequestActioType,
1234                                                 TimeToWait:           &timeToWait,
1235                                         },
1236                                 },
1237                         },
1238                 }
1239                 p.SubsReqParams.SubscriptionDetails = append(p.SubsReqParams.SubscriptionDetails, subscriptionDetail)
1240         }
1241 }
1242
1243 func (p *RESTSubsReqParams) SetSubscriptionDirectives(e2Timeout int64, e2RetryCount int64, routingNeeded bool) {
1244
1245         e2SubscriptionDirectives := &clientmodel.SubscriptionParamsE2SubscriptionDirectives{}
1246         e2SubscriptionDirectives.E2TimeoutTimerValue = e2Timeout
1247         e2SubscriptionDirectives.E2RetryCount = &e2RetryCount
1248         e2SubscriptionDirectives.RMRRoutingNeeded = routingNeeded
1249         p.SubsReqParams.E2SubscriptionDirectives = e2SubscriptionDirectives
1250
1251 }