Add release yaml file for ric-plt-e2mgr:3.0.1
[ric-plt/e2mgr.git] / E2Manager / converters / endc_x2setupResponseToProtobuf.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package converters
22
23 // #cgo CFLAGS: -I../3rdparty/asn1codec/inc/  -I../3rdparty/asn1codec/e2ap_engine/
24 // #cgo LDFLAGS: -L ../3rdparty/asn1codec/lib/ -L../3rdparty/asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
25 // #include <asn1codec_utils.h>
26 // #include <x2setup_response_wrapper.h>
27 import "C"
28 import (
29         "e2mgr/logger"
30         "fmt"
31         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 //      "github.com/pkg/errors"
33         "unsafe"
34 )
35
36 const (
37         maxCellinengNB     = 16384
38         maxofNRNeighbours  = 1024
39         maxnoofNrCellBands = 32
40 )
41
42 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
43         var info *entities.NrFrequencyInfo
44         info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
45
46         if freqInfo.sULInformation != nil {
47                 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
48
49                 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
50                         info.SulInformation.SulTransmissionBandwidth = value
51                 } else {
52                         return nil, err
53                 }
54         }
55
56         if freqInfo.freqBandListNr.list.count > 0 && freqInfo.freqBandListNr.list.count <= maxnoofNrCellBands {
57                 count := int(freqInfo.freqBandListNr.list.count)
58                 freqBandListNr_slice := (*[1 << 30]*C.FreqBandNrItem_t)(unsafe.Pointer(freqInfo.freqBandListNr.list.array))[:count:count]
59                 for _, freqBandNrItem := range freqBandListNr_slice {
60                         frequencyBand := &entities.FrequencyBandItem{NrFrequencyBand: uint32(freqBandNrItem.freqBandIndicatorNr)}
61
62                         if freqBandNrItem.supportedSULBandList.list.count > 0 && freqBandNrItem.supportedSULBandList.list.count <= maxnoofNrCellBands {
63                                 count:= int(freqBandNrItem.supportedSULBandList.list.count)
64                                 supportedSULBandList_slice := (*[1 << 30]*C.SupportedSULFreqBandItem_t)(unsafe.Pointer(freqBandNrItem.supportedSULBandList.list.array))[:count:count]
65                                 for _, supportedSULFreqBandItem := range supportedSULBandList_slice {
66                                         frequencyBand.SupportedSulBands = append(frequencyBand.SupportedSulBands, uint32(supportedSULFreqBandItem.freqBandIndicatorNr))
67                                 }
68                         }
69
70                         info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
71                 }
72         }
73
74         return info, nil
75 }
76
77 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
78         var bw *entities.NrTransmissionBandwidth
79
80         bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
81         bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
82
83         return bw, nil
84 }
85
86 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
87         var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
88
89         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
90                 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
91         } else {
92                 return nil, err
93         }
94
95         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
96                 fddInfo.DlFreqInfo = info
97         } else {
98                 return nil, err
99         }
100
101         if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
102                 fddInfo.UlTransmissionBandwidth = bw
103         } else {
104                 return nil, err
105         }
106
107         if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
108                 fddInfo.DlTransmissionBandwidth = bw
109         } else {
110                 return nil, err
111         }
112
113         return fddInfo, nil
114 }
115
116 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
117         var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
118
119         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
120                 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
121         } else {
122                 return nil, err
123
124         }
125
126         if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
127                 tddInfo.TransmissionBandwidth = bw
128         } else {
129                 return nil, err
130         }
131
132         return tddInfo, nil
133 }
134
135 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
136         var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
137
138         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
139                 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
140         } else {
141                 return nil, err
142         }
143
144         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
145                 fddInfo.DlarFcnFreqInfo = info
146         } else {
147                 return nil, err
148         }
149
150         return fddInfo, nil
151 }
152 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
153         var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
154
155         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
156                 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
157         } else {
158                 return nil, err
159         }
160
161         return tddInfo, nil
162 }
163
164 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
165         var neighbours []*entities.NrNeighbourInformation
166
167         if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxofNRNeighbours {
168                 count:=int(neighbour_Information.list.count)
169                 neighbour_Information_slice := (*[1 << 30]*C.NRNeighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
170                 for _, member := range neighbour_Information_slice {
171                         info := &entities.NrNeighbourInformation{NrPci: uint32(member.nrpCI)}
172
173                         //pLMN_Identity:nRcellIdentifier
174                         plmnId := C.GoBytes(unsafe.Pointer(member.nrCellID.pLMN_Identity.buf), C.int(member.nrCellID.pLMN_Identity.size))
175                         nRcellIdentifier := C.GoBytes(unsafe.Pointer(member.nrCellID.nRcellIdentifier.buf), C.int(member.nrCellID.nRcellIdentifier.size))
176                         info.NrCgi = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
177
178                         if member.fiveGS_TAC != nil {
179                                 info.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.fiveGS_TAC.buf), C.int(member.fiveGS_TAC.size)))
180
181                         }
182
183                         if member.configured_TAC != nil {
184                                 info.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.configured_TAC.buf), C.int(member.configured_TAC.size)))
185                         }
186                         switch member.nRNeighbourModeInfo.present {
187                         case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_fdd:
188                                 if fdd, err := getNRNeighbourInformation_ChoiceNRMode_FDDInfo(*(**C.FDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); fdd != nil && err == nil {
189                                         info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
190                                 }
191
192                         case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_tdd:
193                                 if tdd, err := getNRNeighbourInformation_ChoiceNRMode_TDDInfo(*(**C.TDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); tdd != nil && err == nil {
194                                         info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
195                                 }
196                         }
197                         neighbours = append(neighbours, info)
198                 }
199
200         }
201
202         return neighbours, nil
203 }
204
205 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
206         var servedNRCells []*entities.ServedNRCell
207
208         if servedNRcellsManagementList != nil && servedNRcellsManagementList.list.count > 0 && servedNRcellsManagementList.list.count <= maxCellinengNB {
209                 count :=int(servedNRcellsManagementList.list.count)
210                 servedNRcellsENDCX2ManagementList__Member_slice := (*[1 << 30]*C.ServedNRcellsENDCX2ManagementList__Member)(unsafe.Pointer(servedNRcellsManagementList.list.array))[:count:count]
211                 for _, servedNRcellsENDCX2ManagementList__Member := range servedNRcellsENDCX2ManagementList__Member_slice {
212                         servedNRCellInfo := servedNRcellsENDCX2ManagementList__Member.servedNRCellInfo
213                         servedNRCell := &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{NrPci: uint32(servedNRCellInfo.nrpCI)}}
214
215                         //pLMN_Identity:nRcellIdentifier
216                         plmnId := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.pLMN_Identity.buf), C.int(servedNRCellInfo.nrCellID.pLMN_Identity.size))
217                         nRcellIdentifier := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.nRcellIdentifier.buf), C.int(servedNRCellInfo.nrCellID.nRcellIdentifier.size))
218                         servedNRCell.ServedNrCellInformation.CellId = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
219
220                         if servedNRCellInfo.fiveGS_TAC != nil {
221                                 servedNRCell.ServedNrCellInformation.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.fiveGS_TAC.buf), C.int(servedNRCellInfo.fiveGS_TAC.size)))
222                         }
223
224                         if servedNRCellInfo.configured_TAC != nil {
225                                 servedNRCell.ServedNrCellInformation.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.configured_TAC.buf), C.int(servedNRCellInfo.configured_TAC.size)))
226                         }
227
228                         if servedNRCellInfo.broadcastPLMNs.list.count > 0 && servedNRCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
229                                 count:=int(servedNRCellInfo.broadcastPLMNs.list.count)
230                                 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(servedNRCellInfo.broadcastPLMNs.list.array))[:count:count]
231                                 for _, pLMN_Identity := range pLMN_Identity_slice {
232                                         servedNRCell.ServedNrCellInformation.ServedPlmns = append(servedNRCell.ServedNrCellInformation.ServedPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
233                                 }
234                         }
235                         switch servedNRCellInfo.nrModeInfo.present {
236                         case C.ServedNRCell_Information__nrModeInfo_PR_fdd:
237                                 if fdd, err := getnrModeInfoFDDInfo(*(**C.FDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); fdd != nil && err == nil {
238                                         servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
239                                 } else {
240                                         return nil, err
241                                 }
242                         case C.ServedNRCell_Information__nrModeInfo_PR_tdd:
243                                 if tdd, err := getnrModeInfoTDDInfo(*(**C.TDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); tdd != nil && err == nil {
244                                         servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
245                                 } else {
246                                         return nil, err
247                                 }
248                         }
249
250                         neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
251                         if err != nil {
252                                 return nil, err
253                         }
254                         servedNRCell.NrNeighbourInfos = neighbours
255
256                         servedNRCells = append(servedNRCells, servedNRCell)
257                 }
258         }
259
260         return servedNRCells, nil
261 }
262
263 // Populate  the GNB structure with data from the pdu
264 // Return the GNB and the associated key which can later be used to retrieve the GNB from the database.
265
266 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
267
268         var gnb *entities.Gnb
269         var globalNbId *entities.GlobalNbId
270
271         if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
272                 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
273                 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
274                 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_ENDCX2SetupResponse {
275                         endcX2SetupResponse := (*C.ENDCX2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
276                         if endcX2SetupResponse != nil && endcX2SetupResponse.protocolIEs.list.count > 0 {
277                                 count:=int(endcX2SetupResponse.protocolIEs.list.count)
278                                 endcX2SetupResponse_IEs_slice := (*[1 << 30]*C.ENDCX2SetupResponse_IEs_t)(unsafe.Pointer(endcX2SetupResponse.protocolIEs.list.array))[:count:count]
279                                 for _, endcX2SetupResponse_IE := range endcX2SetupResponse_IEs_slice {
280                                         if endcX2SetupResponse_IE.value.present == C.ENDCX2SetupResponse_IEs__value_PR_RespondingNodeType_EndcX2Setup {
281                                                 respondingNodeType := (*C.RespondingNodeType_EndcX2Setup_t)(unsafe.Pointer(&endcX2SetupResponse_IE.value.choice[0]))
282                                                 switch respondingNodeType.present {
283                                                 case C.RespondingNodeType_EndcX2Setup_PR_respond_en_gNB:
284                                                         en_gNB_ENDCX2SetupReqAckIEs_Container := *(**C.ProtocolIE_Container_119P89_t)(unsafe.Pointer(&respondingNodeType.choice[0]))
285                                                         if en_gNB_ENDCX2SetupReqAckIEs_Container != nil && en_gNB_ENDCX2SetupReqAckIEs_Container.list.count > 0 {
286                                                                 count:=int(en_gNB_ENDCX2SetupReqAckIEs_Container.list.count)
287                                                                 en_gNB_ENDCX2SetupReqAckIEs_slice := (*[1 << 30]*C.En_gNB_ENDCX2SetupReqAckIEs_t)(unsafe.Pointer(en_gNB_ENDCX2SetupReqAckIEs_Container.list.array))[:count:count]
288                                                                 for _, en_gNB_ENDCX2SetupReqAckIE := range en_gNB_ENDCX2SetupReqAckIEs_slice {
289                                                                         switch en_gNB_ENDCX2SetupReqAckIE.value.present {
290                                                                         case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_GlobalGNB_ID:
291                                                                                 globalGNB_ID := (*C.GlobalGNB_ID_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0]))
292                                                                                 plmnId := C.GoBytes(unsafe.Pointer(globalGNB_ID.pLMN_Identity.buf), C.int(globalGNB_ID.pLMN_Identity.size))
293                                                                                 if globalGNB_ID.gNB_ID.present == C.GNB_ID_PR_gNB_ID {
294                                                                                         gnbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&globalGNB_ID.gNB_ID.choice[0]))
295                                                                                         globalNbId = &entities.GlobalNbId{}
296                                                                                         globalNbId.NbId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(gnbIdAsBitString.buf), C.int(gnbIdAsBitString.size)))
297                                                                                         globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
298                                                                                 }
299                                                                         case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
300                                                                                 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
301                                                                                 if err != nil {
302                                                                                         return globalNbId, nil, err
303                                                                                 }
304                                                                                 gnb = &entities.Gnb{}
305                                                                                 gnb.ServedNrCells = servedCells
306                                                                         }
307                                                                 }
308                                                         }
309                                                 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
310                                                         /*ignored*/
311                                                 }
312                                         }
313                                 }
314                         }
315                 }
316         }
317
318         return globalNbId, gnb, nil
319 }
320
321 func UnpackEndcX2SetupResponseAndExtract(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*entities.GlobalNbId, *entities.Gnb, error) {
322         pdu, err := UnpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
323         if err != nil {
324                 return nil, nil, err
325         }
326
327         defer C.delete_pdu(pdu)
328         return endcX2SetupResponseToProtobuf(pdu)
329 }