[RICPLT-2048] X2 ENDC Setup request refactoring
[ric-plt/e2mgr.git] / E2Manager / e2pdus / x2_setup_requests.go
1 /*******************************************************************************
2  *
3  *   Copyright (c) 2019 AT&T Intellectual Property.
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 e2pdus
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_request_wrapper.h>
24 import "C"
25 import (
26         "fmt"
27         "github.com/pkg/errors"
28         "os"
29         "unsafe"
30 )
31
32 const (
33         EnvRicId          = "RIC_ID"
34         ShortMacro_eNB_ID = 18
35         Macro_eNB_ID      = 20
36         LongMacro_eNB_ID  = 21
37         Home_eNB_ID       = 28
38 )
39
40 var PackedEndcX2setupRequest []byte
41 var PackedX2setupRequest []byte
42 var PackedEndcX2setupRequestAsString string
43 var PackedX2setupRequestAsString string
44
45 /*The Ric Id is the combination of pLMNId and ENBId*/
46 var pLMNId []byte
47 var eNBId []byte
48 var eNBIdBitqty uint
49 var ricFlag = []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
50
51 func preparePackedEndcX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) {
52         packedBuf := make([]byte, maxAsn1PackedBufferSize)
53         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
54         packedBufSize := C.ulong(len(packedBuf))
55         pduAsString := ""
56
57         if !C.build_pack_endc_x2setup_request(
58                 (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/,
59                 (*C.uchar)(unsafe.Pointer(&eNB_Id[0])),
60                 C.uint(bitqty),
61                 (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/,
62                 &packedBufSize,
63                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
64                 C.ulong(len(errBuf)),
65                 &errBuf[0]) {
66                 return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0])))
67         }
68
69         pdu := C.new_pdu(C.size_t(1)) //TODO: change signature
70         defer C.delete_pdu(pdu)
71         if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
72                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
73                 pduAsString = C.GoString(&errBuf[0])
74         }
75
76         return packedBuf[:packedBufSize], pduAsString, nil
77 }
78
79 func preparePackedX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) {
80         packedBuf := make([]byte, maxAsn1PackedBufferSize)
81         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
82         packedBufSize := C.ulong(len(packedBuf))
83         pduAsString := ""
84
85         if !C.build_pack_x2setup_request(
86                 (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/,
87                 (*C.uchar)(unsafe.Pointer(&eNB_Id[0])),
88                 C.uint(bitqty),
89                 (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/,
90                 &packedBufSize,
91                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
92                 C.ulong(len(errBuf)),
93                 &errBuf[0]) {
94                 return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0])))
95         }
96
97         pdu := C.new_pdu(C.size_t(1)) //TODO: change signature
98         defer C.delete_pdu(pdu)
99         if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
100                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
101                 pduAsString = C.GoString(&errBuf[0])
102         }
103         return packedBuf[:packedBufSize], pduAsString, nil
104 }
105
106 //Expected value in RIC_ID = pLMN_Identity-eNB_ID/<eNB_ID size in bits>
107 //<6 hex digits>-<6 or 8 hex digits>/<18|20|21|28>
108 //Each byte is represented by two hex digits, the value in the lowest byte of the eNB_ID must be assigned to the lowest bits
109 //For example, to get the value of ffffeab/28  the last byte must be 0x0b, not 0xb0 (-ffffea0b/28).
110 func parseRicID(ricId string) error {
111         if _, err := fmt.Sscanf(ricId, "%6x-%8x/%2d", &pLMNId, &eNBId, &eNBIdBitqty); err != nil {
112                 return fmt.Errorf("unable to extract the value of %s: %s", EnvRicId, err)
113         }
114
115         if len(pLMNId) < 3 {
116                 return fmt.Errorf("invalid value for %s, len(pLMNId:%v) != 3", EnvRicId, pLMNId)
117         }
118
119         if len(eNBId) < 3 {
120                 return fmt.Errorf("invalid value for %s, len(eNBId:%v) != 3 or 4", EnvRicId, eNBId)
121         }
122
123         if eNBIdBitqty != ShortMacro_eNB_ID && eNBIdBitqty != Macro_eNB_ID && eNBIdBitqty != LongMacro_eNB_ID && eNBIdBitqty != Home_eNB_ID {
124                 return fmt.Errorf("invalid value for %s, eNBIdBitqty: %d", EnvRicId, eNBIdBitqty)
125         }
126
127         return nil
128 }
129
130 func init() {
131         var err error
132         ricId := os.Getenv(EnvRicId)
133         //ricId="bbbccc-ffff0e/20"
134         //ricId="bbbccc-abcd0e/20"
135         if err = parseRicID(ricId); err != nil {
136                 panic(err)
137         }
138
139         PackedEndcX2setupRequest, PackedEndcX2setupRequestAsString, err = preparePackedEndcX2SetupRequest(MaxAsn1PackedBufferSize, MaxAsn1CodecMessageBufferSize, pLMNId, eNBId, eNBIdBitqty, ricFlag)
140         if err != nil {
141                 panic(err)
142         }
143         PackedX2setupRequest, PackedX2setupRequestAsString, err = preparePackedX2SetupRequest(MaxAsn1PackedBufferSize, MaxAsn1CodecMessageBufferSize, pLMNId, eNBId, eNBIdBitqty, ricFlag)
144         if err != nil {
145                 panic(err)
146         }
147 }