Added E2AP interface wrapper for UT
[ric-plt/submgr.git] / e2ap / pkg / e2ap_wrapper / ut_packer_e2ap.go
1 /*
2 ==================================================================================
3   Copyright (c) 2021 Nokia
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================================
17 */
18
19 package e2ap_wrapper
20
21 import (
22         "fmt"
23
24         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
25 )
26
27 const (
28         SUB_REQ         int = 1
29         SUB_RESP        int = 2
30         SUB_FAILURE     int = 3
31         SUB_DEL_REQ     int = 4
32         SUB_DEL_RESP    int = 5
33         SUB_DEL_FAILURE int = 6
34 )
35
36 //-----------------------------------------------------------------------------
37 //
38 //-----------------------------------------------------------------------------
39
40 var origPackerif e2ap.E2APPackerIf = NewAsn1E2Packer()
41
42 var allowAction = map[int]bool{
43         SUB_REQ:         true,
44         SUB_RESP:        true,
45         SUB_FAILURE:     true,
46         SUB_DEL_REQ:     true,
47         SUB_DEL_RESP:    true,
48         SUB_DEL_FAILURE: true,
49 }
50
51 func AllowE2apToProcess(mtype int, actionFail bool) {
52         fmt.Printf("INFO: AllowE2apToProcess setting %d : %t -> %t", mtype, allowAction[mtype], actionFail)
53         allowAction[mtype] = actionFail
54 }
55
56 type utMsgPackerSubscriptionRequest struct {
57         e2apMsgPackerSubscriptionRequest
58 }
59
60 func (e2apMsg *utMsgPackerSubscriptionRequest) init() {
61 }
62
63 func (e2apMsg *utMsgPackerSubscriptionRequest) Pack(data *e2ap.E2APSubscriptionRequest) (error, *e2ap.PackedData) {
64         if allowAction[SUB_REQ] {
65                 e2sub := origPackerif.NewPackerSubscriptionRequest()
66                 return e2sub.Pack(data)
67         }
68         return fmt.Errorf("Error: Set to be fail by UT (%v)", allowAction[SUB_REQ]), nil
69 }
70
71 func (e2apMsg *utMsgPackerSubscriptionRequest) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionRequest) {
72         if allowAction[SUB_REQ] {
73                 e2sub := origPackerif.NewPackerSubscriptionRequest()
74                 return e2sub.UnPack(msg)
75         }
76         return fmt.Errorf("Error: Set to be fail by UT (%v)", allowAction[SUB_REQ]), nil
77 }
78
79 func (e2apMsg *utMsgPackerSubscriptionRequest) String() string {
80         return "utMsgPackerSubscriptionRequest"
81 }
82
83 //-----------------------------------------------------------------------------
84 //
85 //-----------------------------------------------------------------------------
86 type utMsgPackerSubscriptionResponse struct {
87         e2apMsgPackerSubscriptionResponse
88 }
89
90 func (e2apMsg *utMsgPackerSubscriptionResponse) init() {
91 }
92
93 func (e2apMsg *utMsgPackerSubscriptionResponse) Pack(data *e2ap.E2APSubscriptionResponse) (error, *e2ap.PackedData) {
94         if allowAction[SUB_RESP] {
95                 e2sub := origPackerif.NewPackerSubscriptionResponse()
96                 return e2sub.Pack(data)
97         }
98         return fmt.Errorf("Error: Set to be fail by UT"), nil
99 }
100
101 func (e2apMsg *utMsgPackerSubscriptionResponse) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionResponse) {
102         if allowAction[SUB_RESP] {
103                 e2sub := origPackerif.NewPackerSubscriptionResponse()
104                 return e2sub.UnPack(msg)
105         }
106         return fmt.Errorf("Error: Set to be fail by UT"), nil
107 }
108
109 func (e2apMsg *utMsgPackerSubscriptionResponse) String() string {
110         return "utMsgPackerSubscriptionResponse"
111 }
112
113 //-----------------------------------------------------------------------------
114 //
115 //-----------------------------------------------------------------------------
116 type utMsgPackerSubscriptionFailure struct {
117         e2apMsgPackerSubscriptionFailure
118 }
119
120 func (e2apMsg *utMsgPackerSubscriptionFailure) init() {
121 }
122
123 func (e2apMsg *utMsgPackerSubscriptionFailure) Pack(data *e2ap.E2APSubscriptionFailure) (error, *e2ap.PackedData) {
124         if allowAction[SUB_FAILURE] {
125                 e2sub := origPackerif.NewPackerSubscriptionFailure()
126                 return e2sub.Pack(data)
127         }
128         return fmt.Errorf("Error: Set to be fail by UT"), nil
129 }
130
131 func (e2apMsg *utMsgPackerSubscriptionFailure) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionFailure) {
132         if allowAction[SUB_FAILURE] {
133                 e2sub := origPackerif.NewPackerSubscriptionFailure()
134                 return e2sub.UnPack(msg)
135         }
136         return fmt.Errorf("Error: Set to be fail by UT"), nil
137 }
138
139 func (e2apMsg *utMsgPackerSubscriptionFailure) String() string {
140         return "utMsgPackerSubscriptionFailure"
141 }
142
143 //-----------------------------------------------------------------------------
144 //
145 //-----------------------------------------------------------------------------
146 type utMsgPackerSubscriptionDeleteRequest struct {
147         e2apMsgPackerSubscriptionDeleteRequest
148 }
149
150 func (e2apMsg *utMsgPackerSubscriptionDeleteRequest) init() {
151 }
152
153 func (e2apMsg *utMsgPackerSubscriptionDeleteRequest) Pack(data *e2ap.E2APSubscriptionDeleteRequest) (error, *e2ap.PackedData) {
154         if allowAction[SUB_DEL_REQ] {
155                 e2sub := origPackerif.NewPackerSubscriptionDeleteRequest()
156                 return e2sub.Pack(data)
157         }
158         return fmt.Errorf("Error: Set to be fail by UT (%v)", allowAction[SUB_DEL_REQ]), nil
159 }
160
161 func (e2apMsg *utMsgPackerSubscriptionDeleteRequest) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionDeleteRequest) {
162         if allowAction[SUB_DEL_REQ] {
163                 e2sub := origPackerif.NewPackerSubscriptionDeleteRequest()
164                 return e2sub.UnPack(msg)
165         }
166         return fmt.Errorf("Error: Set to be fail by UT (%v)", allowAction[SUB_DEL_REQ]), nil
167 }
168
169 func (e2apMsg *utMsgPackerSubscriptionDeleteRequest) String() string {
170         return "utMsgPackerSubscriptionDeleteRequest"
171 }
172
173 //-----------------------------------------------------------------------------
174 //
175 //-----------------------------------------------------------------------------
176 type utMsgPackerSubscriptionDeleteResponse struct {
177         e2apMsgPackerSubscriptionDeleteResponse
178 }
179
180 func (e2apMsg *utMsgPackerSubscriptionDeleteResponse) init() {
181
182 }
183
184 func (e2apMsg *utMsgPackerSubscriptionDeleteResponse) Pack(data *e2ap.E2APSubscriptionDeleteResponse) (error, *e2ap.PackedData) {
185         if allowAction[SUB_DEL_RESP] {
186                 e2sub := origPackerif.NewPackerSubscriptionDeleteResponse()
187                 return e2sub.Pack(data)
188         }
189         return fmt.Errorf("Error: Set to be fail by UT"), nil
190 }
191
192 func (e2apMsg *utMsgPackerSubscriptionDeleteResponse) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionDeleteResponse) {
193         if allowAction[SUB_DEL_RESP] {
194                 e2sub := origPackerif.NewPackerSubscriptionDeleteResponse()
195                 return e2sub.UnPack(msg)
196         }
197         return fmt.Errorf("Error: Set to be fail by UT"), nil
198 }
199
200 func (e2apMsg *utMsgPackerSubscriptionDeleteResponse) String() string {
201         return "utMsgPackerSubscriptionDeleteResponse"
202 }
203
204 //-----------------------------------------------------------------------------
205 //
206 //-----------------------------------------------------------------------------
207 type utMsgPackerSubscriptionDeleteFailure struct {
208         e2apMsgPackerSubscriptionDeleteFailure
209 }
210
211 func (e2apMsg *utMsgPackerSubscriptionDeleteFailure) init() {
212 }
213
214 func (e2apMsg *utMsgPackerSubscriptionDeleteFailure) Pack(data *e2ap.E2APSubscriptionDeleteFailure) (error, *e2ap.PackedData) {
215         if allowAction[SUB_DEL_FAILURE] {
216                 e2sub := origPackerif.NewPackerSubscriptionDeleteFailure()
217                 return e2sub.Pack(data)
218         }
219         return fmt.Errorf("Error: Set to be fail by UT"), nil
220 }
221
222 func (e2apMsg *utMsgPackerSubscriptionDeleteFailure) UnPack(msg *e2ap.PackedData) (error, *e2ap.E2APSubscriptionDeleteFailure) {
223         if allowAction[SUB_DEL_FAILURE] {
224                 e2sub := origPackerif.NewPackerSubscriptionDeleteFailure()
225                 return e2sub.UnPack(msg)
226         }
227         return fmt.Errorf("Error: Set to be fail by UT"), nil
228 }
229
230 func (e2apMsg *utMsgPackerSubscriptionDeleteFailure) String() string {
231         return "utMsgPackerSubscriptionDeleteFailure"
232 }
233
234 //-----------------------------------------------------------------------------
235 // Public E2AP packer creators
236 //-----------------------------------------------------------------------------
237
238 type utAsn1E2APPacker struct{}
239
240 func (*utAsn1E2APPacker) NewPackerSubscriptionRequest() e2ap.E2APMsgPackerSubscriptionRequestIf {
241         return &utMsgPackerSubscriptionRequest{}
242 }
243
244 func (*utAsn1E2APPacker) NewPackerSubscriptionResponse() e2ap.E2APMsgPackerSubscriptionResponseIf {
245         return &utMsgPackerSubscriptionResponse{}
246 }
247
248 func (*utAsn1E2APPacker) NewPackerSubscriptionFailure() e2ap.E2APMsgPackerSubscriptionFailureIf {
249         return &utMsgPackerSubscriptionFailure{}
250 }
251
252 func (*utAsn1E2APPacker) NewPackerSubscriptionDeleteRequest() e2ap.E2APMsgPackerSubscriptionDeleteRequestIf {
253         return &utMsgPackerSubscriptionDeleteRequest{}
254 }
255
256 func (*utAsn1E2APPacker) NewPackerSubscriptionDeleteResponse() e2ap.E2APMsgPackerSubscriptionDeleteResponseIf {
257         return &utMsgPackerSubscriptionDeleteResponse{}
258 }
259
260 func (*utAsn1E2APPacker) NewPackerSubscriptionDeleteFailure() e2ap.E2APMsgPackerSubscriptionDeleteFailureIf {
261         return &utMsgPackerSubscriptionDeleteFailure{}
262 }
263
264 func NewUtAsn1E2APPacker() e2ap.E2APPackerIf {
265         return &utAsn1E2APPacker{}
266 }