RIC-961:implement Xn and X2 component IDs correctly in E2M
[ric-plt/e2mgr.git] / E2Manager / converters / endc_setup_response_converter.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/e2pdus"
30         "e2mgr/logger"
31         "fmt"
32         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
33         "unsafe"
34 )
35
36 const (
37         maxCellinengNB     = 16384
38         maxofNRNeighbours  = 1024
39         maxnoofNrCellBands = 32
40 )
41
42 type EndcSetupResponseConverter struct {
43         logger *logger.Logger
44 }
45
46 type IEndcSetupResponseConverter interface {
47         UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error)
48 }
49
50 func NewEndcSetupResponseConverter(logger *logger.Logger) *EndcSetupResponseConverter {
51         return &EndcSetupResponseConverter{
52                 logger: logger,
53         }
54 }
55
56 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
57         var info *entities.NrFrequencyInfo
58         info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
59
60         if freqInfo.sULInformation != nil {
61                 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
62
63                 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
64                         info.SulInformation.SulTransmissionBandwidth = value
65                 } else {
66                         return nil, err
67                 }
68         }
69
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)}
75
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))
81                                 }
82                         }
83
84                         info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
85                 }
86         }
87
88         return info, nil
89 }
90
91 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
92         var bw *entities.NrTransmissionBandwidth
93
94         bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
95         bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
96
97         return bw, nil
98 }
99
100 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
101         var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
102
103         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
104                 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
105         } else {
106                 return nil, err
107         }
108
109         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
110                 fddInfo.DlFreqInfo = info
111         } else {
112                 return nil, err
113         }
114
115         if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
116                 fddInfo.UlTransmissionBandwidth = bw
117         } else {
118                 return nil, err
119         }
120
121         if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
122                 fddInfo.DlTransmissionBandwidth = bw
123         } else {
124                 return nil, err
125         }
126
127         return fddInfo, nil
128 }
129
130 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
131         var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
132
133         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
134                 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
135         } else {
136                 return nil, err
137
138         }
139
140         if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
141                 tddInfo.TransmissionBandwidth = bw
142         } else {
143                 return nil, err
144         }
145
146         return tddInfo, nil
147 }
148
149 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
150         var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
151
152         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
153                 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
154         } else {
155                 return nil, err
156         }
157
158         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
159                 fddInfo.DlarFcnFreqInfo = info
160         } else {
161                 return nil, err
162         }
163
164         return fddInfo, nil
165 }
166 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
167         var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
168
169         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
170                 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
171         } else {
172                 return nil, err
173         }
174
175         return tddInfo, nil
176 }
177
178 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
179         var neighbours []*entities.NrNeighbourInformation
180
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)}
186
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)
191
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)))
194
195                         }
196
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)))
199                         }
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
204                                 }
205
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
209                                 }
210                         }
211                         neighbours = append(neighbours, info)
212                 }
213
214         }
215
216         return neighbours, nil
217 }
218
219 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
220         var servedNRCells []*entities.ServedNRCell
221
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)}}
228
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)
233
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)))
236                         }
237
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)))
240                         }
241
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))))
247                                 }
248                         }
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
253                                 } else {
254                                         return nil, err
255                                 }
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
259                                 } else {
260                                         return nil, err
261                                 }
262                         }
263
264                         neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
265                         if err != nil {
266                                 return nil, err
267                         }
268                         servedNRCell.NrNeighbourInfos = neighbours
269
270                         servedNRCells = append(servedNRCells, servedNRCell)
271                 }
272         }
273
274         return servedNRCells, nil
275 }
276
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.
279
280 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
281
282         var gnb *entities.Gnb
283         var globalNbId *entities.GlobalNbId
284
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)
312                                                                                 }
313                                                                         case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
314                                                                                 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
315                                                                                 if err != nil {
316                                                                                         return globalNbId, nil, err
317                                                                                 }
318                                                                                 gnb = &entities.Gnb{}
319                                                                                 gnb.ServedNrCells = servedCells
320                                                                         }
321                                                                 }
322                                                         }
323                                                 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
324                                                         /*ignored*/
325                                                 }
326                                         }
327                                 }
328                         }
329                 }
330         }
331
332         return globalNbId, gnb, nil
333 }
334
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)
337
338         if err != nil {
339                 return nil, nil, err
340         }
341
342         defer C.delete_pdu(pdu)
343         return endcX2SetupResponseToProtobuf(pdu)
344 }