ac72b76da7b755cf6bb1e49ead805c6f451eeefc
[ric-plt/submgr.git] / pkg / control / ut_stub_xapp_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         "strconv"
28         "strings"
29         "testing"
30         "time"
31 )
32
33 //-----------------------------------------------------------------------------
34 //
35 //-----------------------------------------------------------------------------
36 var xapp_e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
37
38 //-----------------------------------------------------------------------------
39 //
40 //-----------------------------------------------------------------------------
41 type xappTransaction struct {
42         tc   *testingXappStub
43         xid  string
44         meid *xapp.RMRMeid
45 }
46
47 type testingXappStub struct {
48         testingRmrStubControl
49         xid_seq uint64
50 }
51
52 //-----------------------------------------------------------------------------
53 //
54 //-----------------------------------------------------------------------------
55 func createNewXappStub(desc string, rtfile string, port string, stat string) *testingXappStub {
56         xappCtrl := &testingXappStub{}
57         xappCtrl.testingRmrStubControl.init(desc, rtfile, port, stat, xappCtrl)
58         xappCtrl.xid_seq = 1
59         return xappCtrl
60 }
61
62 //-----------------------------------------------------------------------------
63 //
64 //-----------------------------------------------------------------------------
65 func (tc *testingXappStub) newXid() string {
66         var xid string
67         xid = tc.desc + "_XID_" + strconv.FormatUint(uint64(tc.xid_seq), 10)
68         tc.xid_seq++
69         return xid
70 }
71
72 //-----------------------------------------------------------------------------
73 //
74 //-----------------------------------------------------------------------------
75 func (tc *testingXappStub) newXappTransaction(xid *string, ranname string) *xappTransaction {
76         trans := &xappTransaction{}
77         trans.tc = tc
78         if xid == nil {
79                 trans.xid = tc.newXid()
80         } else {
81                 trans.xid = *xid
82         }
83         trans.meid = &xapp.RMRMeid{RanName: ranname}
84         return trans
85 }
86
87 //-----------------------------------------------------------------------------
88 //
89 //-----------------------------------------------------------------------------
90 func (tc *testingXappStub) Consume(params *xapp.RMRParams) (err error) {
91         xapp.Rmr.Free(params.Mbuf)
92         params.Mbuf = nil
93         msg := &RMRParams{params}
94
95         if params.Mtype == 55555 {
96                 xapp.Logger.Info("(%s) Testing message ignore %s", tc.desc, msg.String())
97                 tc.active = true
98                 return
99         }
100
101         if strings.Contains(msg.Xid, tc.desc) {
102                 xapp.Logger.Info("(%s) Consume %s", tc.desc, msg.String())
103                 tc.IncMsgCnt()
104                 tc.rmrConChan <- msg
105         } else {
106                 xapp.Logger.Info("(%s) Ignore %s", tc.desc, msg.String())
107         }
108         return
109 }
110
111 //-----------------------------------------------------------------------------
112 //
113 //-----------------------------------------------------------------------------
114 type test_subs_req_params struct {
115         req *e2ap.E2APSubscriptionRequest
116 }
117
118 func (p *test_subs_req_params) Init() {
119         p.req = &e2ap.E2APSubscriptionRequest{}
120
121         p.req.RequestId.Id = 1
122         p.req.RequestId.Seq = 0
123         p.req.FunctionId = 1
124
125         p.req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true
126         p.req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150")
127         p.req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123
128         p.req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28
129
130         // gnb -> enb outgoing
131         // enb -> gnb incoming
132         // X2 36423-f40.doc
133         p.req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming
134         p.req.EventTriggerDefinition.ProcedureCode = 5 //28 35
135         p.req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage
136
137         p.req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
138         p.req.ActionSetups[0].ActionId = 0
139         p.req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
140         p.req.ActionSetups[0].ActionDefinition.Present = false
141         //p.req.ActionSetups[index].ActionDefinition.StyleId = 255
142         //p.req.ActionSetups[index].ActionDefinition.ParamId = 222
143         p.req.ActionSetups[0].SubsequentAction.Present = true
144         p.req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
145         p.req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
146
147 }
148
149 func (xappConn *testingXappStub) handle_xapp_subs_req(t *testing.T, rparams *test_subs_req_params, oldTrans *xappTransaction) *xappTransaction {
150         xapp.Logger.Info("(%s) handle_xapp_subs_req", xappConn.desc)
151         e2SubsReq := xapp_e2asnpacker.NewPackerSubscriptionRequest()
152
153         //---------------------------------
154         // xapp activity: Send Subs Req
155         //---------------------------------
156         xapp.Logger.Info("(%s) Send Subs Req", xappConn.desc)
157
158         myparams := rparams
159
160         if myparams == nil {
161                 myparams = &test_subs_req_params{}
162                 myparams.Init()
163         }
164
165         e2SubsReq.Set(myparams.req)
166         xapp.Logger.Debug("%s", e2SubsReq.String())
167         err, packedMsg := e2SubsReq.Pack(nil)
168         if err != nil {
169                 testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error())
170                 return nil
171         }
172
173         var trans *xappTransaction = oldTrans
174         if trans == nil {
175                 trans = xappConn.newXappTransaction(nil, "RAN_NAME_1")
176         }
177
178         params := &RMRParams{&xapp.RMRParams{}}
179         params.Mtype = xapp.RIC_SUB_REQ
180         params.SubId = -1
181         params.Payload = packedMsg.Buf
182         params.Meid = trans.meid
183         params.Xid = trans.xid
184         params.Mbuf = nil
185
186         snderr := xappConn.RmrSend(params)
187         if snderr != nil {
188                 testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error())
189                 return nil
190         }
191         return trans
192 }
193
194 //-----------------------------------------------------------------------------
195 //
196 //-----------------------------------------------------------------------------
197 func (xappConn *testingXappStub) handle_xapp_subs_resp(t *testing.T, trans *xappTransaction) uint32 {
198         xapp.Logger.Info("(%s) handle_xapp_subs_resp", xappConn.desc)
199         e2SubsResp := xapp_e2asnpacker.NewPackerSubscriptionResponse()
200         var e2SubsId uint32
201
202         //---------------------------------
203         // xapp activity: Recv Subs Resp
204         //---------------------------------
205         select {
206         case msg := <-xappConn.rmrConChan:
207                 xappConn.DecMsgCnt()
208                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
209                         testError(t, "(%s) Received RIC_SUB_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
210                         return 0
211                 } else if msg.Xid != trans.xid {
212                         testError(t, "(%s) Received RIC_SUB_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
213                         return 0
214                 } else {
215                         packedData := &packer.PackedData{}
216                         packedData.Buf = msg.Payload
217                         if msg.SubId > 0 {
218                                 e2SubsId = uint32(msg.SubId)
219                         } else {
220                                 e2SubsId = 0
221                         }
222                         unpackerr := e2SubsResp.UnPack(packedData)
223
224                         if unpackerr != nil {
225                                 testError(t, "(%s) RIC_SUB_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error())
226                         }
227                         geterr, resp := e2SubsResp.Get()
228                         if geterr != nil {
229                                 testError(t, "(%s) RIC_SUB_RESP get failed err: %s", xappConn.desc, geterr.Error())
230                         }
231
232                         xapp.Logger.Info("(%s) Recv Subs Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq)
233                         return e2SubsId
234                 }
235         case <-time.After(15 * time.Second):
236                 testError(t, "(%s) Not Received RIC_SUB_RESP within 15 secs", xappConn.desc)
237                 return 0
238         }
239         return 0
240 }
241
242 //-----------------------------------------------------------------------------
243 //
244 //-----------------------------------------------------------------------------
245 func (xappConn *testingXappStub) handle_xapp_subs_fail(t *testing.T, trans *xappTransaction) uint32 {
246         xapp.Logger.Info("(%s) handle_xapp_subs_fail", xappConn.desc)
247         e2SubsFail := xapp_e2asnpacker.NewPackerSubscriptionFailure()
248         var e2SubsId uint32
249
250         //-------------------------------
251         // xapp activity: Recv Subs Fail
252         //-------------------------------
253         select {
254         case msg := <-xappConn.rmrConChan:
255                 xappConn.DecMsgCnt()
256                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
257                         testError(t, "(%s) Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
258                         return 0
259                 } else if msg.Xid != trans.xid {
260                         testError(t, "(%s) Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
261                         return 0
262                 } else {
263                         packedData := &packer.PackedData{}
264                         packedData.Buf = msg.Payload
265                         if msg.SubId > 0 {
266                                 e2SubsId = uint32(msg.SubId)
267                         } else {
268                                 e2SubsId = 0
269                         }
270                         unpackerr := e2SubsFail.UnPack(packedData)
271
272                         if unpackerr != nil {
273                                 testError(t, "(%s) RIC_SUB_FAILURE unpack failed err: %s", xappConn.desc, unpackerr.Error())
274                         }
275                         geterr, resp := e2SubsFail.Get()
276                         if geterr != nil {
277                                 testError(t, "(%s) RIC_SUB_FAILURE get failed err: %s", xappConn.desc, geterr.Error())
278                         }
279
280                         xapp.Logger.Info("(%s) Recv Subs Fail rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq)
281                         return e2SubsId
282                 }
283         case <-time.After(15 * time.Second):
284                 testError(t, "(%s) Not Received RIC_SUB_FAILURE within 15 secs", xappConn.desc)
285                 return 0
286         }
287         return 0
288 }
289
290 //-----------------------------------------------------------------------------
291 //
292 //-----------------------------------------------------------------------------
293 func (xappConn *testingXappStub) handle_xapp_subs_del_req(t *testing.T, oldTrans *xappTransaction, e2SubsId uint32) *xappTransaction {
294         xapp.Logger.Info("(%s) handle_xapp_subs_del_req", xappConn.desc)
295         e2SubsDelReq := xapp_e2asnpacker.NewPackerSubscriptionDeleteRequest()
296
297         //---------------------------------
298         // xapp activity: Send Subs Del Req
299         //---------------------------------
300         xapp.Logger.Info("(%s) Send Subs Del Req", xappConn.desc)
301
302         req := &e2ap.E2APSubscriptionDeleteRequest{}
303         req.RequestId.Id = 1
304         req.RequestId.Seq = e2SubsId
305         req.FunctionId = 1
306
307         e2SubsDelReq.Set(req)
308         xapp.Logger.Debug("%s", e2SubsDelReq.String())
309         err, packedMsg := e2SubsDelReq.Pack(nil)
310         if err != nil {
311                 testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error())
312                 return nil
313         }
314
315         var trans *xappTransaction = oldTrans
316         if trans == nil {
317                 trans = xappConn.newXappTransaction(nil, "RAN_NAME_1")
318         }
319
320         params := &RMRParams{&xapp.RMRParams{}}
321         params.Mtype = xapp.RIC_SUB_DEL_REQ
322         params.SubId = int(e2SubsId)
323         params.Payload = packedMsg.Buf
324         params.Meid = trans.meid
325         params.Xid = trans.xid
326         params.Mbuf = nil
327
328         snderr := xappConn.RmrSend(params)
329         if snderr != nil {
330                 testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error())
331                 return nil
332         }
333         return trans
334 }
335
336 //-----------------------------------------------------------------------------
337 //
338 //-----------------------------------------------------------------------------
339 func (xappConn *testingXappStub) handle_xapp_subs_del_resp(t *testing.T, trans *xappTransaction) {
340         xapp.Logger.Info("(%s) handle_xapp_subs_del_resp", xappConn.desc)
341         e2SubsDelResp := xapp_e2asnpacker.NewPackerSubscriptionDeleteResponse()
342
343         //---------------------------------
344         // xapp activity: Recv Subs Del Resp
345         //---------------------------------
346         select {
347         case msg := <-xappConn.rmrConChan:
348                 xappConn.DecMsgCnt()
349                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
350                         testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_DEL_RESP", xapp.RicMessageTypeToName[msg.Mtype])
351                         return
352                 } else if trans != nil && msg.Xid != trans.xid {
353                         testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
354                         return
355                 } else {
356                         packedData := &packer.PackedData{}
357                         packedData.Buf = msg.Payload
358                         unpackerr := e2SubsDelResp.UnPack(packedData)
359                         if unpackerr != nil {
360                                 testError(t, "(%s) RIC_SUB_DEL_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error())
361                         }
362                         geterr, resp := e2SubsDelResp.Get()
363                         if geterr != nil {
364                                 testError(t, "(%s) RIC_SUB_DEL_RESP get failed err: %s", xappConn.desc, geterr.Error())
365                         }
366                         xapp.Logger.Info("(%s) Recv Subs Del Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq)
367                         return
368                 }
369         case <-time.After(15 * time.Second):
370                 testError(t, "(%s) Not Received RIC_SUB_DEL_RESP within 15 secs", xappConn.desc)
371         }
372 }