840c98e68fc87194d0281332f1873fb49d6005dc
[ric-plt/submgr.git] / pkg / control / ut_stub_e2term_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 var e2t_e2asnpacker e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
35
36 //-----------------------------------------------------------------------------
37 //
38 //-----------------------------------------------------------------------------
39 type testingE2termStub struct {
40         testingRmrStubControl
41 }
42
43 //-----------------------------------------------------------------------------
44 //
45 //-----------------------------------------------------------------------------
46 func createNewE2termStub(desc string, rtfile string, port string, stat string) *testingE2termStub {
47         e2termCtrl := &testingE2termStub{}
48         e2termCtrl.testingRmrStubControl.init(desc, rtfile, port, stat, e2termCtrl)
49         return e2termCtrl
50 }
51
52 //-----------------------------------------------------------------------------
53 //
54 //-----------------------------------------------------------------------------
55 func (tc *testingE2termStub) Consume(params *xapp.RMRParams) (err error) {
56         xapp.Rmr.Free(params.Mbuf)
57         params.Mbuf = nil
58         msg := &RMRParams{params}
59
60         if params.Mtype == 55555 {
61                 xapp.Logger.Info("(%s) Testing message ignore %s", tc.GetDesc(), msg.String())
62                 tc.active = true
63                 return
64         }
65
66         xapp.Logger.Info("(%s) Consume %s", tc.GetDesc(), msg.String())
67         tc.rmrConChan <- msg
68         return
69 }
70
71 //-----------------------------------------------------------------------------
72 //
73 //-----------------------------------------------------------------------------
74 func (e2termConn *testingE2termStub) handle_e2term_subs_req(t *testing.T) (*e2ap.E2APSubscriptionRequest, *RMRParams) {
75         xapp.Logger.Info("(%s) handle_e2term_subs_req", e2termConn.GetDesc())
76         e2SubsReq := e2t_e2asnpacker.NewPackerSubscriptionRequest()
77
78         //---------------------------------
79         // e2term activity: Recv Subs Req
80         //---------------------------------
81         select {
82         case msg := <-e2termConn.rmrConChan:
83                 e2termConn.DecMsgCnt()
84                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_REQ"] {
85                         testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.GetDesc(), "RIC_SUB_REQ", xapp.RicMessageTypeToName[msg.Mtype])
86                 } else {
87                         xapp.Logger.Info("(%s) Recv Subs Req", e2termConn.GetDesc())
88                         packedData := &packer.PackedData{}
89                         packedData.Buf = msg.Payload
90                         unpackerr, req := e2SubsReq.UnPack(packedData)
91                         if unpackerr != nil {
92                                 testError(t, "(%s) RIC_SUB_REQ unpack failed err: %s", e2termConn.GetDesc(), unpackerr.Error())
93                         }
94                         return req, msg
95                 }
96         case <-time.After(15 * time.Second):
97                 testError(t, "(%s) Not Received RIC_SUB_REQ within 15 secs", e2termConn.GetDesc())
98         }
99         return nil, nil
100 }
101
102 func (e2termConn *testingE2termStub) handle_e2term_subs_resp(t *testing.T, req *e2ap.E2APSubscriptionRequest, msg *RMRParams) {
103         xapp.Logger.Info("(%s) handle_e2term_subs_resp", e2termConn.GetDesc())
104         e2SubsResp := e2t_e2asnpacker.NewPackerSubscriptionResponse()
105
106         //---------------------------------
107         // e2term activity: Send Subs Resp
108         //---------------------------------
109         xapp.Logger.Info("(%s) Send Subs Resp", e2termConn.GetDesc())
110
111         resp := &e2ap.E2APSubscriptionResponse{}
112
113         resp.RequestId.Id = req.RequestId.Id
114         resp.RequestId.Seq = req.RequestId.Seq
115         resp.FunctionId = req.FunctionId
116
117         resp.ActionAdmittedList.Items = make([]e2ap.ActionAdmittedItem, len(req.ActionSetups))
118         for index := int(0); index < len(req.ActionSetups); index++ {
119                 resp.ActionAdmittedList.Items[index].ActionId = req.ActionSetups[index].ActionId
120         }
121
122         for index := uint64(0); index < 1; index++ {
123                 item := e2ap.ActionNotAdmittedItem{}
124                 item.ActionId = index
125                 item.Cause.Content = 1
126                 item.Cause.CauseVal = 1
127                 resp.ActionNotAdmittedList.Items = append(resp.ActionNotAdmittedList.Items, item)
128         }
129
130         packerr, packedMsg := e2SubsResp.Pack(resp)
131         if packerr != nil {
132                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
133         }
134         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsResp.String())
135
136         params := &RMRParams{&xapp.RMRParams{}}
137         params.Mtype = xapp.RIC_SUB_RESP
138         //params.SubId = msg.SubId
139         params.SubId = -1
140         params.Payload = packedMsg.Buf
141         params.Meid = msg.Meid
142         //params.Xid = msg.Xid
143         params.Mbuf = nil
144
145         snderr := e2termConn.RmrSend(params)
146         if snderr != nil {
147                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
148         }
149 }
150
151 //-----------------------------------------------------------------------------
152 //
153 //-----------------------------------------------------------------------------
154 type test_subs_fail_params struct {
155         req  *e2ap.E2APSubscriptionRequest
156         fail *e2ap.E2APSubscriptionFailure
157 }
158
159 func (p *test_subs_fail_params) Set(req *e2ap.E2APSubscriptionRequest) {
160         p.req = req
161
162         p.fail = &e2ap.E2APSubscriptionFailure{}
163         p.fail.RequestId.Id = p.req.RequestId.Id
164         p.fail.RequestId.Seq = p.req.RequestId.Seq
165         p.fail.FunctionId = p.req.FunctionId
166         p.fail.ActionNotAdmittedList.Items = make([]e2ap.ActionNotAdmittedItem, len(p.req.ActionSetups))
167         for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ {
168                 p.fail.ActionNotAdmittedList.Items[index].ActionId = p.req.ActionSetups[index].ActionId
169                 p.SetCauseVal(index, 5, 1)
170         }
171 }
172
173 func (p *test_subs_fail_params) SetCauseVal(ind int, content uint8, causeval uint8) {
174
175         if ind < 0 {
176                 for index := int(0); index < len(p.fail.ActionNotAdmittedList.Items); index++ {
177                         p.fail.ActionNotAdmittedList.Items[index].Cause.Content = content
178                         p.fail.ActionNotAdmittedList.Items[index].Cause.CauseVal = causeval
179                 }
180                 return
181         }
182         p.fail.ActionNotAdmittedList.Items[ind].Cause.Content = content
183         p.fail.ActionNotAdmittedList.Items[ind].Cause.CauseVal = causeval
184 }
185
186 func (e2termConn *testingE2termStub) handle_e2term_subs_fail(t *testing.T, fparams *test_subs_fail_params, msg *RMRParams) {
187         xapp.Logger.Info("(%s) handle_e2term_subs_fail", e2termConn.GetDesc())
188         e2SubsFail := e2t_e2asnpacker.NewPackerSubscriptionFailure()
189
190         //---------------------------------
191         // e2term activity: Send Subs Fail
192         //---------------------------------
193         xapp.Logger.Info("(%s) Send Subs Fail", e2termConn.GetDesc())
194
195         packerr, packedMsg := e2SubsFail.Pack(fparams.fail)
196         if packerr != nil {
197                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
198         }
199         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsFail.String())
200
201         params := &RMRParams{&xapp.RMRParams{}}
202         params.Mtype = xapp.RIC_SUB_FAILURE
203         params.SubId = msg.SubId
204         params.Payload = packedMsg.Buf
205         params.Meid = msg.Meid
206         params.Xid = msg.Xid
207         params.Mbuf = nil
208
209         snderr := e2termConn.RmrSend(params)
210         if snderr != nil {
211                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
212         }
213 }
214
215 //-----------------------------------------------------------------------------
216 //
217 //-----------------------------------------------------------------------------
218 func (e2termConn *testingE2termStub) handle_e2term_subs_del_req(t *testing.T) (*e2ap.E2APSubscriptionDeleteRequest, *RMRParams) {
219         xapp.Logger.Info("(%s) handle_e2term_subs_del_req", e2termConn.GetDesc())
220         e2SubsDelReq := e2t_e2asnpacker.NewPackerSubscriptionDeleteRequest()
221
222         //---------------------------------
223         // e2term activity: Recv Subs Del Req
224         //---------------------------------
225         select {
226         case msg := <-e2termConn.rmrConChan:
227                 e2termConn.DecMsgCnt()
228                 if msg.Mtype != xapp.RICMessageTypes["RIC_SUB_DEL_REQ"] {
229                         testError(t, "(%s) Received wrong mtype expected %s got %s, error", e2termConn.GetDesc(), "RIC_SUB_DEL_REQ", xapp.RicMessageTypeToName[msg.Mtype])
230                 } else {
231                         xapp.Logger.Info("(%s) Recv Subs Del Req", e2termConn.GetDesc())
232
233                         packedData := &packer.PackedData{}
234                         packedData.Buf = msg.Payload
235                         unpackerr, req := e2SubsDelReq.UnPack(packedData)
236                         if unpackerr != nil {
237                                 testError(t, "(%s) RIC_SUB_DEL_REQ unpack failed err: %s", e2termConn.GetDesc(), unpackerr.Error())
238                         }
239                         return req, msg
240                 }
241         case <-time.After(15 * time.Second):
242                 testError(t, "(%s) Not Received RIC_SUB_DEL_REQ within 15 secs", e2termConn.GetDesc())
243         }
244         return nil, nil
245 }
246
247 func handle_e2term_recv_empty() bool {
248         if len(e2termConn.rmrConChan) > 0 {
249                 return false
250         }
251         return true
252 }
253
254 func (e2termConn *testingE2termStub) handle_e2term_subs_del_resp(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) {
255         xapp.Logger.Info("(%s) handle_e2term_subs_del_resp", e2termConn.GetDesc())
256         e2SubsDelResp := e2t_e2asnpacker.NewPackerSubscriptionDeleteResponse()
257
258         //---------------------------------
259         // e2term activity: Send Subs Del Resp
260         //---------------------------------
261         xapp.Logger.Info("(%s) Send Subs Del Resp", e2termConn.GetDesc())
262
263         resp := &e2ap.E2APSubscriptionDeleteResponse{}
264         resp.RequestId.Id = req.RequestId.Id
265         resp.RequestId.Seq = req.RequestId.Seq
266         resp.FunctionId = req.FunctionId
267
268         packerr, packedMsg := e2SubsDelResp.Pack(resp)
269         if packerr != nil {
270                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
271         }
272         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsDelResp.String())
273
274         params := &RMRParams{&xapp.RMRParams{}}
275         params.Mtype = xapp.RIC_SUB_DEL_RESP
276         params.SubId = msg.SubId
277         params.Payload = packedMsg.Buf
278         params.Meid = msg.Meid
279         params.Xid = msg.Xid
280         params.Mbuf = nil
281
282         snderr := e2termConn.RmrSend(params)
283         if snderr != nil {
284                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
285         }
286 }
287
288 //-----------------------------------------------------------------------------
289 //
290 //-----------------------------------------------------------------------------
291 func (e2termConn *testingE2termStub) handle_e2term_subs_del_fail(t *testing.T, req *e2ap.E2APSubscriptionDeleteRequest, msg *RMRParams) {
292         xapp.Logger.Info("(%s) handle_e2term_del_subs_fail", e2termConn.GetDesc())
293         e2SubsDelFail := e2t_e2asnpacker.NewPackerSubscriptionDeleteFailure()
294
295         //---------------------------------
296         // e2term activity: Send Subs Del Fail
297         //---------------------------------
298         xapp.Logger.Info("(%s) Send Subs Del Fail", e2termConn.GetDesc())
299
300         resp := &e2ap.E2APSubscriptionDeleteFailure{}
301         resp.RequestId.Id = req.RequestId.Id
302         resp.RequestId.Seq = req.RequestId.Seq
303         resp.FunctionId = req.FunctionId
304         resp.Cause.Content = 3  // CauseMisc
305         resp.Cause.CauseVal = 4 // unspecified
306
307         packerr, packedMsg := e2SubsDelFail.Pack(resp)
308         if packerr != nil {
309                 testError(t, "(%s) pack NOK %s", e2termConn.GetDesc(), packerr.Error())
310         }
311         xapp.Logger.Debug("(%s) %s", e2termConn.GetDesc(), e2SubsDelFail.String())
312
313         params := &RMRParams{&xapp.RMRParams{}}
314         params.Mtype = xapp.RIC_SUB_DEL_FAILURE
315         params.SubId = msg.SubId
316         params.Payload = packedMsg.Buf
317         params.Meid = msg.Meid
318         params.Xid = msg.Xid
319         params.Mbuf = nil
320
321         snderr := e2termConn.RmrSend(params)
322         if snderr != nil {
323                 testError(t, "(%s) RMR SEND FAILED: %s", e2termConn.GetDesc(), snderr.Error())
324         }
325 }