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.
20 // #cgo CFLAGS: -I../asn1codec/inc/ -I../asn1codec/e2ap_engine/
21 // #cgo LDFLAGS: -L ../asn1codec/lib/ -L../asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
22 // #include <asn1codec_utils.h>
23 // #include <x2setup_response_wrapper.h>
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
34 maxCellinengNB = 16384
35 maxofNRNeighbours = 1024
36 maxnoofNrCellBands = 32
39 type EndcSetupResponseConverter struct {
43 type IEndcSetupResponseConverter interface {
44 UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error)
47 func NewEndcSetupResponseConverter(logger *logger.Logger) *EndcSetupResponseConverter {
48 return &EndcSetupResponseConverter{
53 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
54 var info *entities.NrFrequencyInfo
55 info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
57 if freqInfo.sULInformation != nil {
58 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
60 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
61 info.SulInformation.SulTransmissionBandwidth = value
67 if freqInfo.freqBandListNr.list.count > 0 && freqInfo.freqBandListNr.list.count <= maxnoofNrCellBands {
68 count := int(freqInfo.freqBandListNr.list.count)
69 freqBandListNr_slice := (*[1 << 30]*C.FreqBandNrItem_t)(unsafe.Pointer(freqInfo.freqBandListNr.list.array))[:count:count]
70 for _, freqBandNrItem := range freqBandListNr_slice {
71 frequencyBand := &entities.FrequencyBandItem{NrFrequencyBand: uint32(freqBandNrItem.freqBandIndicatorNr)}
73 if freqBandNrItem.supportedSULBandList.list.count > 0 && freqBandNrItem.supportedSULBandList.list.count <= maxnoofNrCellBands {
74 count := int(freqBandNrItem.supportedSULBandList.list.count)
75 supportedSULBandList_slice := (*[1 << 30]*C.SupportedSULFreqBandItem_t)(unsafe.Pointer(freqBandNrItem.supportedSULBandList.list.array))[:count:count]
76 for _, supportedSULFreqBandItem := range supportedSULBandList_slice {
77 frequencyBand.SupportedSulBands = append(frequencyBand.SupportedSulBands, uint32(supportedSULFreqBandItem.freqBandIndicatorNr))
81 info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
88 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
89 var bw *entities.NrTransmissionBandwidth
91 bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
92 bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
97 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
98 var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
100 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
101 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
106 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
107 fddInfo.DlFreqInfo = info
112 if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
113 fddInfo.UlTransmissionBandwidth = bw
118 if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
119 fddInfo.DlTransmissionBandwidth = bw
127 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
128 var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
130 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
131 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
137 if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
138 tddInfo.TransmissionBandwidth = bw
146 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
147 var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
149 if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
150 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
155 if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
156 fddInfo.DlarFcnFreqInfo = info
163 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
164 var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
166 if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
167 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
175 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
176 var neighbours []*entities.NrNeighbourInformation
178 if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxofNRNeighbours {
179 count := int(neighbour_Information.list.count)
180 neighbour_Information_slice := (*[1 << 30]*C.NRNeighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
181 for _, member := range neighbour_Information_slice {
182 info := &entities.NrNeighbourInformation{NrPci: uint32(member.nrpCI)}
184 //pLMN_Identity:nRcellIdentifier
185 plmnId := C.GoBytes(unsafe.Pointer(member.nrCellID.pLMN_Identity.buf), C.int(member.nrCellID.pLMN_Identity.size))
186 nRcellIdentifier := C.GoBytes(unsafe.Pointer(member.nrCellID.nRcellIdentifier.buf), C.int(member.nrCellID.nRcellIdentifier.size))
187 info.NrCgi = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
189 if member.fiveGS_TAC != nil {
190 info.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.fiveGS_TAC.buf), C.int(member.fiveGS_TAC.size)))
194 if member.configured_TAC != nil {
195 info.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.configured_TAC.buf), C.int(member.configured_TAC.size)))
197 switch member.nRNeighbourModeInfo.present {
198 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_fdd:
199 if fdd, err := getNRNeighbourInformation_ChoiceNRMode_FDDInfo(*(**C.FDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); fdd != nil && err == nil {
200 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
203 case C.NRNeighbour_Information__Member__nRNeighbourModeInfo_PR_tdd:
204 if tdd, err := getNRNeighbourInformation_ChoiceNRMode_TDDInfo(*(**C.TDD_InfoNeighbourServedNRCell_Information_t)(unsafe.Pointer(&member.nRNeighbourModeInfo.choice[0]))); tdd != nil && err == nil {
205 info.ChoiceNrMode, info.NrMode = &entities.NrNeighbourInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
208 neighbours = append(neighbours, info)
213 return neighbours, nil
216 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
217 var servedNRCells []*entities.ServedNRCell
219 if servedNRcellsManagementList != nil && servedNRcellsManagementList.list.count > 0 && servedNRcellsManagementList.list.count <= maxCellinengNB {
220 count := int(servedNRcellsManagementList.list.count)
221 servedNRcellsENDCX2ManagementList__Member_slice := (*[1 << 30]*C.ServedNRcellsENDCX2ManagementList__Member)(unsafe.Pointer(servedNRcellsManagementList.list.array))[:count:count]
222 for _, servedNRcellsENDCX2ManagementList__Member := range servedNRcellsENDCX2ManagementList__Member_slice {
223 servedNRCellInfo := servedNRcellsENDCX2ManagementList__Member.servedNRCellInfo
224 servedNRCell := &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{NrPci: uint32(servedNRCellInfo.nrpCI)}}
226 //pLMN_Identity:nRcellIdentifier
227 plmnId := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.pLMN_Identity.buf), C.int(servedNRCellInfo.nrCellID.pLMN_Identity.size))
228 nRcellIdentifier := C.GoBytes(unsafe.Pointer(servedNRCellInfo.nrCellID.nRcellIdentifier.buf), C.int(servedNRCellInfo.nrCellID.nRcellIdentifier.size))
229 servedNRCell.ServedNrCellInformation.CellId = fmt.Sprintf("%02x:%02x", plmnId, nRcellIdentifier)
231 if servedNRCellInfo.fiveGS_TAC != nil {
232 servedNRCell.ServedNrCellInformation.Stac5G = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.fiveGS_TAC.buf), C.int(servedNRCellInfo.fiveGS_TAC.size)))
235 if servedNRCellInfo.configured_TAC != nil {
236 servedNRCell.ServedNrCellInformation.ConfiguredStac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(servedNRCellInfo.configured_TAC.buf), C.int(servedNRCellInfo.configured_TAC.size)))
239 if servedNRCellInfo.broadcastPLMNs.list.count > 0 && servedNRCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
240 count := int(servedNRCellInfo.broadcastPLMNs.list.count)
241 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(servedNRCellInfo.broadcastPLMNs.list.array))[:count:count]
242 for _, pLMN_Identity := range pLMN_Identity_slice {
243 servedNRCell.ServedNrCellInformation.ServedPlmns = append(servedNRCell.ServedNrCellInformation.ServedPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
246 switch servedNRCellInfo.nrModeInfo.present {
247 case C.ServedNRCell_Information__nrModeInfo_PR_fdd:
248 if fdd, err := getnrModeInfoFDDInfo(*(**C.FDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); fdd != nil && err == nil {
249 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Fdd: fdd}, entities.Nr_FDD
253 case C.ServedNRCell_Information__nrModeInfo_PR_tdd:
254 if tdd, err := getnrModeInfoTDDInfo(*(**C.TDD_InfoServedNRCell_Information_t)(unsafe.Pointer(&servedNRCellInfo.nrModeInfo.choice[0]))); tdd != nil && err == nil {
255 servedNRCell.ServedNrCellInformation.ChoiceNrMode, servedNRCell.ServedNrCellInformation.NrMode = &entities.ServedNRCellInformation_ChoiceNRMode{Tdd: tdd}, entities.Nr_TDD
261 neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
265 servedNRCell.NrNeighbourInfos = neighbours
267 servedNRCells = append(servedNRCells, servedNRCell)
271 return servedNRCells, nil
274 // Populate the GNB structure with data from the pdu
275 // Return the GNB and the associated key which can later be used to retrieve the GNB from the database.
277 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
279 var gnb *entities.Gnb
280 var globalNbId *entities.GlobalNbId
282 if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
283 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
284 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
285 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_ENDCX2SetupResponse {
286 endcX2SetupResponse := (*C.ENDCX2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
287 if endcX2SetupResponse != nil && endcX2SetupResponse.protocolIEs.list.count > 0 {
288 count := int(endcX2SetupResponse.protocolIEs.list.count)
289 endcX2SetupResponse_IEs_slice := (*[1 << 30]*C.ENDCX2SetupResponse_IEs_t)(unsafe.Pointer(endcX2SetupResponse.protocolIEs.list.array))[:count:count]
290 for _, endcX2SetupResponse_IE := range endcX2SetupResponse_IEs_slice {
291 if endcX2SetupResponse_IE.value.present == C.ENDCX2SetupResponse_IEs__value_PR_RespondingNodeType_EndcX2Setup {
292 respondingNodeType := (*C.RespondingNodeType_EndcX2Setup_t)(unsafe.Pointer(&endcX2SetupResponse_IE.value.choice[0]))
293 switch respondingNodeType.present {
294 case C.RespondingNodeType_EndcX2Setup_PR_respond_en_gNB:
295 en_gNB_ENDCX2SetupReqAckIEs_Container := *(**C.ProtocolIE_Container_119P89_t)(unsafe.Pointer(&respondingNodeType.choice[0]))
296 if en_gNB_ENDCX2SetupReqAckIEs_Container != nil && en_gNB_ENDCX2SetupReqAckIEs_Container.list.count > 0 {
297 count := int(en_gNB_ENDCX2SetupReqAckIEs_Container.list.count)
298 en_gNB_ENDCX2SetupReqAckIEs_slice := (*[1 << 30]*C.En_gNB_ENDCX2SetupReqAckIEs_t)(unsafe.Pointer(en_gNB_ENDCX2SetupReqAckIEs_Container.list.array))[:count:count]
299 for _, en_gNB_ENDCX2SetupReqAckIE := range en_gNB_ENDCX2SetupReqAckIEs_slice {
300 switch en_gNB_ENDCX2SetupReqAckIE.value.present {
301 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_GlobalGNB_ID:
302 globalGNB_ID := (*C.GlobalGNB_ID_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0]))
303 plmnId := C.GoBytes(unsafe.Pointer(globalGNB_ID.pLMN_Identity.buf), C.int(globalGNB_ID.pLMN_Identity.size))
304 if globalGNB_ID.gNB_ID.present == C.GNB_ID_PR_gNB_ID {
305 gnbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&globalGNB_ID.gNB_ID.choice[0]))
306 globalNbId = &entities.GlobalNbId{}
307 globalNbId.NbId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(gnbIdAsBitString.buf), C.int(gnbIdAsBitString.size)))
308 globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
310 case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
311 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
313 return globalNbId, nil, err
315 gnb = &entities.Gnb{}
316 gnb.ServedNrCells = servedCells
320 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
329 return globalNbId, gnb, nil
332 func (c *EndcSetupResponseConverter) UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error) {
333 pdu, err := UnpackX2apPdu(c.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuf), packedBuf, e2pdus.MaxAsn1CodecMessageBufferSize)
339 defer C.delete_pdu(pdu)
340 return endcX2SetupResponseToProtobuf(pdu)