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