f564c484a24ec9b810fc5838113ec7beb021814a
[ric-plt/submgr.git] / pkg / control / e2ap.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 /*
23 #include <wrapper.h>
24
25 #cgo LDFLAGS: -le2ap_wrapper -le2ap
26 */
27 import "C"
28
29 import (
30         "encoding/hex"
31         "fmt"
32         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
33         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap_wrapper"
34         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
35         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
36 )
37
38 var packerif e2ap.E2APPackerIf = e2ap_wrapper.NewAsn1E2Packer()
39
40 type E2ap struct {
41 }
42
43 //-----------------------------------------------------------------------------
44 //
45 //-----------------------------------------------------------------------------
46 func (c *E2ap) UnpackSubscriptionRequest(payload []byte) (*e2ap.E2APSubscriptionRequest, error) {
47         e2SubReq := packerif.NewPackerSubscriptionRequest()
48         packedData := &packer.PackedData{}
49         packedData.Buf = payload
50         err := e2SubReq.UnPack(packedData)
51         if err != nil {
52                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
53         }
54         err, subReq := e2SubReq.Get()
55         if err != nil {
56                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
57         }
58         return subReq, nil
59 }
60
61 func (c *E2ap) PackSubscriptionRequest(req *e2ap.E2APSubscriptionRequest) (int, *packer.PackedData, error) {
62         e2SubReq := packerif.NewPackerSubscriptionRequest()
63         err := e2SubReq.Set(req)
64         if err != nil {
65                 return 0, nil, err
66         }
67         err, packedData := e2SubReq.Pack(nil)
68         if err != nil {
69                 return 0, nil, err
70         }
71         return xapp.RIC_SUB_REQ, packedData, nil
72 }
73
74 //-----------------------------------------------------------------------------
75 //
76 //-----------------------------------------------------------------------------
77 func (c *E2ap) UnpackSubscriptionResponse(payload []byte) (*e2ap.E2APSubscriptionResponse, error) {
78         e2SubResp := packerif.NewPackerSubscriptionResponse()
79         packedData := &packer.PackedData{}
80         packedData.Buf = payload
81         err := e2SubResp.UnPack(packedData)
82         if err != nil {
83                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
84         }
85         err, subResp := e2SubResp.Get()
86         if err != nil {
87                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
88         }
89         return subResp, nil
90 }
91
92 func (c *E2ap) PackSubscriptionResponse(req *e2ap.E2APSubscriptionResponse) (int, *packer.PackedData, error) {
93         e2SubResp := packerif.NewPackerSubscriptionResponse()
94         err := e2SubResp.Set(req)
95         if err != nil {
96                 return 0, nil, err
97         }
98         err, packedData := e2SubResp.Pack(nil)
99         if err != nil {
100                 return 0, nil, err
101         }
102         return xapp.RIC_SUB_RESP, packedData, nil
103 }
104
105 //-----------------------------------------------------------------------------
106 //
107 //-----------------------------------------------------------------------------
108 func (c *E2ap) UnpackSubscriptionFailure(payload []byte) (*e2ap.E2APSubscriptionFailure, error) {
109         e2SubFail := packerif.NewPackerSubscriptionFailure()
110         packedData := &packer.PackedData{}
111         packedData.Buf = payload
112         err := e2SubFail.UnPack(packedData)
113         if err != nil {
114                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
115         }
116         err, subFail := e2SubFail.Get()
117         if err != nil {
118                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
119         }
120         return subFail, nil
121 }
122
123 func (c *E2ap) PackSubscriptionFailure(req *e2ap.E2APSubscriptionFailure) (int, *packer.PackedData, error) {
124         e2SubFail := packerif.NewPackerSubscriptionFailure()
125         err := e2SubFail.Set(req)
126         if err != nil {
127                 return 0, nil, err
128         }
129         err, packedData := e2SubFail.Pack(nil)
130         if err != nil {
131                 return 0, nil, err
132         }
133         return xapp.RIC_SUB_FAILURE, packedData, nil
134 }
135
136 //-----------------------------------------------------------------------------
137 //
138 //-----------------------------------------------------------------------------
139 func (c *E2ap) UnpackSubscriptionDeleteRequest(payload []byte) (*e2ap.E2APSubscriptionDeleteRequest, error) {
140         e2SubDelReq := packerif.NewPackerSubscriptionDeleteRequest()
141         packedData := &packer.PackedData{}
142         packedData.Buf = payload
143         err := e2SubDelReq.UnPack(packedData)
144         if err != nil {
145                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
146         }
147         err, subDelReq := e2SubDelReq.Get()
148         if err != nil {
149                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
150         }
151         return subDelReq, nil
152 }
153
154 func (c *E2ap) PackSubscriptionDeleteRequest(req *e2ap.E2APSubscriptionDeleteRequest) (int, *packer.PackedData, error) {
155         e2SubDelReq := packerif.NewPackerSubscriptionDeleteRequest()
156         err := e2SubDelReq.Set(req)
157         if err != nil {
158                 return 0, nil, err
159         }
160         err, packedData := e2SubDelReq.Pack(nil)
161         if err != nil {
162                 return 0, nil, err
163         }
164         return xapp.RIC_SUB_DEL_REQ, packedData, nil
165 }
166
167 //-----------------------------------------------------------------------------
168 //
169 //-----------------------------------------------------------------------------
170 func (c *E2ap) UnpackSubscriptionDeleteResponse(payload []byte) (*e2ap.E2APSubscriptionDeleteResponse, error) {
171         e2SubDelResp := packerif.NewPackerSubscriptionDeleteResponse()
172         packedData := &packer.PackedData{}
173         packedData.Buf = payload
174         err := e2SubDelResp.UnPack(packedData)
175         if err != nil {
176                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
177         }
178         err, subDelResp := e2SubDelResp.Get()
179         if err != nil {
180                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
181         }
182         return subDelResp, nil
183 }
184
185 func (c *E2ap) PackSubscriptionDeleteResponse(req *e2ap.E2APSubscriptionDeleteResponse) (int, *packer.PackedData, error) {
186         e2SubDelResp := packerif.NewPackerSubscriptionDeleteResponse()
187         err := e2SubDelResp.Set(req)
188         if err != nil {
189                 return 0, nil, err
190         }
191         err, packedData := e2SubDelResp.Pack(nil)
192         if err != nil {
193                 return 0, nil, err
194         }
195         return xapp.RIC_SUB_DEL_RESP, packedData, nil
196 }
197
198 //-----------------------------------------------------------------------------
199 //
200 //-----------------------------------------------------------------------------
201 func (c *E2ap) UnpackSubscriptionDeleteFailure(payload []byte) (*e2ap.E2APSubscriptionDeleteFailure, error) {
202         e2SubDelFail := packerif.NewPackerSubscriptionDeleteFailure()
203         packedData := &packer.PackedData{}
204         packedData.Buf = payload
205         err := e2SubDelFail.UnPack(packedData)
206         if err != nil {
207                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
208         }
209         err, subDelFail := e2SubDelFail.Get()
210         if err != nil {
211                 return nil, fmt.Errorf("%s buf[%s]", err.Error(), hex.EncodeToString(payload))
212         }
213         return subDelFail, nil
214 }
215
216 func (c *E2ap) PackSubscriptionDeleteFailure(req *e2ap.E2APSubscriptionDeleteFailure) (int, *packer.PackedData, error) {
217         e2SubDelFail := packerif.NewPackerSubscriptionDeleteFailure()
218         err := e2SubDelFail.Set(req)
219         if err != nil {
220                 return 0, nil, err
221         }
222         err, packedData := e2SubDelFail.Pack(nil)
223         if err != nil {
224                 return 0, nil, err
225         }
226         return xapp.RIC_SUB_DEL_FAILURE, packedData, nil
227 }