sync from Azure to LF
[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
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 <SuccessfulOutcome.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         "unsafe"
31 )
32
33 type X2ResetResponseExtractor struct {
34         logger *logger.Logger
35 }
36
37 func NewX2ResetResponseExtractor(logger *logger.Logger) *X2ResetResponseExtractor {
38         return &X2ResetResponseExtractor{
39                 logger: logger,
40         }
41 }
42
43 type IX2ResetResponseExtractor interface {
44         ExtractCriticalityDiagnosticsFromPdu(packedBuffer []byte) (*entities.CriticalityDiagnostics, error)
45 }
46
47 func (e *X2ResetResponseExtractor) ExtractCriticalityDiagnosticsFromPdu(packedBuffer []byte) (*entities.CriticalityDiagnostics, error) {
48         pdu, err := UnpackX2apPdu(e.logger, e2pdus.MaxAsn1CodecAllocationBufferSize, len(packedBuffer), packedBuffer, e2pdus.MaxAsn1CodecMessageBufferSize)
49
50         if err != nil {
51                 return nil, err
52         }
53
54         if pdu.present != C.E2AP_PDU_PR_successfulOutcome {
55                 return nil, fmt.Errorf("Invalid E2AP_PDU value")
56         }
57
58         successfulOutcome := *(**C.SuccessfulOutcome_t)(unsafe.Pointer(&pdu.choice[0]))
59
60         if successfulOutcome == nil || successfulOutcome.value.present != C.SuccessfulOutcome__value_PR_ResetResponse {
61                 return nil, fmt.Errorf("Unexpected SuccessfulOutcome value")
62         }
63
64         resetResponse := (*C.ResetResponse_t)(unsafe.Pointer(&successfulOutcome.value.choice[0]))
65
66         protocolIEsListCount := resetResponse.protocolIEs.list.count
67
68         if protocolIEsListCount == 0 {
69                 return nil, nil
70         }
71
72         if protocolIEsListCount != 1 {
73                 return nil, fmt.Errorf("Invalid protocolIEs list count")
74         }
75
76         resetResponseIEs := (*[1 << 30]*C.ResetResponse_IEs_t)(unsafe.Pointer(resetResponse.protocolIEs.list.array))[:int(protocolIEsListCount):int(protocolIEsListCount)]
77
78         resetResponseIE := resetResponseIEs[0]
79
80         if resetResponseIE.value.present != C.ResetResponse_IEs__value_PR_CriticalityDiagnostics {
81                 return nil, fmt.Errorf("Invalid protocolIEs value")
82         }
83
84         cd := (*C.CriticalityDiagnostics_t)(unsafe.Pointer(&resetResponseIE.value.choice[0]))
85
86         return getCriticalityDiagnostics(cd)
87 }