730856389b6863efb566c57eda2021b712af0fe4
[ric-plt/submgr.git] / pkg / control / messaging_test.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 control
21
22 import (
23         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
24         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
25         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27         "testing"
28         "time"
29 )
30
31 //-----------------------------------------------------------------------------
32 //
33 //-----------------------------------------------------------------------------
34
35 var e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
36
37 //-----------------------------------------------------------------------------
38 //
39 //-----------------------------------------------------------------------------
40 func createSubsReq() *e2ap.E2APSubscriptionRequest {
41         req := &e2ap.E2APSubscriptionRequest{}
42
43         req.RequestId.Id = 1
44         req.RequestId.Seq = 0
45         req.FunctionId = 1
46
47         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true
48         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150")
49         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123
50         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28
51
52         // gnb -> enb outgoing
53         // enb -> gnb incoming
54         // X2 36423-f40.doc
55         req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming
56         req.EventTriggerDefinition.ProcedureCode = 5 //28 35
57         req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage
58
59         req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
60         req.ActionSetups[0].ActionId = 0
61         req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
62         req.ActionSetups[0].ActionDefinition.Present = false
63         //req.ActionSetups[index].ActionDefinition.StyleId = 255
64         //req.ActionSetups[index].ActionDefinition.ParamId = 222
65         req.ActionSetups[0].SubsequentAction.Present = true
66         req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
67         req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
68
69         return req
70 }
71
72 //-----------------------------------------------------------------------------
73 //
74 //-----------------------------------------------------------------------------
75 func createSubsResp(req *e2ap.E2APSubscriptionRequest) *e2ap.E2APSubscriptionResponse {
76
77         resp := &e2ap.E2APSubscriptionResponse{}
78
79         resp.RequestId.Id = req.RequestId.Id
80         resp.RequestId.Seq = req.RequestId.Seq
81         resp.FunctionId = req.FunctionId
82
83         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
84         for index := int(0); index < len(req.ActionSetups); index++ {
85                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
86         }
87
88         for index := uint64(0); index < 1; index++ {
89                 item := e2ap.ActionNotAdmittedItem{}
90                 item.ActionId = index
91                 item.Cause.Content = 1
92                 item.Cause.CauseVal = 1
93                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
94         }
95
96         return resp
97 }
98
99 //-----------------------------------------------------------------------------
100 //
101 //-----------------------------------------------------------------------------
102 func createSubsDelReq(e2SubsId uint32) *e2ap.E2APSubscriptionDeleteRequest {
103         req := &e2ap.E2APSubscriptionDeleteRequest{}
104         req.RequestId.Id = 1
105         req.RequestId.Seq = e2SubsId
106         req.FunctionId = 1
107         return req
108 }
109
110 //-----------------------------------------------------------------------------
111 //
112 //-----------------------------------------------------------------------------
113 func createSubsDelResp(req *e2ap.E2APSubscriptionDeleteRequest) *e2ap.E2APSubscriptionDeleteResponse {
114         resp := &e2ap.E2APSubscriptionDeleteResponse{}
115         resp.RequestId.Id = req.RequestId.Id
116         resp.RequestId.Seq = req.RequestId.Seq
117         resp.FunctionId = req.FunctionId
118         return resp
119 }
120
121 //-----------------------------------------------------------------------------
122 //
123 //-----------------------------------------------------------------------------
124 func handle_xapp_subs_req(t *testing.T) {
125         xapp.Logger.Info("handle_xapp_subs_req start")
126         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
127
128         //---------------------------------
129         // xapp activity: Send Subs Req
130         //---------------------------------
131         //select {
132         //case <-time.After(1 * time.Second):
133         xapp.Logger.Info("(xappConn) Send Subs Req")
134         req := createSubsReq()
135         e2SubsReq.Set(req)
136         xapp.Logger.Debug("%s", e2SubsReq.String())
137         err, packedMsg := e2SubsReq.Pack(nil)
138         if err != nil {
139                 testError(t, "(xappConn) pack NOK %s", err.Error())
140         }
141
142         params := &xapp.RMRParams{}
143         params.Mtype = xapp.RIC_SUB_REQ
144         params.SubId = -1
145         params.Payload = packedMsg.Buf
146         params.Meid = &xapp.RMRMeid{RanName: "RAN_NAME_1"}
147         params.Xid = "XID_1"
148         params.Mbuf = nil
149
150         snderr := xappConn.RmrSend(params)
151         if snderr != nil {
152                 testError(t, "(xappConn) RMR SEND FAILED: %s", snderr.Error())
153         }
154         //}
155 }
156
157 //-----------------------------------------------------------------------------
158 //
159 //-----------------------------------------------------------------------------
160 func handle_e2term_subs_req(t *testing.T) (*e2ap.E2APSubscriptionRequest, *xapp.RMRParams) {
161         xapp.Logger.Info("handle_e2term_subs_req start")
162         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
163
164         //---------------------------------
165         // e2term activity: Recv Subs Req
166         //---------------------------------
167         select {
168         case msg := <-e2termConn.rmrConChan:
169                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
170                         testError(t, "(e2termConn) Received non RIC_SUB_REQ message")
171                 } else {
172                         xapp.Logger.Info("(e2termConn) Recv Subs Req")
173                         packedData := &packer.PackedData{}
174                         packedData.Buf = msg.Payload
175                         unpackerr := e2SubsReq.UnPack(packedData)
176                         if unpackerr != nil {
177                                 testError(t, "(e2termConn) RIC_SUB_REQ unpack failed err: %s", unpackerr.Error())
178                         }
179                         geterr, req := e2SubsReq.Get()
180                         if geterr != nil {
181                                 testError(t, "(e2termConn) RIC_SUB_REQ get failed err: %s", geterr.Error())
182                         }
183                         return req, msg
184                 }
185         case <-time.After(15 * time.Second):
186                 testError(t, "(e2termConn) Not Received RIC_SUB_REQ within 15 secs")
187         }
188         return nil, nil
189 }
190
191 func handle_e2term_subs_resp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *xapp.RMRParams) {
192         xapp.Logger.Info("handle_e2term_subs_resp start")
193         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
194
195         //---------------------------------
196         // e2term activity: Send Subs Resp
197         //---------------------------------
198         xapp.Logger.Info("(e2termConn) Send Subs Resp")
199         resp := createSubsResp(req)
200         e2SubsResp.Set(resp)
201         xapp.Logger.Debug("%s", e2SubsResp.String())
202         packerr, packedMsg := e2SubsResp.Pack(nil)
203         if packerr != nil {
204                 testError(t, "(e2termConn) pack NOK %s", packerr.Error())
205         }
206
207         params := &xapp.RMRParams{}
208         params.Mtype = xapp.RIC_SUB_RESP
209         params.SubId = msg.SubId
210         params.Payload = packedMsg.Buf
211         params.Meid = msg.Meid
212         params.Xid = msg.Xid
213         params.Mbuf = nil
214
215         snderr := e2termConn.RmrSend(params)
216         if snderr != nil {
217                 testError(t, "(e2termConn) RMR SEND FAILED: %s", snderr.Error())
218         }
219 }
220
221 func handle_e2term_subs_reqandresp(t *testing.T) {
222         req, msg := handle_e2term_subs_req(t)
223         handle_e2term_subs_resp(t, req, msg)
224 }
225
226 //-----------------------------------------------------------------------------
227 //
228 //-----------------------------------------------------------------------------
229 func handle_xapp_subs_resp(t *testing.T) int {
230         xapp.Logger.Info("handle_xapp_subs_resp start")
231         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
232         var e2SubsId int
233
234         //---------------------------------
235         // xapp activity: Recv Subs Resp
236         //---------------------------------
237         select {
238         case msg := <-xappConn.rmrConChan:
239                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
240                         testError(t, "(xappConn) Received non RIC_SUB_RESP message")
241                 } else {
242                         xapp.Logger.Info("(xappConn) Recv Subs Resp")
243
244                         packedData := &packer.PackedData{}
245                         packedData.Buf = msg.Payload
246                         e2SubsId = msg.SubId
247                         unpackerr := e2SubsResp.UnPack(packedData)
248
249                         if unpackerr != nil {
250                                 testError(t, "(xappConn) RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
251                         }
252                         geterr, _ := e2SubsResp.Get()
253                         if geterr != nil {
254                                 testError(t, "(xappConn) RIC_SUB_RESP get failed err: %s", geterr.Error())
255                         }
256
257                 }
258         case <-time.After(15 * time.Second):
259                 testError(t, "(xappConn) Not Received RIC_SUB_RESP within 15 secs")
260         }
261         return e2SubsId
262 }
263
264 //-----------------------------------------------------------------------------
265 //
266 //-----------------------------------------------------------------------------
267 func handle_xapp_subs_del_req(t *testing.T, e2SubsId int) {
268         xapp.Logger.Info("handle_xapp_subs_del_req start")
269         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
270
271         //---------------------------------
272         // xapp activity: Send Subs Del Req
273         //---------------------------------
274         //select {
275         //case <-time.After(1 * time.Second):
276         xapp.Logger.Info("(xappConn) Send Subs Del Req")
277         req := createSubsDelReq(uint32(e2SubsId))
278         e2SubsDelReq.Set(req)
279         xapp.Logger.Debug("%s", e2SubsDelReq.String())
280         err, packedMsg := e2SubsDelReq.Pack(nil)
281         if err != nil {
282                 testError(t, "(xappConn) pack NOK %s", err.Error())
283         }
284
285         params := &xapp.RMRParams{}
286         params.Mtype = xapp.RIC_SUB_DEL_REQ
287         params.SubId = e2SubsId
288         params.Payload = packedMsg.Buf
289         params.Meid = &xapp.RMRMeid{RanName: "RAN_NAME_1"}
290         params.Xid = "XID_1"
291         params.Mbuf = nil
292
293         snderr := xappConn.RmrSend(params)
294         if snderr != nil {
295                 testError(t, "(xappConn) RMR SEND FAILED: %s", snderr.Error())
296         }
297         //}
298 }
299
300 //-----------------------------------------------------------------------------
301 //
302 //-----------------------------------------------------------------------------
303 func handle_e2term_subs_del_req(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *xapp.RMRParams) {
304         xapp.Logger.Info("handle_e2term_subs_del_req start")
305         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
306
307         //---------------------------------
308         // e2term activity: Recv Subs Del Req
309         //---------------------------------
310         select {
311         case msg := <-e2termConn.rmrConChan:
312                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
313                         testError(t, "(e2termConn) Received non RIC_SUB_DEL_REQ message")
314                 } else {
315                         xapp.Logger.Info("(e2termConn) Recv Subs Del Req")
316
317                         packedData := &packer.PackedData{}
318                         packedData.Buf = msg.Payload
319                         unpackerr := e2SubsDelReq.UnPack(packedData)
320                         if unpackerr != nil {
321                                 testError(t, "(e2termConn) RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
322                         }
323                         geterr, req := e2SubsDelReq.Get()
324                         if geterr != nil {
325                                 testError(t, "(e2termConn) RIC_SUB_DEL_REQ get failed err: %s", geterr.Error())
326                         }
327                         return req, msg
328                 }
329         case <-time.After(15 * time.Second):
330                 testError(t, "(e2termConn) Not Received RIC_SUB_DEL_REQ within 15 secs")
331         }
332         return nil, nil
333 }
334
335 func handle_e2term_subs_del_resp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *xapp.RMRParams) {
336         xapp.Logger.Info("handle_e2term_subs_del_resp start")
337         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
338
339         //---------------------------------
340         // e2term activity: Send Subs Del Resp
341         //---------------------------------
342         xapp.Logger.Info("(e2termConn) Send Subs Del Resp")
343         resp := createSubsDelResp(req)
344         e2SubsDelResp.Set(resp)
345         xapp.Logger.Debug("%s", e2SubsDelResp.String())
346         packerr, packedMsg := e2SubsDelResp.Pack(nil)
347         if packerr != nil {
348                 testError(t, "(e2termConn) pack NOK %s", packerr.Error())
349         }
350
351         params := &xapp.RMRParams{}
352         params.Mtype = xapp.RIC_SUB_DEL_RESP
353         params.SubId = msg.SubId
354         params.Payload = packedMsg.Buf
355         params.Meid = msg.Meid
356         params.Xid = msg.Xid
357         params.Mbuf = nil
358
359         snderr := e2termConn.RmrSend(params)
360         if snderr != nil {
361                 testError(t, "(e2termConn) RMR SEND FAILED: %s", snderr.Error())
362         }
363
364 }
365
366 func handle_e2term_subs_del_reqandresp(t *testing.T) {
367         req, msg := handle_e2term_subs_del_req(t)
368         handle_e2term_subs_del_resp(t, req, msg)
369 }
370
371 //-----------------------------------------------------------------------------
372 //
373 //-----------------------------------------------------------------------------
374 func handle_xapp_subs_del_resp(t *testing.T) {
375         xapp.Logger.Info("handle_xapp_subs_del_resp start")
376         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
377
378         //---------------------------------
379         // xapp activity: Recv Subs Del Resp
380         //---------------------------------
381         select {
382         case msg := <-xappConn.rmrConChan:
383                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
384                         testError(t, "(xappConn) Received non RIC_SUB_DEL_RESP message")
385                 } else {
386                         xapp.Logger.Info("(xappConn) Recv Subs Del Resp")
387
388                         packedData := &packer.PackedData{}
389                         packedData.Buf = msg.Payload
390                         unpackerr := e2SubsDelResp.UnPack(packedData)
391                         if unpackerr != nil {
392                                 testError(t, "(xappConn) RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
393                         }
394                         geterr, _ := e2SubsDelResp.Get()
395                         if geterr != nil {
396                                 testError(t, "(xappConn) RIC_SUB_DEL_RESP get failed err: %s", geterr.Error())
397                         }
398
399                 }
400         case <-time.After(15 * time.Second):
401                 testError(t, "(xappConn) Not Received RIC_SUB_DEL_RESP within 15 secs")
402         }
403 }
404
405 //-----------------------------------------------------------------------------
406 //
407 //-----------------------------------------------------------------------------
408 func handle_wait_subs_clean(t *testing.T, e2SubsId int) bool {
409         xapp.Logger.Info("handle_wait_subs_clean start")
410         if mainCtrl.wait_subs_clean(e2SubsId, 10) == false {
411                 testError(t, "(general) no clean within 10 secs")
412                 return false
413         }
414         return true
415 }
416
417 //-----------------------------------------------------------------------------
418 // TestSubReqAndSubDelOk
419 //
420 //   stub                          stub
421 // +-------+     +---------+    +---------+
422 // | xapp  |     | submgr  |    | e2term  |
423 // +-------+     +---------+    +---------+
424 //     |              |              |
425 //     | SubReq       |              |
426 //     |------------->|              |
427 //     |              |              |
428 //     |              | SubReq       |
429 //     |              |------------->|
430 //     |              |              |
431 //     |              |      SubResp |
432 //     |              |<-------------|
433 //     |              |              |
434 //     |      SubResp |              |
435 //     |<-------------|              |
436 //     |              |              |
437 //     |              |              |
438 //     | SubDelReq    |              |
439 //     |------------->|              |
440 //     |              |              |
441 //     |              | SubDelReq    |
442 //     |              |------------->|
443 //     |              |              |
444 //     |              |   SubDelResp |
445 //     |              |<-------------|
446 //     |              |              |
447 //     |   SubDelResp |              |
448 //     |<-------------|              |
449 //
450 //-----------------------------------------------------------------------------
451 func TestSubReqAndSubDelOk(t *testing.T) {
452         xapp.Logger.Info("TestSubReqAndSubDelOk start")
453
454         handle_xapp_subs_req(t)
455         handle_e2term_subs_reqandresp(t)
456         e2SubsId := handle_xapp_subs_resp(t)
457
458         handle_xapp_subs_del_req(t, e2SubsId)
459         handle_e2term_subs_del_reqandresp(t)
460         handle_xapp_subs_del_resp(t)
461
462         //Wait that subs is cleaned
463         handle_wait_subs_clean(t, e2SubsId)
464 }
465
466 //-----------------------------------------------------------------------------
467 // TestSubReqRetransmission
468 //
469 //   stub                          stub
470 // +-------+     +---------+    +---------+
471 // | xapp  |     | submgr  |    | e2term  |
472 // +-------+     +---------+    +---------+
473 //     |              |              |
474 //     |  SubReq      |              |
475 //     |------------->|              |
476 //     |              |              |
477 //     |              | SubReq       |
478 //     |              |------------->|
479 //     |              |              |
480 //     |  SubReq      |              |
481 //     | (retrans)    |              |
482 //     |------------->|              |
483 //     |              |              |
484 //     |              |      SubResp |
485 //     |              |<-------------|
486 //     |              |              |
487 //     |      SubResp |              |
488 //     |<-------------|              |
489 //     |              |              |
490 //     |         [SUBS DELETE]       |
491 //     |              |              |
492 //
493 //-----------------------------------------------------------------------------
494 func TestSubReqRetransmission(t *testing.T) {
495         xapp.Logger.Info("TestSubReqRetransmission start")
496
497         //Subs Create
498         handle_xapp_subs_req(t)
499         req, msg := handle_e2term_subs_req(t)
500         handle_xapp_subs_req(t)
501
502         handle_e2term_subs_resp(t, req, msg)
503
504         e2SubsId := handle_xapp_subs_resp(t)
505
506         //Subs Delete
507         handle_xapp_subs_del_req(t, e2SubsId)
508         handle_e2term_subs_del_reqandresp(t)
509         handle_xapp_subs_del_resp(t)
510
511         //Wait that subs is cleaned
512         handle_wait_subs_clean(t, e2SubsId)
513 }
514
515 //-----------------------------------------------------------------------------
516 // TestSubDelReqRetransmission
517 //
518 //   stub                          stub
519 // +-------+     +---------+    +---------+
520 // | xapp  |     | submgr  |    | e2term  |
521 // +-------+     +---------+    +---------+
522 //     |              |              |
523 //     |         [SUBS CREATE]       |
524 //     |              |              |
525 //     |              |              |
526 //     | SubDelReq    |              |
527 //     |------------->|              |
528 //     |              |              |
529 //     |              | SubDelReq    |
530 //     |              |------------->|
531 //     |              |              |
532 //     | SubDelReq    |              |
533 //     |------------->|              |
534 //     |              |              |
535 //     |              |   SubDelResp |
536 //     |              |<-------------|
537 //     |              |              |
538 //     |   SubDelResp |              |
539 //     |<-------------|              |
540 //
541 //-----------------------------------------------------------------------------
542 func TestSubDelReqRetransmission(t *testing.T) {
543         xapp.Logger.Info("TestSubDelReqRetransmission start")
544
545         //Subs Create
546         handle_xapp_subs_req(t)
547         handle_e2term_subs_reqandresp(t)
548         e2SubsId := handle_xapp_subs_resp(t)
549
550         //Subs Delete
551         handle_xapp_subs_del_req(t, e2SubsId)
552         req, msg := handle_e2term_subs_del_req(t)
553
554         <-time.After(2 * time.Second)
555
556         handle_xapp_subs_del_req(t, e2SubsId)
557
558         handle_e2term_subs_del_resp(t, req, msg)
559
560         //Wait that subs is cleaned
561         handle_wait_subs_clean(t, e2SubsId)
562 }