Merge "[RICPLT-1423] Big Red button API - Implementation - Disconnect status"
[ric-plt/e2mgr.git] / E2Manager / handlers / x2setupFailureResponseToProtobuf.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         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28         "unsafe"
29 )
30
31 const (
32         maxNrOfErrors = 256
33 )
34
35 // The following are possible values of a choice field, find which the pdu contains.
36 func getCause(causeIE *C.Cause_t, setupFailure *entities.SetupFailure) error {
37         switch causeIE.present {
38         case C.Cause_PR_radioNetwork:
39                 v := (*C.CauseRadioNetwork_t)(unsafe.Pointer(&causeIE.choice[0]))
40                 setupFailure.CauseGroup = &entities.SetupFailure_NetworkLayerCause{NetworkLayerCause: entities.RadioNetworkLayer_Cause(1 + *v)}
41         case C.Cause_PR_transport:
42                 v := (*C.CauseTransport_t)(unsafe.Pointer(&causeIE.choice[0]))
43                 setupFailure.CauseGroup = &entities.SetupFailure_TransportLayerCause{TransportLayerCause: entities.TransportLayer_Cause(1 + *v)}
44         case C.Cause_PR_protocol:
45                 v := (*C.CauseProtocol_t)(unsafe.Pointer(&causeIE.choice[0]))
46                 setupFailure.CauseGroup = &entities.SetupFailure_ProtocolCause{ProtocolCause: entities.Protocol_Cause(1 + *v)}
47         case C.Cause_PR_misc:
48                 v := (*C.CauseMisc_t)(unsafe.Pointer(&causeIE.choice[0]))
49                 setupFailure.CauseGroup = &entities.SetupFailure_MiscellaneousCause{MiscellaneousCause: entities.Miscellaneous_Cause(1 + *v)}
50         }
51         return nil
52 }
53
54 func getCriticalityDiagnostics(critDiagIE *C.CriticalityDiagnostics_t) (*entities.CriticalityDiagnostics, error) {
55         var critDiag *entities.CriticalityDiagnostics
56
57         if critDiagIE.procedureCode != nil {
58                 critDiag = &entities.CriticalityDiagnostics{}
59                 critDiag.ProcedureCode = uint32(*critDiagIE.procedureCode)
60
61         }
62
63         if critDiagIE.triggeringMessage != nil {
64                 if critDiag == nil {
65                         critDiag = &entities.CriticalityDiagnostics{}
66                 }
67                 critDiag.TriggeringMessage = entities.TriggeringMessage(1 + *critDiagIE.triggeringMessage)
68
69         }
70
71         if critDiagIE.procedureCriticality != nil {
72                 if critDiag == nil {
73                         critDiag = &entities.CriticalityDiagnostics{}
74                 }
75                 critDiag.ProcedureCriticality = entities.Criticality(1 + *critDiagIE.procedureCriticality)
76
77         }
78
79         if critDiagIE.iEsCriticalityDiagnostics != nil && critDiagIE.iEsCriticalityDiagnostics.list.count > 0 && critDiagIE.iEsCriticalityDiagnostics.list.count < maxNrOfErrors {
80                 if critDiag == nil {
81                         critDiag = &entities.CriticalityDiagnostics{}
82                 }
83                 var infoElements []*entities.InformationElementCriticalityDiagnostic
84                 iEsCriticalityDiagnostics := (*C.CriticalityDiagnostics_IE_List_t)(critDiagIE.iEsCriticalityDiagnostics)
85                 count:=int(iEsCriticalityDiagnostics.list.count)
86                 iEsCriticalityDiagnostics_slice := (*[1 << 30]*C.CriticalityDiagnostics_IE_List__Member)(unsafe.Pointer(iEsCriticalityDiagnostics.list.array))[:count:count]
87                 for _, criticalityDiagnostics_IE_List__Member := range  iEsCriticalityDiagnostics_slice {
88                         infoElement := &entities.InformationElementCriticalityDiagnostic{IeCriticality: entities.Criticality(1 + criticalityDiagnostics_IE_List__Member.iECriticality)}
89                         infoElement.IeId = uint32(criticalityDiagnostics_IE_List__Member.iE_ID)
90                         infoElement.TypeOfError = entities.TypeOfError(1 + criticalityDiagnostics_IE_List__Member.typeOfError)
91
92                         infoElements = append(infoElements, infoElement)
93
94                 }
95                 critDiag.InformationElementCriticalityDiagnostics = infoElements
96         }
97
98         return critDiag, nil
99 }
100
101 // Populate and return the EN-DC/X2 setup response failure structure with data from the pdu
102 func x2SetupFailureResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.SetupFailure, error) {
103         setupFailure := entities.SetupFailure{}
104
105         if pdu.present == C.E2AP_PDU_PR_unsuccessfulOutcome {
106                 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
107                 unsuccessfulOutcome := *(**C.UnsuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
108                 if unsuccessfulOutcome != nil && unsuccessfulOutcome.value.present == C.UnsuccessfulOutcome__value_PR_X2SetupFailure {
109                         x2SetupFailure := (*C.X2SetupFailure_t)(unsafe.Pointer(&unsuccessfulOutcome.value.choice[0]))
110                         if x2SetupFailure != nil && x2SetupFailure.protocolIEs.list.count > 0 {
111                                 count:=int(x2SetupFailure.protocolIEs.list.count)
112                                 x2SetupFailure_IEs_slice := (*[1 << 30]*C.X2SetupFailure_IEs_t)(unsafe.Pointer(x2SetupFailure.protocolIEs.list.array))[:count:count]
113                                 for _, x2SetupFailure_IE := range x2SetupFailure_IEs_slice {
114                                         if x2SetupFailure_IE != nil {
115                                                 switch x2SetupFailure_IE.value.present {
116                                                 case C.X2SetupFailure_IEs__value_PR_Cause:
117                                                         causeIE := (*C.Cause_t)(unsafe.Pointer(&x2SetupFailure_IE.value.choice[0]))
118                                                         err := getCause(causeIE, &setupFailure)
119                                                         if err != nil {
120                                                                 return nil, err
121                                                         }
122                                                 case C.X2SetupFailure_IEs__value_PR_TimeToWait:
123                                                         setupFailure.TimeToWait = entities.TimeToWait(1 + *((*C.TimeToWait_t)(unsafe.Pointer(&x2SetupFailure_IE.value.choice[0]))))
124                                                 case C.X2SetupFailure_IEs__value_PR_CriticalityDiagnostics:
125                                                         cdIE := (*C.CriticalityDiagnostics_t)(unsafe.Pointer(&x2SetupFailure_IE.value.choice[0]))
126                                                         if cd, err := getCriticalityDiagnostics(cdIE); err == nil {
127                                                                 setupFailure.CriticalityDiagnostics = cd
128                                                         } else {
129                                                                 return nil, err
130                                                         }
131                                                 }
132                                         }
133                                 }
134                         }
135                 }
136         }
137
138         return &setupFailure, nil
139 }
140
141 func unpackX2SetupFailureResponseAndExtract(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*entities.SetupFailure, error) {
142         pdu, err := unpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize)
143         if err != nil {
144                 return nil, err
145         }
146
147         defer C.delete_pdu(pdu)
148
149         return x2SetupFailureResponseToProtobuf(pdu)
150 }