Fix licensing issues
[ric-plt/resource-status-manager.git] / RSM / e2pdus / resource_status_request.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../3rdparty/asn1codec/inc/ -I../3rdparty/asn1codec/e2ap_engine/
21 // #cgo LDFLAGS: -L ../3rdparty/asn1codec/lib/ -L../3rdparty/asn1codec/e2ap_engine/ -le2ap_codec -lasncodec
22 // #include <asn1codec_utils.h>
23 // #include <resource_status_request_wrapper.h>
24 import "C"
25 import (
26         "fmt"
27         "github.com/pkg/errors"
28         "rsm/enums"
29         "unsafe"
30 )
31
32 const (
33         MaxAsn1PackedBufferSize       = 4096
34         MaxAsn1CodecMessageBufferSize = 4096
35 )
36
37 type Measurement_ID int64
38
39 type ResourceStatusRequestData struct {
40         CellID                       string // PLMNIdentifier:eUTRANCellIdentifier
41         MeasurementID                Measurement_ID
42         MeasurementID2               Measurement_ID
43         PartialSuccessAllowed        bool
44         PrbPeriodic                  bool
45         TnlLoadIndPeriodic           bool
46         HwLoadIndPeriodic            bool
47         AbsStatusPeriodic            bool
48         RsrpMeasurementPeriodic      bool
49         CsiPeriodic                  bool
50         PeriodicityMS                enums.ReportingPeriodicity
51         PeriodicityRsrpMeasurementMS enums.ReportingPeriodicityRSRPMR
52         PeriodicityCsiMS             enums.ReportingPeriodicityCSIR
53 }
54
55 func BuildPackedResourceStatusRequest(registrationRequest enums.Registration_Request, request *ResourceStatusRequestData, maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, withDebug bool) ([]byte, string, error) {
56
57         packedBuf := make([]byte, maxAsn1PackedBufferSize)
58         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
59         packedBufSize := C.ulong(len(packedBuf))
60         pduAsString := ""
61
62         var pLMNIdentifier, eUTRANCellIdentifier string
63
64         if _, err := fmt.Sscanf(request.CellID, "%x:%x", &pLMNIdentifier, &eUTRANCellIdentifier); err != nil {
65                 return nil, "", fmt.Errorf("BuildPackedResourceStatusRequest() - unexpected CellID value [%s] (want: \"<PLMNIdentifier>:<eUTRANCellIdentifier>\"), err: %s", request.CellID, err)
66         }
67
68         /*
69         9.2.0   General
70         When specifying information elements which are to be represented by bit strings, if not otherwise specifically stated in the semantics description of the concerned IE or elsewhere, the following principle applies with regards to the ordering of bits:
71         -       The first bit (leftmost bit) contains the most significant bit (MSB);
72         -       The last bit (rightmost bit) contains the least significant bit (LSB);
73         -       When importing bit strings from other specifications, the first bit of the bit string contains the first bit of the concerned information.
74
75         */
76         /*reportCharacteristics:
77         1)    First Bit = PRB Periodic.
78         2)    Second Bit = TNL load Ind Periodic.
79         3)    Third Bit = HW Load Ind Periodic.
80         4)    Fourth Bit = Composite Available Capacity Periodic, this bit should be set to 1 if at least one of the First, Second or Third bits is set to 1.
81         5)    Fifth Bit = ABPartialSuccessIndicator_tS Status Periodic.
82         6)    Sixth Bit = RSRP Measurement Report Periodic.
83         7)    Seventh Bit = CSI Report Periodic.
84         */
85         var prbPeriodic, tnlLoadIndPeriodic, hwLoadIndPeriodic, compositeAvailablCapacityPeriodic, absStatusPeriodic, rsrpMeasurementPeriodic, csiPeriodic int
86         var partialSuccessAllowed C.PartialSuccessIndicator_t
87
88         if request.PartialSuccessAllowed {
89                 partialSuccessAllowed = C.PartialSuccessIndicator_partial_success_allowed
90         } else {
91                 partialSuccessAllowed = C.PartialSuccessIndicator_t(-1)
92         }
93         if request.PrbPeriodic {
94                 prbPeriodic = 1
95         }
96         if request.TnlLoadIndPeriodic {
97                 tnlLoadIndPeriodic = 1
98         }
99         if request.HwLoadIndPeriodic {
100                 hwLoadIndPeriodic = 1
101         }
102         if request.PrbPeriodic || request.TnlLoadIndPeriodic || request.HwLoadIndPeriodic {
103                 compositeAvailablCapacityPeriodic = 1
104         }
105         if request.AbsStatusPeriodic {
106                 absStatusPeriodic = 1
107         }
108         if request.RsrpMeasurementPeriodic {
109                 rsrpMeasurementPeriodic = 1
110         }
111         if request.CsiPeriodic {
112                 csiPeriodic = 1
113         }
114         reportCharacteristics := uint32(prbPeriodic<<7 | tnlLoadIndPeriodic<<6 | hwLoadIndPeriodic<<5 | compositeAvailablCapacityPeriodic<<4 | absStatusPeriodic<<3 | rsrpMeasurementPeriodic<<2 | csiPeriodic<<1)
115
116         if !C.build_pack_resource_status_request(
117                 (*C.uchar)(unsafe.Pointer(&[]byte(pLMNIdentifier)[0])),
118                 (*C.uchar)(unsafe.Pointer(&[]byte(eUTRANCellIdentifier)[0])),
119                 C.Measurement_ID_t(request.MeasurementID),
120                 C.Measurement_ID_t(request.MeasurementID2),
121                 C.Registration_Request_t(registrationRequest),
122                 C.uint(reportCharacteristics),
123                 C.ReportingPeriodicity_t(request.PeriodicityMS-1),
124                 partialSuccessAllowed,
125                 C.ReportingPeriodicityRSRPMR_t(request.PeriodicityRsrpMeasurementMS-1),
126                 C.ReportingPeriodicityCSIR_t(request.PeriodicityCsiMS-1),
127                 &packedBufSize,
128                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
129                 C.ulong(len(errBuf)),
130                 &errBuf[0]) {
131                 return nil, "", errors.New(fmt.Sprintf("BuildPackedResourceStatusRequest - packing error: %s", C.GoString(&errBuf[0])))
132         }
133
134         if withDebug {
135                 pdu := C.new_pdu()
136                 defer C.delete_pdu(pdu)
137                 if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
138                         C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
139                         pduAsString = C.GoString(&errBuf[0])
140                 }
141         }
142
143         return packedBuf[:packedBufSize], pduAsString, nil
144 }