2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
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>
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
37 maxCellinengNB = 16384
38 maxofNRNeighbours = 1024
39 maxnoofNrCellBands = 32
42 type EndcSetupResponseConverter struct {
46 type IEndcSetupResponseConverter interface {
47 UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error)
50 func NewEndcSetupResponseConverter(logger *logger.Logger) *EndcSetupResponseConverter {
51 return &EndcSetupResponseConverter{
56 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
57 var info *entities.NrFrequencyInfo
58 info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
60 if freqInfo.sULInformation != nil {
61 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
63 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
64 info.SulInformation.SulTransmissionBandwidth = value
70 if freqInfo.freqBandListNr.list.count > 0 && freqInfo.freqBandListNr.list.count <= maxnoofNrCellBands {
71 count := int(freqInfo.freqBandListNr.list.count)
72 freqBandListNr_slice := (*[1 << 30]*C.FreqBandNrItem_t)(unsafe.Pointer(freqInfo.freqBandListNr.list.array))[:count:count]
73 for _, freqBandNrItem := range freqBandListNr_slice {
74 frequencyBand := &entities.FrequencyBandItem{NrFrequencyBand: uint32(freqBandNrItem.freqBandIndicatorNr)}
76 if freqBandNrItem.supportedSULBandList.list.count > 0 && freqBandNrItem.supportedSULBandList.list.count <= maxnoofNrCellBands {
77 count := int(freqBandNrItem.supportedSULBandList.list.count)
78 supportedSULBandList_slice := (*[1 << 30]*C.SupportedSULFreqBandItem_t)(unsafe.Pointer(freqBandNrItem.supportedSULBandList.list.array))[:count:count]
79 for _, supportedSULFreqBandItem := range supportedSULBandList_slice {
80 frequencyBand.SupportedSulBands = append(frequencyBand.SupportedSulBands, uint32(supportedSULFreqBandItem.freqBandIndicatorNr))
84 info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
91 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
92 var bw *entities.NrTransmissionBandwidth
94 bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
95 bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
100 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
101 var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
103 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
104 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
109 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
110 fddInfo.DlFreqInfo = info
115 if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
116 fddInfo.UlTransmissionBandwidth = bw
121 if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
122 fddInfo.DlTransmissionBandwidth = bw
130 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
131 var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
133 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
134 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
140 if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
141 tddInfo.TransmissionBandwidth = bw
149 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
150 var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
152 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
153 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
158 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
159 fddInfo.DlarFcnFreqInfo = info
166 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
167 var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
169 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
170 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
178 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
179 var neighbours []*entities.NrNeighbourInformation
181 if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxofNRNeighbours {
182 count := int(neighbour_Information.list.count)
183 neighbour_Information_slice := (*[1 << 30]*C.NRNeighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
184 for _, member := range neighbour_Information_slice {
185 info := &entities.NrNeighbourInformation{NrPci: uint32(member.nrpCI)}
187 //pLMN_Identity:nRcellIdentifier
188 plmnId := C.GoBytes(unsafe.Pointer(member.nrCellID.pLMN_Identity.buf), C.int(member.nrCellID.pLMN_Identity.size))
189 nRcellIdentifier := C.GoBytes(unsafe.Pointer(member.nrCellID.nRcellIdentifier.buf), C.int(member.nrCellID.nRcellIdentifier.size))
190 info.NrCgi = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
192 if member.fiveGS_TAC != nil {
193 info.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.fiveGS_TAC.buf), C.int(member.fiveGS_TAC.size)))
197 if member.configured_TAC != nil {
198 info.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.configured_TAC.buf), C.int(member.configured_TAC.size)))
200 switch member.nRNeighbourModeInfo.present {
201 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_fdd:
202 if fdd, err := getNRNeighbourInformation_ChoiceNRMode_FDDInfo(*(**C.FDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); fdd != nil && err == nil {
203 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
206 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_tdd:
207 if tdd, err := getNRNeighbourInformation_ChoiceNRMode_TDDInfo(*(**C.TDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); tdd != nil && err == nil {
208 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
211 neighbours = append(neighbours, info)
216 return neighbours, nil
219 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
220 var servedNRCells []*entities.ServedNRCell
222 if servedNRcellsManagementList != nil && servedNRcellsManagementList.list.count > 0 && servedNRcellsManagementList.list.count <= maxCellinengNB {
223 count := int(servedNRcellsManagementList.list.count)
224 servedNRcellsENDCX2ManagementList__Member_slice := (*[1 << 30]*C.ServedNRcellsENDCX2ManagementList__Member)(unsafe.Pointer(servedNRcellsManagementList.list.array))[:count:count]
225 for _, servedNRcellsENDCX2ManagementList__Member := range servedNRcellsENDCX2ManagementList__Member_slice {
226 servedNRCellInfo := servedNRcellsENDCX2ManagementList__Member.servedNRCellInfo
227 servedNRCell := &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{NrPci: uint32(servedNRCellInfo.nrpCI)}}
229 //pLMN_Identity:nRcellIdentifier
230 plmnId := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.pLMN_Identity.buf), C.int(servedNRCellInfo.nrCellID.pLMN_Identity.size))
231 nRcellIdentifier := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.nRcellIdentifier.buf), C.int(servedNRCellInfo.nrCellID.nRcellIdentifier.size))
232 servedNRCell.ServedNrCellInformation.CellId = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
234 if servedNRCellInfo.fiveGS_TAC != nil {
235 servedNRCell.ServedNrCellInformation.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.fiveGS_TAC.buf), C.int(servedNRCellInfo.fiveGS_TAC.size)))
238 if servedNRCellInfo.configured_TAC != nil {
239 servedNRCell.ServedNrCellInformation.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.configured_TAC.buf), C.int(servedNRCellInfo.configured_TAC.size)))
242 if servedNRCellInfo.broadcastPLMNs.list.count > 0 && servedNRCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
243 count := int(servedNRCellInfo.broadcastPLMNs.list.count)
244 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(servedNRCellInfo.broadcastPLMNs.list.array))[:count:count]
245 for _, pLMN_Identity := range pLMN_Identity_slice {
246 servedNRCell.ServedNrCellInformation.ServedPlmns = append(servedNRCell.ServedNrCellInformation.ServedPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
249 switch servedNRCellInfo.nrModeInfo.present {
250 case C.ServedNRCell_Information__nrModeInfo_PR_fdd:
251 if fdd, err := getnrModeInfoFDDInfo(*(**C.FDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); fdd != nil && err == nil {
252 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
256 case C.ServedNRCell_Information__nrModeInfo_PR_tdd:
257 if tdd, err := getnrModeInfoTDDInfo(*(**C.TDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); tdd != nil && err == nil {
258 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
264 neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
268 servedNRCell.NrNeighbourInfos = neighbours
270 servedNRCells = append(servedNRCells, servedNRCell)
274 return servedNRCells, nil
277 // Populate the GNB structure with data from the pdu
278 // Return the GNB and the associated key which can later be used to retrieve the GNB from the database.
280 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
282 var gnb *entities.Gnb
283 var globalNbId *entities.GlobalNbId
285 if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
286 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
287 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
288 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_ENDCX2SetupResponse {
289 endcX2SetupResponse := (*C.ENDCX2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
290 if endcX2SetupResponse != nil && endcX2SetupResponse.protocolIEs.list.count > 0 {
291 count := int(endcX2SetupResponse.protocolIEs.list.count)
292 endcX2SetupResponse_IEs_slice := (*[1 << 30]*C.ENDCX2SetupResponse_IEs_t)(unsafe.Pointer(endcX2SetupResponse.protocolIEs.list.array))[:count:count]
293 for _, endcX2SetupResponse_IE := range endcX2SetupResponse_IEs_slice {
294 if endcX2SetupResponse_IE.value.present == C.ENDCX2SetupResponse_IEs__value_PR_RespondingNodeType_EndcX2Setup {
295 respondingNodeType := (*C.RespondingNodeType_EndcX2Setup_t)(unsafe.Pointer(&endcX2SetupResponse_IE.value.choice[0]))
296 switch respondingNodeType.present {
297 case C.RespondingNodeType_EndcX2Setup_PR_respond_en_gNB:
298 en_gNB_ENDCX2SetupReqAckIEs_Container := *(**C.ProtocolIE_Container_119P89_t)(unsafe.Pointer(&respondingNodeType.choice[0]))
299 if en_gNB_ENDCX2SetupReqAckIEs_Container != nil && en_gNB_ENDCX2SetupReqAckIEs_Container.list.count > 0 {
300 count := int(en_gNB_ENDCX2SetupReqAckIEs_Container.list.count)
301 en_gNB_ENDCX2SetupReqAckIEs_slice := (*[1 << 30]*C.En_gNB_ENDCX2SetupReqAckIEs_t)(unsafe.Pointer(en_gNB_ENDCX2SetupReqAckIEs_Container.list.array))[:count:count]
302 for _, en_gNB_ENDCX2SetupReqAckIE := range en_gNB_ENDCX2SetupReqAckIEs_slice {
303 switch en_gNB_ENDCX2SetupReqAckIE.value.present {
304 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_GlobalGNB_ID:
305 globalGNB_ID := (*C.GlobalGNB_ID_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0]))
306 plmnId := C.GoBytes(unsafe.Pointer(globalGNB_ID.pLMN_Identity.buf), C.int(globalGNB_ID.pLMN_Identity.size))
307 if globalGNB_ID.gNB_ID.present == C.GNB_ID_PR_gNB_ID {
308 gnbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&globalGNB_ID.gNB_ID.choice[0]))
309 globalNbId = &entities.GlobalNbId{}
310 globalNbId.NbId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(gnbIdAsBitString.buf), C.int(gnbIdAsBitString.size)))
311 globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
313 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
314 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
316 return globalNbId, nil, err
318 gnb = &entities.Gnb{}
319 gnb.ServedNrCells = servedCells
323 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
332 return globalNbId, gnb, nil
335 func (c *EndcSetupResponseConverter) UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error) {
336 pdu, err := UnpackX2apPdu(c.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuf), packedBuf, e2pdus.MaxAsn1CodecMessageBufferSize)
342 defer C.delete_pdu(pdu)
343 return endcX2SetupResponseToProtobuf(pdu)