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