sync R3 content from Azure
[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
18 package converters
19
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>
24 import "C"
25 import (
26         "e2mgr/e2pdus"
27         "e2mgr/logger"
28         "fmt"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30         "unsafe"
31 )
32
33 const (
34         maxCellinengNB     = 16384
35         maxofNRNeighbours  = 1024
36         maxnoofNrCellBands = 32
37 )
38
39 type EndcSetupResponseConverter struct {
40         logger *logger.Logger
41 }
42
43 type IEndcSetupResponseConverter interface {
44         UnpackEndcSetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Gnb, error)
45 }
46
47 func NewEndcSetupResponseConverter(logger *logger.Logger) *EndcSetupResponseConverter {
48         return &EndcSetupResponseConverter{
49                 logger: logger,
50         }
51 }
52
53 func getNRFreqInfo(freqInfo C.NRFreqInfo_t) (*entities.NrFrequencyInfo, error) {
54         var info *entities.NrFrequencyInfo
55         info = &entities.NrFrequencyInfo{NrArFcn: uint64(freqInfo.nRARFCN)}
56
57         if freqInfo.sULInformation != nil {
58                 info.SulInformation = &entities.NrFrequencyInfo_SulInformation{SulArFcn: uint64((*C.SULInformation_t)(freqInfo.sULInformation).sUL_ARFCN)}
59
60                 if value, err := getNR_TxBW((*C.SULInformation_t)(freqInfo.sULInformation).sUL_TxBW); err == nil {
61                         info.SulInformation.SulTransmissionBandwidth = value
62                 } else {
63                         return nil, err
64                 }
65         }
66
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)}
72
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))
78                                 }
79                         }
80
81                         info.FrequencyBands = append(info.FrequencyBands, frequencyBand)
82                 }
83         }
84
85         return info, nil
86 }
87
88 func getNR_TxBW(txBW C.NR_TxBW_t) (*entities.NrTransmissionBandwidth, error) {
89         var bw *entities.NrTransmissionBandwidth
90
91         bw = &entities.NrTransmissionBandwidth{Nrscs: entities.Nrscs(1 + int64(txBW.nRSCS))}
92         bw.Ncnrb = entities.Ncnrb(1 + int64(txBW.nRNRB))
93
94         return bw, nil
95 }
96
97 func getnrModeInfoFDDInfo(fdd *C.FDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_FddInfo, error) {
98         var fddInfo *entities.ServedNRCellInformation_ChoiceNRMode_FddInfo
99
100         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
101                 fddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{UlFreqInfo: info}
102         } else {
103                 return nil, err
104         }
105
106         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
107                 fddInfo.DlFreqInfo = info
108         } else {
109                 return nil, err
110         }
111
112         if bw, err := getNR_TxBW(fdd.ul_NR_TxBW); err == nil {
113                 fddInfo.UlTransmissionBandwidth = bw
114         } else {
115                 return nil, err
116         }
117
118         if bw, err := getNR_TxBW(fdd.dl_NR_TxBW); err == nil {
119                 fddInfo.DlTransmissionBandwidth = bw
120         } else {
121                 return nil, err
122         }
123
124         return fddInfo, nil
125 }
126
127 func getnrModeInfoTDDInfo(tdd *C.TDD_InfoServedNRCell_Information_t) (*entities.ServedNRCellInformation_ChoiceNRMode_TddInfo, error) {
128         var tddInfo *entities.ServedNRCellInformation_ChoiceNRMode_TddInfo
129
130         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
131                 tddInfo = &entities.ServedNRCellInformation_ChoiceNRMode_TddInfo{NrFreqInfo: info}
132         } else {
133                 return nil, err
134
135         }
136
137         if bw, err := getNR_TxBW(tdd.nR_TxBW); err == nil {
138                 tddInfo.TransmissionBandwidth = bw
139         } else {
140                 return nil, err
141         }
142
143         return tddInfo, nil
144 }
145
146 func getNRNeighbourInformation_ChoiceNRMode_FDDInfo(fdd *C.FDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_FddInfo, error) {
147         var fddInfo *entities.NrNeighbourInformation_ChoiceNRMode_FddInfo
148
149         if info, err := getNRFreqInfo(fdd.ul_NRFreqInfo); err == nil {
150                 fddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_FddInfo{UlarFcnFreqInfo: info}
151         } else {
152                 return nil, err
153         }
154
155         if info, err := getNRFreqInfo(fdd.dl_NRFreqInfo); err == nil {
156                 fddInfo.DlarFcnFreqInfo = info
157         } else {
158                 return nil, err
159         }
160
161         return fddInfo, nil
162 }
163 func getNRNeighbourInformation_ChoiceNRMode_TDDInfo(tdd *C.TDD_InfoNeighbourServedNRCell_Information_t) (*entities.NrNeighbourInformation_ChoiceNRMode_TddInfo, error) {
164         var tddInfo *entities.NrNeighbourInformation_ChoiceNRMode_TddInfo
165
166         if info, err := getNRFreqInfo(tdd.nRFreqInfo); err == nil {
167                 tddInfo = &entities.NrNeighbourInformation_ChoiceNRMode_TddInfo{ArFcnNrFreqInfo: info}
168         } else {
169                 return nil, err
170         }
171
172         return tddInfo, nil
173 }
174
175 func getnRNeighbourInfo(neighbour_Information *C.NRNeighbour_Information_t) ([]*entities.NrNeighbourInformation, error) {
176         var neighbours []*entities.NrNeighbourInformation
177
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)}
183
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)
188
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)))
191
192                         }
193
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)))
196                         }
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
201                                 }
202
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
206                                 }
207                         }
208                         neighbours = append(neighbours, info)
209                 }
210
211         }
212
213         return neighbours, nil
214 }
215
216 func getServedNRCells(servedNRcellsManagementList *C.ServedNRcellsENDCX2ManagementList_t) ([]*entities.ServedNRCell, error) {
217         var servedNRCells []*entities.ServedNRCell
218
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)}}
225
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)
230
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)))
233                         }
234
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)))
237                         }
238
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))))
244                                 }
245                         }
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
250                                 } else {
251                                         return nil, err
252                                 }
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
256                                 } else {
257                                         return nil, err
258                                 }
259                         }
260
261                         neighbours, err := getnRNeighbourInfo(servedNRcellsENDCX2ManagementList__Member.nRNeighbourInfo)
262                         if err != nil {
263                                 return nil, err
264                         }
265                         servedNRCell.NrNeighbourInfos = neighbours
266
267                         servedNRCells = append(servedNRCells, servedNRCell)
268                 }
269         }
270
271         return servedNRCells, nil
272 }
273
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.
276
277 func endcX2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Gnb, error) {
278
279         var gnb *entities.Gnb
280         var globalNbId *entities.GlobalNbId
281
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)
309                                                                                 }
310                                                                         case C.En_gNB_ENDCX2SetupReqAckIEs__value_PR_ServedNRcellsENDCX2ManagementList:
311                                                                                 servedCells, err := getServedNRCells((*C.ServedNRcellsENDCX2ManagementList_t)(unsafe.Pointer(&en_gNB_ENDCX2SetupReqAckIE.value.choice[0])))
312                                                                                 if err != nil {
313                                                                                         return globalNbId, nil, err
314                                                                                 }
315                                                                                 gnb = &entities.Gnb{}
316                                                                                 gnb.ServedNrCells = servedCells
317                                                                         }
318                                                                 }
319                                                         }
320                                                 case C.RespondingNodeType_EndcX2Setup_PR_respond_eNB:
321                                                         /*ignored*/
322                                                 }
323                                         }
324                                 }
325                         }
326                 }
327         }
328
329         return globalNbId, gnb, nil
330 }
331
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)
334
335         if err != nil {
336                 return nil, nil, err
337         }
338
339         defer C.delete_pdu(pdu)
340         return endcX2SetupResponseToProtobuf(pdu)
341 }