Restructured test files. stubs locates in own files etc.
[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 func (xappConn *testingXappStub) handle_xapp_subs_req(t *testing.T, oldTrans *xappTransaction) *xappTransaction {
115         xapp.Logger.Info("(%s) handle_xapp_subs_req", xappConn.desc)
116         e2SubsReq := xapp_e2asnpacker.NewPackerSubscriptionRequest()
117
118         //---------------------------------
119         // xapp activity: Send Subs Req
120         //---------------------------------
121         xapp.Logger.Info("(%s) Send Subs Req", xappConn.desc)
122
123         req := &e2ap.E2APSubscriptionRequest{}
124
125         req.RequestId.Id = 1
126         req.RequestId.Seq = 0
127         req.FunctionId = 1
128
129         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present = true
130         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.StringPut("310150")
131         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Id = 123
132         req.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId.Bits = e2ap.E2AP_ENBIDHomeBits28
133
134         // gnb -> enb outgoing
135         // enb -> gnb incoming
136         // X2 36423-f40.doc
137         req.EventTriggerDefinition.InterfaceDirection = e2ap.E2AP_InterfaceDirectionIncoming
138         req.EventTriggerDefinition.ProcedureCode = 5 //28 35
139         req.EventTriggerDefinition.TypeOfMessage = e2ap.E2AP_InitiatingMessage
140
141         req.ActionSetups = make([]e2ap.ActionToBeSetupItem, 1)
142         req.ActionSetups[0].ActionId = 0
143         req.ActionSetups[0].ActionType = e2ap.E2AP_ActionTypeReport
144         req.ActionSetups[0].ActionDefinition.Present = false
145         //req.ActionSetups[index].ActionDefinition.StyleId = 255
146         //req.ActionSetups[index].ActionDefinition.ParamId = 222
147         req.ActionSetups[0].SubsequentAction.Present = true
148         req.ActionSetups[0].SubsequentAction.Type = e2ap.E2AP_SubSeqActionTypeContinue
149         req.ActionSetups[0].SubsequentAction.TimetoWait = e2ap.E2AP_TimeToWaitZero
150
151         e2SubsReq.Set(req)
152         xapp.Logger.Debug("%s", e2SubsReq.String())
153         err, packedMsg := e2SubsReq.Pack(nil)
154         if err != nil {
155                 testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error())
156                 return nil
157         }
158
159         var trans *xappTransaction = oldTrans
160         if trans == nil {
161                 trans = xappConn.newXappTransaction(nil, "RAN_NAME_1")
162         }
163
164         params := &RMRParams{&xapp.RMRParams{}}
165         params.Mtype = xapp.RIC_SUB_REQ
166         params.SubId = -1
167         params.Payload = packedMsg.Buf
168         params.Meid = trans.meid
169         params.Xid = trans.xid
170         params.Mbuf = nil
171
172         snderr := xappConn.RmrSend(params)
173         if snderr != nil {
174                 testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error())
175                 return nil
176         }
177         return trans
178 }
179
180 //-----------------------------------------------------------------------------
181 //
182 //-----------------------------------------------------------------------------
183 func (xappConn *testingXappStub) handle_xapp_subs_resp(t *testing.T, trans *xappTransaction) int {
184         xapp.Logger.Info("(%s) handle_xapp_subs_resp", xappConn.desc)
185         e2SubsResp := xapp_e2asnpacker.NewPackerSubscriptionResponse()
186         var e2SubsId int
187
188         //---------------------------------
189         // xapp activity: Recv Subs Resp
190         //---------------------------------
191         select {
192         case msg := <-xappConn.rmrConChan:
193                 xappConn.DecMsgCnt()
194                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_RESP"] {
195                         testError(t, "(%s) Received RIC_SUB_RESP wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_RESP", xapp.RicMessageTypeToName[msg.Mtype])
196                         return -1
197                 } else if msg.Xid != trans.xid {
198                         testError(t, "(%s) Received RIC_SUB_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
199                         return -1
200                 } else {
201                         packedData := &packer.PackedData{}
202                         packedData.Buf = msg.Payload
203                         e2SubsId = msg.SubId
204                         unpackerr := e2SubsResp.UnPack(packedData)
205
206                         if unpackerr != nil {
207                                 testError(t, "(%s) RIC_SUB_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error())
208                         }
209                         geterr, resp := e2SubsResp.Get()
210                         if geterr != nil {
211                                 testError(t, "(%s) RIC_SUB_RESP get failed err: %s", xappConn.desc, geterr.Error())
212                         }
213
214                         xapp.Logger.Info("(%s) Recv Subs Resp rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq)
215                         return e2SubsId
216                 }
217         case <-time.After(15 * time.Second):
218                 testError(t, "(%s) Not Received RIC_SUB_RESP within 15 secs", xappConn.desc)
219                 return -1
220         }
221         return -1
222 }
223
224 //-----------------------------------------------------------------------------
225 //
226 //-----------------------------------------------------------------------------
227 func (xappConn *testingXappStub) handle_xapp_subs_fail(t *testing.T, trans *xappTransaction) int {
228         xapp.Logger.Info("(%s) handle_xapp_subs_fail", xappConn.desc)
229         e2SubsFail := xapp_e2asnpacker.NewPackerSubscriptionFailure()
230         var e2SubsId int
231
232         //-------------------------------
233         // xapp activity: Recv Subs Fail
234         //-------------------------------
235         select {
236         case msg := <-xappConn.rmrConChan:
237                 xappConn.DecMsgCnt()
238                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_FAILURE"] {
239                         testError(t, "(%s) Received RIC_SUB_FAILURE wrong mtype expected %s got %s, error", xappConn.desc, "RIC_SUB_FAILURE", xapp.RicMessageTypeToName[msg.Mtype])
240                         return -1
241                 } else if msg.Xid != trans.xid {
242                         testError(t, "(%s) Received RIC_SUB_FAILURE wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
243                         return -1
244                 } else {
245                         packedData := &packer.PackedData{}
246                         packedData.Buf = msg.Payload
247                         e2SubsId = msg.SubId
248                         unpackerr := e2SubsFail.UnPack(packedData)
249
250                         if unpackerr != nil {
251                                 testError(t, "(%s) RIC_SUB_FAILURE unpack failed err: %s", xappConn.desc, unpackerr.Error())
252                         }
253                         geterr, resp := e2SubsFail.Get()
254                         if geterr != nil {
255                                 testError(t, "(%s) RIC_SUB_FAILURE get failed err: %s", xappConn.desc, geterr.Error())
256                         }
257
258                         xapp.Logger.Info("(%s) Recv Subs Fail rmr: xid=%s subid=%d, asn: seqnro=%d", xappConn.desc, msg.Xid, msg.SubId, resp.RequestId.Seq)
259                         return e2SubsId
260                 }
261         case <-time.After(15 * time.Second):
262                 testError(t, "(%s) Not Received RIC_SUB_FAILURE within 15 secs", xappConn.desc)
263                 return -1
264         }
265         return -1
266 }
267
268 //-----------------------------------------------------------------------------
269 //
270 //-----------------------------------------------------------------------------
271 func (xappConn *testingXappStub) handle_xapp_subs_del_req(t *testing.T, oldTrans *xappTransaction, e2SubsId int) *xappTransaction {
272         xapp.Logger.Info("(%s) handle_xapp_subs_del_req", xappConn.desc)
273         e2SubsDelReq := xapp_e2asnpacker.NewPackerSubscriptionDeleteRequest()
274
275         //---------------------------------
276         // xapp activity: Send Subs Del Req
277         //---------------------------------
278         xapp.Logger.Info("(%s) Send Subs Del Req", xappConn.desc)
279
280         req := &e2ap.E2APSubscriptionDeleteRequest{}
281         req.RequestId.Id = 1
282         req.RequestId.Seq = uint32(e2SubsId)
283         req.FunctionId = 1
284
285         e2SubsDelReq.Set(req)
286         xapp.Logger.Debug("%s", e2SubsDelReq.String())
287         err, packedMsg := e2SubsDelReq.Pack(nil)
288         if err != nil {
289                 testError(t, "(%s) pack NOK %s", xappConn.desc, err.Error())
290                 return nil
291         }
292
293         var trans *xappTransaction = oldTrans
294         if trans == nil {
295                 trans = xappConn.newXappTransaction(nil, "RAN_NAME_1")
296         }
297
298         params := &RMRParams{&xapp.RMRParams{}}
299         params.Mtype = xapp.RIC_SUB_DEL_REQ
300         params.SubId = e2SubsId
301         params.Payload = packedMsg.Buf
302         params.Meid = trans.meid
303         params.Xid = trans.xid
304         params.Mbuf = nil
305
306         snderr := xappConn.RmrSend(params)
307         if snderr != nil {
308                 testError(t, "(%s) RMR SEND FAILED: %s", xappConn.desc, snderr.Error())
309                 return nil
310         }
311         return trans
312 }
313
314 //-----------------------------------------------------------------------------
315 //
316 //-----------------------------------------------------------------------------
317 func (xappConn *testingXappStub) handle_xapp_subs_del_resp(t *testing.T, trans *xappTransaction) {
318         xapp.Logger.Info("(%s) handle_xapp_subs_del_resp", xappConn.desc)
319         e2SubsDelResp := xapp_e2asnpacker.NewPackerSubscriptionDeleteResponse()
320
321         //---------------------------------
322         // xapp activity: Recv Subs Del Resp
323         //---------------------------------
324         select {
325         case msg := <-xappConn.rmrConChan:
326                 xappConn.DecMsgCnt()
327                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_RESP"] {
328                         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])
329                         return
330                 } else if msg.Xid != trans.xid {
331                         testError(t, "(%s) Received RIC_SUB_DEL_RESP wrong xid expected %s got %s, error", xappConn.desc, trans.xid, msg.Xid)
332                         return
333                 } else {
334                         packedData := &packer.PackedData{}
335                         packedData.Buf = msg.Payload
336                         unpackerr := e2SubsDelResp.UnPack(packedData)
337                         if unpackerr != nil {
338                                 testError(t, "(%s) RIC_SUB_DEL_RESP unpack failed err: %s", xappConn.desc, unpackerr.Error())
339                         }
340                         geterr, resp := e2SubsDelResp.Get()
341                         if geterr != nil {
342                                 testError(t, "(%s) RIC_SUB_DEL_RESP get failed err: %s", xappConn.desc, geterr.Error())
343                         }
344                         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)
345                         return
346                 }
347         case <-time.After(15 * time.Second):
348                 testError(t, "(%s) Not Received RIC_SUB_DEL_RESP within 15 secs", xappConn.desc)
349         }
350 }