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