RICPLT-2957 Clean old test directory. Unittests can handle these cases
[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 // TestSubRequestSubResponseOk
123 //
124 //   stub                          stub
125 // +-------+     +---------+    +---------+
126 // | xapp  |     | submgr  |    | e2term  |
127 // +-------+     +---------+    +---------+
128 //     |              |              |
129 //     | SubReq       |              |
130 //     |------------->|              |
131 //     |              |              |
132 //     |              | SubReq       |
133 //     |              |------------->|
134 //     |              |              |
135 //     |              |      SubResp |
136 //     |              |<-------------|
137 //     |              |              |
138 //     |      SubResp |              |
139 //     |<-------------|              |
140 //     |              |              |
141 //     |              |              |
142 //     | SubDelReq    |              |
143 //     |------------->|              |
144 //     |              |              |
145 //     |              | SubDelReq    |
146 //     |              |------------->|
147 //     |              |              |
148 //     |              |   SubDelResp |
149 //     |              |<-------------|
150 //     |              |              |
151 //     |   SubDelResp |              |
152 //     |<-------------|              |
153 //
154 //-----------------------------------------------------------------------------
155 func TestSubReqAndSubDelOk(t *testing.T) {
156
157         xapp.Logger.Info("TestSubReqAndSubDelOk start")
158         e2SubsReq := e2asnpacker.NewPackerSubscriptionRequest()
159         e2SubsResp := e2asnpacker.NewPackerSubscriptionResponse()
160         e2SubsDelReq := e2asnpacker.NewPackerSubscriptionDeleteRequest()
161         e2SubsDelResp := e2asnpacker.NewPackerSubscriptionDeleteResponse()
162         var e2SubsId int
163
164         //---------------------------------
165         // xapp activity: Send Subs Req
166         //---------------------------------
167         select {
168         case <-time.After(5 * time.Second):
169                 xapp.Logger.Info("(xappConn) Send Subs Req")
170                 req := createSubsReq()
171                 e2SubsReq.Set(req)
172                 xapp.Logger.Debug("%s", e2SubsReq.String())
173                 err, packedMsg := e2SubsReq.Pack(nil)
174                 if err != nil {
175                         testError(t, "(xappConn) pack NOK %s", err.Error())
176                 }
177
178                 params := &xapp.RMRParams{}
179                 params.Mtype = xapp.RIC_SUB_REQ
180                 params.SubId = -1
181                 params.Payload = packedMsg.Buf
182                 params.Meid = &xapp.RMRMeid{RanName: "RAN_NAME_1"}
183                 params.Xid = "XID_1"
184                 params.Mbuf = nil
185
186                 snderr := xappConn.RmrSend(params)
187                 if snderr != nil {
188                         testError(t, "(xappConn) RMR SEND FAILED: %s", snderr.Error())
189                 }
190         }
191
192         //---------------------------------
193         // e2term activity: Recv Subs Req & Send Subs Resp
194         //---------------------------------
195         select {
196         case msg := <-e2termConn.rmrConChan:
197                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
198                         testError(t, "(e2termConn) Received non RIC_SUB_REQ message")
199                 } else {
200                         xapp.Logger.Info("(e2termConn) Recv Subs Req & Send Subs Resp")
201                         packedData := &packer.PackedData{}
202                         packedData.Buf = msg.Payload
203                         unpackerr := e2SubsReq.UnPack(packedData)
204                         if unpackerr != nil {
205                                 testError(t, "(e2termConn) RIC_SUB_REQ unpack failed err: %s", unpackerr.Error())
206                         }
207                         geterr, req := e2SubsReq.Get()
208                         if geterr != nil {
209                                 testError(t, "(e2termConn) RIC_SUB_REQ get failed err: %s", geterr.Error())
210                         }
211
212                         resp := createSubsResp(req)
213                         e2SubsResp.Set(resp)
214                         xapp.Logger.Debug("%s", e2SubsResp.String())
215                         packerr, packedMsg := e2SubsResp.Pack(nil)
216                         if packerr != nil {
217                                 testError(t, "(e2termConn) pack NOK %s", packerr.Error())
218                         }
219
220                         params := &xapp.RMRParams{}
221                         params.Mtype = xapp.RIC_SUB_RESP
222                         params.SubId = msg.SubId
223                         params.Payload = packedMsg.Buf
224                         params.Meid = msg.Meid
225                         params.Xid = msg.Xid
226                         params.Mbuf = nil
227
228                         snderr := e2termConn.RmrSend(params)
229                         if snderr != nil {
230                                 testError(t, "(e2termConn) RMR SEND FAILED: %s", snderr.Error())
231                         }
232
233                 }
234         case <-time.After(15 * time.Second):
235                 testError(t, "(e2termConn) Not Received RIC_SUB_REQ within 15 secs")
236         }
237
238         //---------------------------------
239         // xapp activity: Recv Subs Resp
240         //---------------------------------
241         select {
242         case msg := <-xappConn.rmrConChan:
243                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
244                         testError(t, "(xappConn) Received non RIC_SUB_RESP message")
245                 } else {
246                         xapp.Logger.Info("(xappConn) Recv Subs Resp")
247
248                         packedData := &packer.PackedData{}
249                         packedData.Buf = msg.Payload
250                         e2SubsId = msg.SubId
251                         unpackerr := e2SubsResp.UnPack(packedData)
252
253                         if unpackerr != nil {
254                                 testError(t, "(xappConn) RIC_SUB_RESP unpack failed err: %s", unpackerr.Error())
255                         }
256                         geterr, _ := e2SubsResp.Get()
257                         if geterr != nil {
258                                 testError(t, "(xappConn) RIC_SUB_RESP get failed err: %s", geterr.Error())
259                         }
260
261                 }
262         case <-time.After(15 * time.Second):
263                 testError(t, "(xappConn) Not Received RIC_SUB_RESP within 15 secs")
264         }
265
266         //---------------------------------
267         // xapp activity: Send Subs Del Req
268         //---------------------------------
269         select {
270         case <-time.After(2 * time.Second):
271                 xapp.Logger.Info("(xappConn) Send Subs Del Req")
272                 req := createSubsDelReq(uint32(e2SubsId))
273                 e2SubsDelReq.Set(req)
274                 xapp.Logger.Debug("%s", e2SubsDelReq.String())
275                 err, packedMsg := e2SubsDelReq.Pack(nil)
276                 if err != nil {
277                         testError(t, "(xappConn) pack NOK %s", err.Error())
278                 }
279
280                 params := &xapp.RMRParams{}
281                 params.Mtype = xapp.RIC_SUB_DEL_REQ
282                 params.SubId = e2SubsId
283                 params.Payload = packedMsg.Buf
284                 params.Meid = &xapp.RMRMeid{RanName: "RAN_NAME_1"}
285                 params.Xid = "XID_1"
286                 params.Mbuf = nil
287
288                 snderr := xappConn.RmrSend(params)
289                 if snderr != nil {
290                         testError(t, "(xappConn) RMR SEND FAILED: %s", snderr.Error())
291                 }
292         }
293
294         //---------------------------------
295         // e2term activity: Recv Subs Del Req & Send Subs Del Resp
296         //---------------------------------
297         select {
298         case msg := <-e2termConn.rmrConChan:
299                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
300                         testError(t, "(e2termConn) Received non RIC_SUB_DEL_REQ message")
301                 } else {
302                         xapp.Logger.Info("(e2termConn) Recv Subs Del Req & Send Subs Del Resp")
303
304                         packedData := &packer.PackedData{}
305                         packedData.Buf = msg.Payload
306                         unpackerr := e2SubsDelReq.UnPack(packedData)
307                         if unpackerr != nil {
308                                 testError(t, "(e2termConn) RIC_SUB_DEL_REQ unpack failed err: %s", unpackerr.Error())
309                         }
310                         geterr, req := e2SubsDelReq.Get()
311                         if geterr != nil {
312                                 testError(t, "(e2termConn) RIC_SUB_DEL_REQ get failed err: %s", geterr.Error())
313                         }
314
315                         resp := createSubsDelResp(req)
316                         e2SubsDelResp.Set(resp)
317                         xapp.Logger.Debug("%s", e2SubsDelResp.String())
318                         packerr, packedMsg := e2SubsDelResp.Pack(nil)
319                         if packerr != nil {
320                                 testError(t, "(e2termConn) pack NOK %s", packerr.Error())
321                         }
322
323                         params := &xapp.RMRParams{}
324                         params.Mtype = xapp.RIC_SUB_DEL_RESP
325                         params.SubId = msg.SubId
326                         params.Payload = packedMsg.Buf
327                         params.Meid = msg.Meid
328                         params.Xid = msg.Xid
329                         params.Mbuf = nil
330
331                         snderr := e2termConn.RmrSend(params)
332                         if snderr != nil {
333                                 testError(t, "(e2termConn) RMR SEND FAILED: %s", snderr.Error())
334                         }
335
336                 }
337         case <-time.After(15 * time.Second):
338                 testError(t, "(e2termConn) Not Received RIC_SUB_DEL_REQ within 15 secs")
339         }
340
341         //---------------------------------
342         // xapp activity: Recv Subs Del Resp
343         //---------------------------------
344         select {
345         case msg := <-xappConn.rmrConChan:
346                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
347                         testError(t, "(xappConn) Received non RIC_SUB_DEL_RESP message")
348                 } else {
349                         xapp.Logger.Info("(xappConn) Recv Subs Del Resp")
350
351                         packedData := &packer.PackedData{}
352                         packedData.Buf = msg.Payload
353                         unpackerr := e2SubsDelResp.UnPack(packedData)
354                         if unpackerr != nil {
355                                 testError(t, "(xappConn) RIC_SUB_DEL_RESP unpack failed err: %s", unpackerr.Error())
356                         }
357                         geterr, _ := e2SubsDelResp.Get()
358                         if geterr != nil {
359                                 testError(t, "(xappConn) RIC_SUB_DEL_RESP get failed err: %s", geterr.Error())
360                         }
361
362                 }
363         case <-time.After(15 * time.Second):
364                 testError(t, "(xappConn) Not Received RIC_SUB_DEL_RESP within 15 secs")
365         }
366
367 }