[RICPLT-2585] add UTs coverage
[ric-plt/e2mgr.git] / E2Manager / converters / x2_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         "github.com/pkg/errors"
31         "unsafe"
32 )
33
34 const (
35         maxnoofMBSFN                     = 8
36         maxnoofBPLMNs                    = 6
37         maxCellineNB                     = 256
38         maxnoofMBMSServiceAreaIdentities = 256
39         maxnoofBands                     = 16
40         maxPools                         = 16
41         maxnoofNeighbours                = 512
42 )
43
44 type X2SetupResponseConverter struct {
45         logger *logger.Logger
46 }
47
48 type IX2SetupResponseConverter interface {
49         UnpackX2SetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Enb, error)
50 }
51
52 func NewX2SetupResponseConverter(logger *logger.Logger) *X2SetupResponseConverter {
53         return &X2SetupResponseConverter{
54                 logger: logger,
55         }
56 }
57
58 // The following are possible values of a choice field, find which the pdu contains.
59 func getENB_ID_choice(eNB_ID C.ENB_ID_t) (entities.EnbType, []byte) {
60
61         enbIdAsBitString := (*C.BIT_STRING_t)(unsafe.Pointer(&eNB_ID.choice[0]))
62         switch eNB_ID.present {
63         case C.ENB_ID_PR_macro_eNB_ID:
64                 return entities.EnbType_MACRO_ENB, C.GoBytes(unsafe.Pointer(enbIdAsBitString.buf), C.int(enbIdAsBitString.size))
65         case C.ENB_ID_PR_home_eNB_ID:
66                 return entities.EnbType_HOME_ENB, C.GoBytes(unsafe.Pointer(enbIdAsBitString.buf), C.int(enbIdAsBitString.size))
67         case C.ENB_ID_PR_short_Macro_eNB_ID:
68                 return entities.EnbType_SHORT_MACRO_ENB, C.GoBytes(unsafe.Pointer(enbIdAsBitString.buf), C.int(enbIdAsBitString.size))
69         case C.ENB_ID_PR_long_Macro_eNB_ID:
70                 return entities.EnbType_LONG_MACRO_ENB, C.GoBytes(unsafe.Pointer(enbIdAsBitString.buf), C.int(enbIdAsBitString.size))
71         }
72
73         return entities.EnbType_UNKNOWN_ENB_TYPE, nil
74 }
75
76 func getFDDInfo(fdd *C.FDD_Info_t) (*entities.FddInfo, error) {
77         var fddInfo *entities.FddInfo
78
79         if fdd != nil {
80                 fddInfo = &entities.FddInfo{UlearFcn: uint32(fdd.uL_EARFCN)}
81                 fddInfo.DlearFcn = uint32(fdd.dL_EARFCN)
82                 fddInfo.UlTransmissionBandwidth = entities.TransmissionBandwidth(1 + fdd.uL_Transmission_Bandwidth)
83                 fddInfo.DlTransmissionBandwidth = entities.TransmissionBandwidth(1 + fdd.dL_Transmission_Bandwidth)
84
85                 extIEs := (*C.ProtocolExtensionContainer_170P145_t)(unsafe.Pointer(fdd.iE_Extensions))
86                 if extIEs != nil && extIEs.list.count > 0 {
87                         count := int(extIEs.list.count)
88                         extIEs_slice := (*[1 << 30]*C.FDD_Info_ExtIEs_t)(unsafe.Pointer(extIEs.list.array))[:count:count]
89                         for _, member := range extIEs_slice {
90                                 switch member.extensionValue.present {
91                                 case C.FDD_Info_ExtIEs__extensionValue_PR_EARFCNExtension:
92                                         eARFCNExtension := (*C.EARFCNExtension_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
93                                         if member.id == C.ProtocolIE_ID_id_UL_EARFCNExtension {
94                                                 fddInfo.UlearFcn = uint32(*eARFCNExtension)
95                                         }
96                                         if member.id == C.ProtocolIE_ID_id_DL_EARFCNExtension {
97                                                 fddInfo.DlearFcn = uint32(*eARFCNExtension)
98                                         }
99                                 case C.FDD_Info_ExtIEs__extensionValue_PR_OffsetOfNbiotChannelNumberToEARFCN:
100                                         /*ignored*/
101                                 case C.FDD_Info_ExtIEs__extensionValue_PR_NRS_NSSS_PowerOffset:
102                                         /*ignored*/
103                                 case C.FDD_Info_ExtIEs__extensionValue_PR_NSSS_NumOccasionDifferentPrecoder:
104                                         /*ignored*/
105                                 }
106                         }
107                 }
108         }
109
110         return fddInfo, nil
111 }
112
113 func getSpecialSubframeInfo(info C.SpecialSubframe_Info_t) *entities.SpecialSubframeInfo {
114         specialSubframeInfo := entities.SpecialSubframeInfo{}
115
116         specialSubframeInfo.SpecialSubframePatterns = entities.SpecialSubframe_Patterns(1 + info.specialSubframePatterns)
117         specialSubframeInfo.CyclicPrefixDl = entities.CyclicPrefix(1 + info.cyclicPrefixDL)
118         specialSubframeInfo.CyclicPrefixUl = entities.CyclicPrefix(1 + info.cyclicPrefixUL)
119
120         return &specialSubframeInfo
121 }
122
123 func getAdditionalSpecialSubframeInfo(info *C.AdditionalSpecialSubframe_Info_t) *entities.AdditionalSpecialSubframeInfo {
124         additionalSpecialSubframeInfo := &entities.AdditionalSpecialSubframeInfo{AdditionalSpecialSubframePatterns: entities.AdditionalSpecialSubframe_Patterns(1 + info.additionalspecialSubframePatterns)}
125
126         additionalSpecialSubframeInfo.CyclicPrefixDl = entities.CyclicPrefix(1 + info.cyclicPrefixDL)
127         additionalSpecialSubframeInfo.CyclicPrefixUl = entities.CyclicPrefix(1 + info.cyclicPrefixUL)
128
129         return additionalSpecialSubframeInfo
130 }
131
132 func getAdditionalSpecialSubframeExtensionInfo(info *C.AdditionalSpecialSubframeExtension_Info_t) *entities.AdditionalSpecialSubframeExtensionInfo {
133         additionalSpecialSubframeExtensionInfo := &entities.AdditionalSpecialSubframeExtensionInfo{AdditionalSpecialSubframePatternsExtension: entities.AdditionalSpecialSubframePatterns_Extension(1 + info.additionalspecialSubframePatternsExtension)}
134
135         additionalSpecialSubframeExtensionInfo.CyclicPrefixDl = entities.CyclicPrefix(1 + info.cyclicPrefixDL)
136         additionalSpecialSubframeExtensionInfo.CyclicPrefixUl = entities.CyclicPrefix(1 + info.cyclicPrefixUL)
137
138         return additionalSpecialSubframeExtensionInfo
139 }
140
141 func getTDDInfo(tdd *C.TDD_Info_t) (*entities.TddInfo, error) {
142         var tddInfo *entities.TddInfo
143
144         if tdd != nil {
145                 tddInfo = &entities.TddInfo{EarFcn: uint32(tdd.eARFCN)}
146                 tddInfo.TransmissionBandwidth = entities.TransmissionBandwidth(1 + tdd.transmission_Bandwidth)
147                 tddInfo.SubframeAssignment = entities.SubframeAssignment(1 + tdd.subframeAssignment)
148
149                 tddInfo.SpecialSubframeInfo = getSpecialSubframeInfo(tdd.specialSubframe_Info)
150
151                 extIEs := (*C.ProtocolExtensionContainer_170P206_t)(unsafe.Pointer(tdd.iE_Extensions))
152                 if extIEs != nil && extIEs.list.count > 0 {
153                         count := int(extIEs.list.count)
154                         extIEs_slice := (*[1 << 30]*C.TDD_Info_ExtIEs_t)(unsafe.Pointer(extIEs.list.array))[:count:count]
155                         for _, member := range extIEs_slice {
156                                 switch member.extensionValue.present {
157                                 case C.TDD_Info_ExtIEs__extensionValue_PR_AdditionalSpecialSubframe_Info:
158                                         tddInfo.AdditionalSpecialSubframeInfo = getAdditionalSpecialSubframeInfo((*C.AdditionalSpecialSubframe_Info_t)(unsafe.Pointer(&member.extensionValue.choice[0])))
159                                 case C.TDD_Info_ExtIEs__extensionValue_PR_EARFCNExtension:
160                                         tddInfo.EarFcn = uint32(*(*C.EARFCNExtension_t)(unsafe.Pointer(&member.extensionValue.choice[0])))
161                                 case C.TDD_Info_ExtIEs__extensionValue_PR_AdditionalSpecialSubframeExtension_Info:
162                                         tddInfo.AdditionalSpecialSubframeExtensionInfo = getAdditionalSpecialSubframeExtensionInfo((*C.AdditionalSpecialSubframeExtension_Info_t)(unsafe.Pointer(&member.extensionValue.choice[0])))
163                                 }
164                         }
165                 }
166         }
167
168         return tddInfo, nil
169 }
170
171 // The following are possible values of a choice field, find which the pdu contains.
172 func getSubframeAllocation_choice(subframeAllocation C.SubframeAllocation_t) (entities.SubframeAllocationType, string, error) {
173
174         switch subframeAllocation.present {
175         case C.SubframeAllocation_PR_oneframe:
176                 frameAllocation := (*C.Oneframe_t)(unsafe.Pointer(&subframeAllocation.choice[0]))
177                 return entities.SubframeAllocationType_ONE_FRAME, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(frameAllocation.buf), C.int(frameAllocation.size))), nil
178         case C.SubframeAllocation_PR_fourframes:
179                 frameAllocation := (*C.Fourframes_t)(unsafe.Pointer(&subframeAllocation.choice[0]))
180                 return entities.SubframeAllocationType_FOUR_FRAME, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(frameAllocation.buf), C.int(frameAllocation.size))), nil
181         }
182         return entities.SubframeAllocationType_UNKNOWN_SUBFRAME_ALLOCATION_TYPE, "", errors.Errorf("unexpected subframe allocation choice")
183 }
184
185 func getMBSFN_Subframe_Infolist(mBSFN_Subframe_Infolist *C.MBSFN_Subframe_Infolist_t) ([]*entities.MbsfnSubframe, error) {
186         var mBSFNSubframes []*entities.MbsfnSubframe
187
188         if mBSFN_Subframe_Infolist.list.count > 0 && mBSFN_Subframe_Infolist.list.count <= maxnoofMBSFN {
189                 count := int(mBSFN_Subframe_Infolist.list.count)
190                 BSFN_Subframe_Infolist_slice := (*[1 << 30]*C.MBSFN_Subframe_Info_t)(unsafe.Pointer(mBSFN_Subframe_Infolist.list.array))[:count:count]
191                 for _, member := range BSFN_Subframe_Infolist_slice {
192                         mBSFNSubframe := &entities.MbsfnSubframe{RadioframeAllocationPeriod: entities.RadioframeAllocationPeriod(1 + member.radioframeAllocationPeriod)}
193
194                         mBSFNSubframe.RadioframeAllocationOffset = uint32(member.radioframeAllocationOffset)
195
196                         allocType, subframeAllocation, err := getSubframeAllocation_choice(member.subframeAllocation)
197                         if err != nil {
198                                 return nil, err
199                         }
200                         mBSFNSubframe.SubframeAllocation = subframeAllocation
201                         mBSFNSubframe.SubframeAllocationType = allocType
202
203                         mBSFNSubframes = append(mBSFNSubframes, mBSFNSubframe)
204                 }
205         }
206
207         return mBSFNSubframes, nil
208 }
209
210 func getPRACHConfiguration(prachConf *C.PRACH_Configuration_t) *entities.PrachConfiguration {
211
212         var prachConfiguration *entities.PrachConfiguration
213
214         prachConfiguration = &entities.PrachConfiguration{RootSequenceIndex: uint32(prachConf.rootSequenceIndex)}
215         prachConfiguration.ZeroCorrelationZoneConfiguration = uint32(prachConf.zeroCorrelationIndex)
216         prachConfiguration.HighSpeedFlag = prachConf.highSpeedFlag != 0
217         prachConfiguration.PrachFrequencyOffset = uint32(prachConf.prach_FreqOffset)
218         if prachConf.prach_ConfigIndex != nil {
219                 prachConfiguration.PrachConfigurationIndex = uint32(*prachConf.prach_ConfigIndex)
220         }
221
222         return prachConfiguration
223 }
224 func getServedCellsInfoExt(extIEs *C.ProtocolExtensionContainer_170P192_t, servedCellInfo *entities.ServedCellInfo) error {
225
226         if extIEs != nil && extIEs.list.count > 0 {
227                 count := int(extIEs.list.count)
228                 extIEs_slice := (*[1 << 30]*C.ServedCell_Information_ExtIEs_t)(unsafe.Pointer(extIEs.list.array))[:count:count]
229                 for _, member := range extIEs_slice {
230                         switch member.extensionValue.present {
231                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_Number_of_Antennaports:
232                                 ports := (*C.Number_of_Antennaports_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
233                                 servedCellInfo.NumberOfAntennaPorts = entities.NumberOfAntennaPorts(1 + *ports)
234                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_PRACH_Configuration:
235                                 prachConfiguration := getPRACHConfiguration((*C.PRACH_Configuration_t)(unsafe.Pointer(&member.extensionValue.choice[0])))
236                                 servedCellInfo.PrachConfiguration = prachConfiguration
237                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_MBSFN_Subframe_Infolist:
238                                 mBSFN_Subframe_Infolist, err := getMBSFN_Subframe_Infolist((*C.MBSFN_Subframe_Infolist_t)(unsafe.Pointer(&member.extensionValue.choice[0])))
239                                 if err != nil {
240                                         return err
241                                 }
242                                 servedCellInfo.MbsfnSubframeInfos = mBSFN_Subframe_Infolist
243                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_CSG_Id:
244                                 csgId := (*C.CSG_Id_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
245                                 servedCellInfo.CsgId = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(csgId.buf), C.int(csgId.size)))
246                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_MBMS_Service_Area_Identity_List:
247                                 mBMS_Service_Area_Identity_List := (*C.MBMS_Service_Area_Identity_List_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
248                                 if mBMS_Service_Area_Identity_List.list.count > 0 && mBMS_Service_Area_Identity_List.list.count < maxnoofMBMSServiceAreaIdentities {
249                                         count := int(mBMS_Service_Area_Identity_List.list.count)
250                                         mBMS_Service_Area_Identity_List_slice := (*[1 << 30]*C.MBMS_Service_Area_Identity_t)(unsafe.Pointer(mBMS_Service_Area_Identity_List.list.array))[:count:count]
251                                         for _, identity := range mBMS_Service_Area_Identity_List_slice {
252                                                 servedCellInfo.MbmsServiceAreaIdentities = append(servedCellInfo.MbmsServiceAreaIdentities, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(identity.buf), C.int(identity.size))))
253                                         }
254                                 }
255                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_MultibandInfoList:
256                                 multibandInfoList := (*C.MultibandInfoList_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
257                                 if multibandInfoList.list.count > 0 && multibandInfoList.list.count < maxnoofBands {
258                                         count := int(multibandInfoList.list.count)
259                                         multibandInfoList_slice := (*[1 << 30]*C.BandInfo_t)(unsafe.Pointer(multibandInfoList.list.array))[:count:count]
260                                         for _, bandInfo := range multibandInfoList_slice {
261                                                 servedCellInfo.MultibandInfos = append(servedCellInfo.MultibandInfos, uint32(bandInfo.freqBandIndicator))
262                                         }
263                                 }
264                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_FreqBandIndicatorPriority:
265                                 priority := (*C.FreqBandIndicatorPriority_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
266                                 servedCellInfo.FreqBandIndicatorPriority = entities.FreqBandIndicatorPriority(1 + *priority)
267                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_BandwidthReducedSI:
268                                 si := (*C.BandwidthReducedSI_t)(unsafe.Pointer(&member.extensionValue.choice[0]))
269                                 servedCellInfo.BandwidthReducedSi = entities.BandwidthReducedSI(1 + *si)
270                         case C.ServedCell_Information_ExtIEs__extensionValue_PR_ProtectedEUTRAResourceIndication:
271                                 /*ignored*/
272
273                         }
274
275                 }
276
277         }
278
279         return nil
280 }
281
282 func getServedCellsNeighbour_Info(neighbour_Information *C.Neighbour_Information_t) ([]*entities.NeighbourInformation, error) {
283         var neighbours []*entities.NeighbourInformation
284
285         if neighbour_Information != nil && neighbour_Information.list.count > 0 && neighbour_Information.list.count <= maxnoofNeighbours {
286                 count := int(neighbour_Information.list.count)
287                 neighbour_Information_slice := (*[1 << 30]*C.Neighbour_Information__Member)(unsafe.Pointer(neighbour_Information.list.array))[:count:count]
288                 for _, member := range neighbour_Information_slice {
289
290                         //pLMN_Identity:eUTRANcellIdentifier
291                         plmnId := C.GoBytes(unsafe.Pointer(member.eCGI.pLMN_Identity.buf), C.int(member.eCGI.pLMN_Identity.size))
292                         eUTRANcellIdentifier := C.GoBytes(unsafe.Pointer(member.eCGI.eUTRANcellIdentifier.buf), C.int(member.eCGI.eUTRANcellIdentifier.size))
293                         neighbourInfo := &entities.NeighbourInformation{Ecgi: fmt.Sprintf("%02x:%02x", plmnId, eUTRANcellIdentifier)}
294
295                         neighbourInfo.Pci = uint32(member.pCI)
296
297                         neighbourInfo.EarFcn = uint32(member.eARFCN)
298
299                         extIEs := (*C.ProtocolExtensionContainer_170P172_t)(unsafe.Pointer(member.iE_Extensions))
300                         if extIEs != nil && extIEs.list.count > 0 {
301                                 count := int(extIEs.list.count)
302                                 neighbour_Information_ExtIEs_slice := (*[1 << 30]*C.Neighbour_Information_ExtIEs_t)(unsafe.Pointer(extIEs.list.array))[:count:count]
303                                 for _, neighbour_Information_ExtIE := range neighbour_Information_ExtIEs_slice {
304                                         switch neighbour_Information_ExtIE.extensionValue.present {
305                                         case C.Neighbour_Information_ExtIEs__extensionValue_PR_TAC:
306                                                 tac := (*C.TAC_t)(unsafe.Pointer(&neighbour_Information_ExtIE.extensionValue.choice[0]))
307                                                 neighbourInfo.Tac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(tac.buf), C.int(tac.size)))
308                                         case C.Neighbour_Information_ExtIEs__extensionValue_PR_EARFCNExtension:
309                                                 earFcn := (*C.EARFCNExtension_t)(unsafe.Pointer(&neighbour_Information_ExtIE.extensionValue.choice[0]))
310                                                 neighbourInfo.EarFcn = uint32(*earFcn)
311                                         }
312                                 }
313                         }
314
315                         neighbours = append(neighbours, neighbourInfo)
316                 }
317         }
318
319         return neighbours, nil
320 }
321
322 func getServedCells(servedCellsIE *C.ServedCells_t) ([]*entities.ServedCellInfo, error) {
323         var servedCells []*entities.ServedCellInfo
324
325         if servedCellsIE != nil && servedCellsIE.list.count > 0 && servedCellsIE.list.count < maxCellineNB {
326                 count := int(servedCellsIE.list.count)
327                 servedCells__Member_slice := (*[1 << 30]*C.ServedCells__Member)(unsafe.Pointer(servedCellsIE.list.array))[:count:count]
328                 for _, member := range servedCells__Member_slice {
329                         servedCellInfo := &entities.ServedCellInfo{Pci: uint32(member.servedCellInfo.pCI)}
330
331                         //pLMN_Identity:eUTRANcellIdentifier
332                         plmnId := C.GoBytes(unsafe.Pointer(member.servedCellInfo.cellId.pLMN_Identity.buf), C.int(member.servedCellInfo.cellId.pLMN_Identity.size))
333                         eUTRANcellIdentifier := C.GoBytes(unsafe.Pointer(member.servedCellInfo.cellId.eUTRANcellIdentifier.buf), C.int(member.servedCellInfo.cellId.eUTRANcellIdentifier.size))
334                         servedCellInfo.CellId = fmt.Sprintf("%02x:%02x", plmnId, eUTRANcellIdentifier)
335
336                         servedCellInfo.Tac = fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(member.servedCellInfo.tAC.buf), C.int(member.servedCellInfo.tAC.size)))
337
338                         if member.servedCellInfo.broadcastPLMNs.list.count > 0 && member.servedCellInfo.broadcastPLMNs.list.count <= maxnoofBPLMNs {
339                                 count := int(member.servedCellInfo.broadcastPLMNs.list.count)
340                                 pLMN_Identity_slice := (*[1 << 30]*C.PLMN_Identity_t)(unsafe.Pointer(member.servedCellInfo.broadcastPLMNs.list.array))[:count:count]
341                                 for _, pLMN_Identity := range pLMN_Identity_slice {
342                                         servedCellInfo.BroadcastPlmns = append(servedCellInfo.BroadcastPlmns, fmt.Sprintf("%02x", C.GoBytes(unsafe.Pointer(pLMN_Identity.buf), C.int(pLMN_Identity.size))))
343                                 }
344                         }
345
346                         switch member.servedCellInfo.eUTRA_Mode_Info.present {
347                         case C.EUTRA_Mode_Info_PR_fDD:
348                                 if fdd, err := getFDDInfo(*(**C.FDD_Info_t)(unsafe.Pointer(&member.servedCellInfo.eUTRA_Mode_Info.choice[0]))); fdd != nil && err == nil {
349                                         servedCellInfo.ChoiceEutraMode, servedCellInfo.EutraMode = &entities.ChoiceEUTRAMode{Fdd: fdd}, entities.Eutra_FDD
350                                 } else {
351                                         return nil, err
352                                 }
353                         case C.EUTRA_Mode_Info_PR_tDD:
354                                 if tdd, err := getTDDInfo(*(**C.TDD_Info_t)(unsafe.Pointer(&member.servedCellInfo.eUTRA_Mode_Info.choice[0]))); tdd != nil && err == nil {
355                                         servedCellInfo.ChoiceEutraMode, servedCellInfo.EutraMode = &entities.ChoiceEUTRAMode{Tdd: tdd}, entities.Eutra_TDD
356                                 } else {
357                                         return nil, err
358                                 }
359                         }
360
361                         neighbours, err := getServedCellsNeighbour_Info(member.neighbour_Info)
362                         if err != nil {
363                                 return nil, err
364                         }
365                         servedCellInfo.NeighbourInfos = neighbours
366
367                         if err := getServedCellsInfoExt((*C.ProtocolExtensionContainer_170P192_t)(unsafe.Pointer(member.servedCellInfo.iE_Extensions)), servedCellInfo); err != nil {
368                                 return nil, err
369                         }
370
371                         servedCells = append(servedCells, servedCellInfo)
372
373                 }
374         }
375
376         return servedCells, nil
377 }
378
379 func getGUGroupIDList(guGroupIDList *C.GUGroupIDList_t) []string {
380         var ids []string
381
382         if guGroupIDList != nil && guGroupIDList.list.count > 0 && guGroupIDList.list.count <= maxPools {
383                 count := int(guGroupIDList.list.count)
384                 guGroupIDList_slice := (*[1 << 30]*C.GU_Group_ID_t)(unsafe.Pointer(guGroupIDList.list.array))[:count:count]
385                 for _, guGroupID := range guGroupIDList_slice {
386                         plmnId := C.GoBytes(unsafe.Pointer(guGroupID.pLMN_Identity.buf), C.int(guGroupID.pLMN_Identity.size))
387                         mME_Group_ID := C.GoBytes(unsafe.Pointer(guGroupID.mME_Group_ID.buf), C.int(guGroupID.mME_Group_ID.size))
388                         ids = append(ids, fmt.Sprintf("%02x:%02x", plmnId, mME_Group_ID))
389                 }
390         }
391
392         return ids
393 }
394
395 // Populate  the ENB structure with data from the pdu
396 // Return the ENB and the associated key which can later be used to retrieve the ENB from the database.
397 func x2SetupResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.GlobalNbId, *entities.Enb, error) {
398         var globalNbId *entities.GlobalNbId
399
400         enb := entities.Enb{}
401
402         if pdu.present == C.E2AP_PDU_PR_successfulOutcome {
403                 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
404                 successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
405                 if successfulOutcome != nil && successfulOutcome.value.present == C.SuccessfulOutcome__value_PR_X2SetupResponse {
406                         x2SetupResponse := (*C.X2SetupResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
407                         if x2SetupResponse != nil && x2SetupResponse.protocolIEs.list.count > 0 {
408                                 count := int(x2SetupResponse.protocolIEs.list.count)
409                                 x2SetupResponse_IEs_slice := (*[1 << 30]*C.X2SetupResponse_IEs_t)(unsafe.Pointer(x2SetupResponse.protocolIEs.list.array))[:count:count]
410                                 for _, x2SetupResponse_IE := range x2SetupResponse_IEs_slice {
411                                         switch x2SetupResponse_IE.value.present {
412                                         case C.X2SetupResponse_IEs__value_PR_GlobalENB_ID:
413                                                 globalENB_ID := (*C.GlobalENB_ID_t)(unsafe.Pointer(&x2SetupResponse_IE.value.choice[0]))
414                                                 plmnId := C.GoBytes(unsafe.Pointer(globalENB_ID.pLMN_Identity.buf), C.int(globalENB_ID.pLMN_Identity.size))
415                                                 enbType, enbVal := getENB_ID_choice(globalENB_ID.eNB_ID)
416
417                                                 globalNbId = &entities.GlobalNbId{}
418                                                 globalNbId.NbId = fmt.Sprintf("%02x", enbVal)
419                                                 globalNbId.PlmnId = fmt.Sprintf("%02x", plmnId)
420                                                 enb.EnbType = enbType
421
422                                         case C.X2SetupResponse_IEs__value_PR_ServedCells:
423                                                 ServedCells, err := getServedCells((*C.ServedCells_t)(unsafe.Pointer(&x2SetupResponse_IE.value.choice[0])))
424                                                 if err != nil {
425                                                         return globalNbId, nil, err
426                                                 }
427                                                 enb.ServedCells = ServedCells
428                                         case C.X2SetupResponse_IEs__value_PR_GUGroupIDList:
429                                                 enb.GuGroupIds = getGUGroupIDList((*C.GUGroupIDList_t)(unsafe.Pointer(&x2SetupResponse_IE.value.choice[0])))
430                                         case C.X2SetupResponse_IEs__value_PR_CriticalityDiagnostics:
431                                                 /*ignored*/
432                                         case C.X2SetupResponse_IEs__value_PR_LHN_ID:
433                                                 /*ignored*/
434                                         }
435                                 }
436                         }
437                 }
438         }
439
440         return globalNbId, &enb, nil
441 }
442
443 func (c *X2SetupResponseConverter) UnpackX2SetupResponseAndExtract(packedBuf []byte) (*entities.GlobalNbId, *entities.Enb, error) {
444         pdu, err := UnpackX2apPdu(c.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuf), packedBuf, e2pdus.MaxAsn1CodecMessageBufferSize)
445         if err != nil {
446                 return nil, nil, err
447         }
448
449         defer C.delete_pdu(pdu)
450         return x2SetupResponseToProtobuf(pdu)
451 }