867564c920ba694f9b2323b15032f549f7e991bd
[scp/ric-app/kpimon.git] / 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 <stdlib.h>
24 #include <e2ap/wrapper.h>
25 #cgo LDFLAGS: -le2apwrapper
26 #cgo CFLAGS: -I/usr/local/include/e2ap
27 */
28 import "C"
29
30 import (
31         "errors"
32         "unsafe"
33 )
34
35 type E2ap struct {
36 }
37
38 /* RICsubscriptionRequest */
39
40 func (c *E2ap) GetSubscriptionRequestSequenceNumber(payload []byte) (subId uint16, err error) {
41         cptr := unsafe.Pointer(&payload[0])
42         cret := C.e2ap_get_ric_subscription_request_sequence_number(cptr, C.size_t(len(payload)))
43         if cret < 0 {
44                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Request Sequence Number due to wrong or invalid payload")
45         }
46         subId = uint16(cret)
47         return
48 }
49
50 func (c *E2ap) SetSubscriptionRequestSequenceNumber(payload []byte, newSubscriptionid uint16) (newPayload []byte, err error) {
51         cptr := unsafe.Pointer(&payload[0])
52         size := C.e2ap_set_ric_subscription_request_sequence_number(cptr, C.size_t(len(payload)), C.long(newSubscriptionid))
53         if size < 0 {
54                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Request Sequence Number due to wrong or invalid payload")
55         }
56         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
57         return
58 }
59
60 func (c *E2ap) SetSubscriptionRequestPayload(payload []byte, ricRequestorID uint16, ricRequestSequenceNumber uint16, ranFunctionID uint16, eventTriggerDefinition []byte, eventTriggerDefinitionSize int, actionCount int, actionIds []int64, actionTypes []int64, actionDefinitions []ActionDefinition, subsequentActions []SubsequentAction) (newPayload []byte, err error) {
61         cptr := unsafe.Pointer(&payload[0])
62         eventTrigger := unsafe.Pointer(&eventTriggerDefinition[0])
63         actIds := unsafe.Pointer(&actionIds[0])
64         actTypes := unsafe.Pointer(&actionTypes[0])
65
66         count := len(actionDefinitions)
67         actDefs := (*C.RICactionDefinition)(C.calloc(C.size_t(len(actionDefinitions)), C.sizeof_RICactionDefinition))
68         for index := 0; index < count; index++ {
69                 ptr := *(*C.RICactionDefinition)(unsafe.Pointer((uintptr)(unsafe.Pointer(actDefs)) + (uintptr)(C.sizeof_RICactionDefinition*C.int(index))))
70                 ptr.size = C.int(actionDefinitions[index].Size)
71                 if ptr.size != 0 {
72                         ptr.actionDefinition = (*C.uint8_t)(C.CBytes(actionDefinitions[index].Buf))
73                 }
74         }
75         defer C.free(unsafe.Pointer(actDefs))
76
77         count = len(subsequentActions)
78         subActs := (*C.RICSubsequentAction)(C.calloc(C.size_t(len(subsequentActions)), C.sizeof_RICSubsequentAction))
79         for index := 0; index < count; index++ {
80                 ptr := *(*C.RICSubsequentAction)(unsafe.Pointer((uintptr)(unsafe.Pointer(subActs)) + (uintptr)(C.sizeof_RICSubsequentAction*C.int(index))))
81                 ptr.isValid = C.int(subsequentActions[index].IsValid)
82                 ptr.subsequentActionType = C.long(subsequentActions[index].SubsequentActionType)
83                 ptr.timeToWait = C.long(subsequentActions[index].TimeToWait)
84         }
85         defer C.free(unsafe.Pointer(subActs))
86
87         size := C.e2ap_encode_ric_subscription_request_message(cptr, C.size_t(len(payload)), C.long(ricRequestorID), C.long(ricRequestSequenceNumber), C.long(ranFunctionID), eventTrigger, C.size_t(eventTriggerDefinitionSize), C.int(actionCount), (*C.long)(actIds), (*C.long)(actTypes), actDefs, subActs)
88         if size < 0 {
89                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Request Payload due to wrong or invalid payload")
90         }
91         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
92         return
93 }
94
95 /* RICsubscriptionResponse */
96
97 func (c *E2ap) GetSubscriptionResponseSequenceNumber(payload []byte) (subId uint16, err error) {
98         cptr := unsafe.Pointer(&payload[0])
99         cret := C.e2ap_get_ric_subscription_response_sequence_number(cptr, C.size_t(len(payload)))
100         if cret < 0 {
101                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Response Sequence Number due to wrong or invalid payload")
102         }
103         subId = uint16(cret)
104         return
105 }
106
107 func (c *E2ap) SetSubscriptionResponseSequenceNumber(payload []byte, newSubscriptionid uint16) (newPayload []byte, err error) {
108         cptr := unsafe.Pointer(&payload[0])
109         size := C.e2ap_set_ric_subscription_response_sequence_number(cptr, C.size_t(len(payload)), C.long(newSubscriptionid))
110         if size < 0 {
111                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Response Sequence Number due to wrong or invalid payload")
112         }
113         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
114         return
115 }
116
117 func (c *E2ap) GetSubscriptionResponseMessage(payload []byte) (decodedMsg *DecodedSubscriptionResponseMessage, err error) {
118         cptr := unsafe.Pointer(&payload[0])
119         decodedMsg = &DecodedSubscriptionResponseMessage{}
120         decodedCMsg := C.e2ap_decode_ric_subscription_response_message(cptr, C.size_t(len(payload)))
121         defer C.free(unsafe.Pointer(decodedCMsg))
122
123         if decodedCMsg == nil {
124                 return decodedMsg, errors.New("e2ap wrapper is unable to decode subscription response message due to wrong or invalid payload")
125         }
126
127         decodedMsg.RequestID = int32(decodedCMsg.requestorID)
128         decodedMsg.RequestSequenceNumber = int32(decodedCMsg.requestSequenceNumber)
129         decodedMsg.FuncID = int32(decodedCMsg.ranfunctionID)
130
131         admittedCount := int(decodedCMsg.ricActionAdmittedList.count)
132         for index := 0; index < admittedCount; index++ {
133                 decodedMsg.ActionAdmittedList.ActionID[index] = int32(decodedCMsg.ricActionAdmittedList.ricActionID[index])
134         }
135         decodedMsg.ActionAdmittedList.Count = admittedCount
136
137         notAdmittedCount := int(decodedCMsg.ricActionNotAdmittedList.count)
138         for index := 0; index < notAdmittedCount; index++ {
139                 decodedMsg.ActionNotAdmittedList.ActionID[index] = int32(decodedCMsg.ricActionNotAdmittedList.ricActionID[index])
140                 decodedMsg.ActionNotAdmittedList.Cause[index].CauseType = int32(decodedCMsg.ricActionNotAdmittedList.ricCause[index].ricCauseType)
141                 decodedMsg.ActionNotAdmittedList.Cause[index].CauseID = int32(decodedCMsg.ricActionNotAdmittedList.ricCause[index].ricCauseID)
142         }
143         decodedMsg.ActionNotAdmittedList.Count = notAdmittedCount
144
145         return
146 }
147
148 /* RICsubscriptionFailure */
149
150 func (c *E2ap) GetSubscriptionFailureSequenceNumber(payload []byte) (subId uint16, err error) {
151         cptr := unsafe.Pointer(&payload[0])
152         cret := C.e2ap_get_ric_subscription_failure_sequence_number(cptr, C.size_t(len(payload)))
153         if cret < 0 {
154                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Failure Sequence Number due to wrong or invalid payload")
155         }
156         subId = uint16(cret)
157         return
158 }
159
160 /* RICsubscriptionDeleteRequest */
161
162 func (c *E2ap) GetSubscriptionDeleteRequestSequenceNumber(payload []byte) (subId uint16, err error) {
163         cptr := unsafe.Pointer(&payload[0])
164         cret := C.e2ap_get_ric_subscription_delete_request_sequence_number(cptr, C.size_t(len(payload)))
165         if cret < 0 {
166                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Delete Request Sequence Number due to wrong or invalid payload")
167         }
168         subId = uint16(cret)
169         return
170 }
171
172 func (c *E2ap) SetSubscriptionDeleteRequestSequenceNumber(payload []byte, newSubscriptionid uint16) (newPayload []byte, err error) {
173         cptr := unsafe.Pointer(&payload[0])
174         size := C.e2ap_set_ric_subscription_delete_request_sequence_number(cptr, C.size_t(len(payload)), C.long(newSubscriptionid))
175         if size < 0 {
176                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Delete Request Sequence Number due to wrong or invalid payload")
177         }
178         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
179         return
180 }
181
182 func (c *E2ap) SetSubscriptionDeleteRequestPayload(payload []byte, ricRequestorID uint16, ricRequestSequenceNumber uint16, ranFunctionID uint16) (newPayload []byte, err error) {
183         cptr := unsafe.Pointer(&payload[0])
184         size := C.e2ap_encode_ric_subscription_delete_request_message(cptr, C.size_t(len(payload)), C.long(ricRequestorID), C.long(ricRequestSequenceNumber), C.long(ranFunctionID))
185         if size < 0 {
186                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Delete Request Payload due to wrong or invalid payload")
187         }
188         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
189         return
190 }
191
192 /* RICsubscriptionDeleteResponse */
193
194 func (c *E2ap) GetSubscriptionDeleteResponseSequenceNumber(payload []byte) (subId uint16, err error) {
195         cptr := unsafe.Pointer(&payload[0])
196         cret := C.e2ap_get_ric_subscription_delete_response_sequence_number(cptr, C.size_t(len(payload)))
197         if cret < 0 {
198                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Delete Response Sequence Number due to wrong or invalid payload")
199         }
200         subId = uint16(cret)
201         return
202 }
203
204 func (c *E2ap) SetSubscriptionDeleteResponseSequenceNumber(payload []byte, newSubscriptionid uint16) (newPayload []byte, err error) {
205         cptr := unsafe.Pointer(&payload[0])
206         size := C.e2ap_set_ric_subscription_delete_response_sequence_number(cptr, C.size_t(len(payload)), C.long(newSubscriptionid))
207         if size < 0 {
208                 return make([]byte, 0), errors.New("e2ap wrapper is unable to set Subscription Delete Response Sequence Number due to wrong or invalid payload")
209         }
210         newPayload = C.GoBytes(cptr, (C.int(size)+7)/8)
211         return
212 }
213
214 /* RICsubscriptionDeleteFailure */
215
216 func (c *E2ap) GetSubscriptionDeleteFailureSequenceNumber(payload []byte) (subId uint16, err error) {
217         cptr := unsafe.Pointer(&payload[0])
218         cret := C.e2ap_get_ric_subscription_delete_failure_sequence_number(cptr, C.size_t(len(payload)))
219         if cret < 0 {
220                 return 0, errors.New("e2ap wrapper is unable to get Subscirption Failure Sequence Number due to wrong or invalid payload")
221         }
222         subId = uint16(cret)
223         return
224 }
225
226 /* RICindication */
227
228 func (c *E2ap) GetIndicationMessage(payload []byte) (decodedMsg *DecodedIndicationMessage, err error) {
229         cptr := unsafe.Pointer(&payload[0])
230         decodedMsg = &DecodedIndicationMessage{}
231         decodedCMsg := C.e2ap_decode_ric_indication_message(cptr, C.size_t(len(payload)))
232         if decodedCMsg == nil {
233                 return decodedMsg, errors.New("e2ap wrapper is unable to decode indication message due to wrong or invalid payload")
234         }
235         defer C.e2ap_free_decoded_ric_indication_message(decodedCMsg)
236
237         decodedMsg.RequestID = int32(decodedCMsg.requestorID)
238         decodedMsg.RequestSequenceNumber = int32(decodedCMsg.requestSequenceNumber)
239         decodedMsg.FuncID = int32(decodedCMsg.ranfunctionID)
240         decodedMsg.ActionID = int32(decodedCMsg.actionID)
241         decodedMsg.IndSN = int32(decodedCMsg.indicationSN)
242         decodedMsg.IndType = int32(decodedCMsg.indicationType)
243         indhdr := unsafe.Pointer(decodedCMsg.indicationHeader)
244         decodedMsg.IndHeader = C.GoBytes(indhdr, C.int(decodedCMsg.indicationHeaderSize))
245         decodedMsg.IndHeaderLength = int32(decodedCMsg.indicationHeaderSize)
246         indmsg := unsafe.Pointer(decodedCMsg.indicationMessage)
247         decodedMsg.IndMessage = C.GoBytes(indmsg, C.int(decodedCMsg.indicationMessageSize))
248         decodedMsg.IndMessageLength = int32(decodedCMsg.indicationMessageSize)
249         callproc := unsafe.Pointer(decodedCMsg.callProcessID)
250         decodedMsg.CallProcessID = C.GoBytes(callproc, C.int(decodedCMsg.callProcessIDSize))
251         decodedMsg.CallProcessIDLength = int32(decodedCMsg.callProcessIDSize)
252         return
253 }