[RICPLT-2590] Update Red Button Flow with MultiE2T support + UTs
[ric-plt/e2mgr.git] / E2Manager / e2pdus / x2_setup_requests.go
1 /*
2  *   Copyright (c) 2019 AT&T Intellectual Property.
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20  */
21
22 package e2pdus
23
24 // #cgo CFLAGS: -I../3rdparty/asn1codec/inc/ -I../3rdparty/asn1codec/e2ap_engine/
25 // #cgo LDFLAGS: -L ../3rdparty/asn1codec/lib/ -L../3rdparty/asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
26 // #include <asn1codec_utils.h>
27 // #include <x2setup_request_wrapper.h>
28 import "C"
29 import (
30         "fmt"
31         "github.com/pkg/errors"
32         "os"
33         "unsafe"
34 )
35
36 const (
37         EnvRicId          = "RIC_ID"
38         ShortMacro_eNB_ID = 18
39         Macro_eNB_ID      = 20
40         LongMacro_eNB_ID  = 21
41         Home_eNB_ID       = 28
42 )
43
44 var PackedEndcX2setupRequest []byte
45 var PackedX2setupRequest []byte
46 var PackedEndcX2setupRequestAsString string
47 var PackedX2setupRequestAsString string
48
49 /*The Ric Id is the combination of pLMNId and ENBId*/
50 var pLMNId []byte
51 var eNBId []byte
52 var eNBIdBitqty uint
53 var ricFlag = []byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
54
55 func preparePackedEndcX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) {
56         packedBuf := make([]byte, maxAsn1PackedBufferSize)
57         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
58         packedBufSize := C.ulong(len(packedBuf))
59         pduAsString := ""
60
61         if !C.build_pack_endc_x2setup_request(
62                 (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/,
63                 (*C.uchar)(unsafe.Pointer(&eNB_Id[0])),
64                 C.uint(bitqty),
65                 (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/,
66                 &packedBufSize,
67                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
68                 C.ulong(len(errBuf)),
69                 &errBuf[0]) {
70                 return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0])))
71         }
72
73         pdu := C.new_pdu(C.size_t(1)) //TODO: change signature
74         defer C.delete_pdu(pdu)
75         if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
76                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
77                 pduAsString = C.GoString(&errBuf[0])
78         }
79
80         return packedBuf[:packedBufSize], pduAsString, nil
81 }
82
83 func preparePackedX2SetupRequest(maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, pLMNId []byte, eNB_Id []byte /*18, 20, 21, 28 bits length*/, bitqty uint, ricFlag []byte) ([]byte, string, error) {
84         packedBuf := make([]byte, maxAsn1PackedBufferSize)
85         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
86         packedBufSize := C.ulong(len(packedBuf))
87         pduAsString := ""
88
89         if !C.build_pack_x2setup_request(
90                 (*C.uchar)(unsafe.Pointer(&pLMNId[0])) /*pLMN_Identity*/,
91                 (*C.uchar)(unsafe.Pointer(&eNB_Id[0])),
92                 C.uint(bitqty),
93                 (*C.uchar)(unsafe.Pointer(&ricFlag[0])) /*pLMN_Identity*/,
94                 &packedBufSize,
95                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
96                 C.ulong(len(errBuf)),
97                 &errBuf[0]) {
98                 return nil, "", errors.New(fmt.Sprintf("packing error: %s", C.GoString(&errBuf[0])))
99         }
100
101         pdu := C.new_pdu(C.size_t(1)) //TODO: change signature
102         defer C.delete_pdu(pdu)
103         if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
104                 C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
105                 pduAsString = C.GoString(&errBuf[0])
106         }
107         return packedBuf[:packedBufSize], pduAsString, nil
108 }
109
110 //Expected value in RIC_ID = pLMN_Identity-eNB_ID/<eNB_ID size in bits>
111 //<6 hex digits>-<6 or 8 hex digits>/<18|20|21|28>
112 //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
113 //For example, to get the value of ffffeab/28  the last byte must be 0x0b, not 0xb0 (-ffffea0b/28).
114 func parseRicID(ricId string) error {
115         if _, err := fmt.Sscanf(ricId, "%6x-%8x/%2d", &pLMNId, &eNBId, &eNBIdBitqty); err != nil {
116                 return fmt.Errorf("unable to extract the value of %s: %s", EnvRicId, err)
117         }
118
119         if len(pLMNId) < 3 {
120                 return fmt.Errorf("invalid value for %s, len(pLMNId:%v) != 3", EnvRicId, pLMNId)
121         }
122
123         if len(eNBId) < 3 {
124                 return fmt.Errorf("invalid value for %s, len(eNBId:%v) != 3 or 4", EnvRicId, eNBId)
125         }
126
127         if eNBIdBitqty != ShortMacro_eNB_ID && eNBIdBitqty != Macro_eNB_ID && eNBIdBitqty != LongMacro_eNB_ID && eNBIdBitqty != Home_eNB_ID {
128                 return fmt.Errorf("invalid value for %s, eNBIdBitqty: %d", EnvRicId, eNBIdBitqty)
129         }
130
131         return nil
132 }
133
134 func init() {
135         var err error
136         ricId := os.Getenv(EnvRicId)
137         //ricId="bbbccc-ffff0e/20"
138         //ricId="bbbccc-abcd0e/20"
139         if err = parseRicID(ricId); err != nil {
140                 panic(err)
141         }
142
143         PackedEndcX2setupRequest, PackedEndcX2setupRequestAsString, err = preparePackedEndcX2SetupRequest(MaxAsn1PackedBufferSize, MaxAsn1CodecMessageBufferSize, pLMNId, eNBId, eNBIdBitqty, ricFlag)
144         if err != nil {
145                 panic(err)
146         }
147         PackedX2setupRequest, PackedX2setupRequestAsString, err = preparePackedX2SetupRequest(MaxAsn1PackedBufferSize, MaxAsn1CodecMessageBufferSize, pLMNId, eNBId, eNBIdBitqty, ricFlag)
148         if err != nil {
149                 panic(err)
150         }
151 }