RIC-961:implement Xn and X2 component IDs correctly in E2M
[ric-plt/e2mgr.git] / E2Manager / converters / x2_reset_response_extractor.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 <SuccessfulOutcome.h>
27 import "C"
28 import (
29         "e2mgr/e2pdus"
30         "e2mgr/logger"
31         "fmt"
32         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
33         "unsafe"
34 )
35
36 type X2ResetResponseExtractor struct {
37         logger *logger.Logger
38 }
39
40 func NewX2ResetResponseExtractor(logger *logger.Logger) *X2ResetResponseExtractor {
41         return &X2ResetResponseExtractor{
42                 logger: logger,
43         }
44 }
45
46 type IX2ResetResponseExtractor interface {
47         ExtractCriticalityDiagnosticsFromPdu(packedBuffer []byte) (*entities.CriticalityDiagnostics, error)
48 }
49
50 func (e *X2ResetResponseExtractor) ExtractCriticalityDiagnosticsFromPdu(packedBuffer []byte) (*entities.CriticalityDiagnostics, error) {
51         pdu, err := UnpackX2apPdu(e.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuffer), packedBuffer, e2pdus.MaxAsn1CodecMessageBufferSize)
52
53         if err != nil {
54                 return nil, err
55         }
56
57         if pdu.present != C.E2AP_PDU_PR_successfulOutcome {
58                 return nil, fmt.Errorf("Invalid E2AP_PDU value")
59         }
60
61         successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
62
63         if successfulOutcome == nil || successfulOutcome.value.present != C.SuccessfulOutcome__value_PR_ResetResponse {
64                 return nil, fmt.Errorf("Unexpected SuccessfulOutcome value")
65         }
66
67         resetResponse := (*C.ResetResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
68
69         protocolIEsListCount := resetResponse.protocolIEs.list.count
70
71         if protocolIEsListCount == 0 {
72                 return nil, nil
73         }
74
75         if protocolIEsListCount != 1 {
76                 return nil, fmt.Errorf("Invalid protocolIEs list count")
77         }
78
79         resetResponseIEs := (*[1 << 30]*C.ResetResponse_IEs_t)(unsafe.Pointer(resetResponse.protocolIEs.list.array))[:int(protocolIEsListCount):int(protocolIEsListCount)]
80
81         resetResponseIE := resetResponseIEs[0]
82
83         if resetResponseIE.value.present != C.ResetResponse_IEs__value_PR_CriticalityDiagnostics {
84                 return nil, fmt.Errorf("Invalid protocolIEs value")
85         }
86
87         cd := (*C.CriticalityDiagnostics_t)(unsafe.Pointer(&resetResponseIE.value.choice[0]))
88
89         return getCriticalityDiagnostics(cd)
90 }