Merge "Improve unit test coverage"
[ric-plt/e2mgr.git] / E2Manager / converters / endc_setup_failure_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         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32         "unsafe"
33 )
34
35 type EndcSetupFailureResponseConverter struct {
36         logger *logger.Logger
37 }
38
39 type IEndcSetupFailureResponseConverter interface {
40         UnpackEndcSetupFailureResponseAndExtract(packedBuf []byte) (*entities.SetupFailure, error)
41 }
42
43 func NewEndcSetupFailureResponseConverter(logger *logger.Logger) *EndcSetupFailureResponseConverter {
44         return &EndcSetupFailureResponseConverter{
45                 logger: logger,
46         }
47 }
48
49
50 // Populate and return the EN-DC/X2 setup response failure structure with data from the pdu
51 func endcX2SetupFailureResponseToProtobuf(pdu *C.E2AP_PDU_t) (*entities.SetupFailure, error) {
52         setupFailure := entities.SetupFailure{}
53
54         if pdu.present == C.E2AP_PDU_PR_unsuccessfulOutcome {
55                 //dereference a union of pointers (C union is represented as a byte array with the size of the largest member)
56                 unsuccessfulOutcome := *(**C.UnsuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
57                 if unsuccessfulOutcome != nil && unsuccessfulOutcome.value.present == C.UnsuccessfulOutcome__value_PR_ENDCX2SetupFailure {
58                         endcX2SetupFailure := (*C.ENDCX2SetupFailure_t)(unsafe.Pointer(&unsuccessfulOutcome.value.choice[0]))
59                         if endcX2SetupFailure != nil && endcX2SetupFailure.protocolIEs.list.count > 0 {
60                                 count:=int(endcX2SetupFailure.protocolIEs.list.count)
61                                 endcX2SetupFailure_IEs_slice := (*[1 << 30]*C.ENDCX2SetupFailure_IEs_t)(unsafe.Pointer(endcX2SetupFailure.protocolIEs.list.array))[:count:count]
62                                 for _, endcX2SetupFailure_IE := range endcX2SetupFailure_IEs_slice {
63                                         if endcX2SetupFailure_IE != nil {
64                                                 switch endcX2SetupFailure_IE.value.present {
65                                                 case C.ENDCX2SetupFailure_IEs__value_PR_Cause:
66                                                         causeIE := (*C.Cause_t)(unsafe.Pointer(&endcX2SetupFailure_IE.value.choice[0]))
67                                                         err := getCause(causeIE, &setupFailure)
68                                                         if err != nil {
69                                                                 return nil, err
70                                                         }
71                                                 case C.ENDCX2SetupFailure_IEs__value_PR_TimeToWait:
72                                                         setupFailure.TimeToWait = entities.TimeToWait(1 + *((*C.TimeToWait_t)(unsafe.Pointer(&endcX2SetupFailure_IE.value.choice[0]))))
73                                                 case C.ENDCX2SetupFailure_IEs__value_PR_CriticalityDiagnostics:
74                                                         cdIE := (*C.CriticalityDiagnostics_t)(unsafe.Pointer(&endcX2SetupFailure_IE.value.choice[0]))
75                                                         if cd, err := getCriticalityDiagnostics(cdIE); err == nil {
76                                                                 setupFailure.CriticalityDiagnostics = cd
77                                                         } else {
78                                                                 return nil, err
79                                                         }
80                                                 }
81                                         }
82                                 }
83                         }
84                 }
85         }
86
87         return &setupFailure, nil
88 }
89
90 func (c *EndcSetupFailureResponseConverter) UnpackEndcSetupFailureResponseAndExtract(packedBuf []byte) (*entities.SetupFailure, error) {
91         pdu, err := UnpackX2apPdu(c.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuf), packedBuf, e2pdus.MaxAsn1CodecMessageBufferSize)
92         if err != nil {
93                 return nil, err
94         }
95
96         defer C.delete_pdu(pdu)
97
98         return endcX2SetupFailureResponseToProtobuf(pdu)
99 }