Add header missing license header
[ric-plt/resource-status-manager.git] / RSM / e2pdus / resource_status_request.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 <stdlib.h>
27 // #include <asn1codec_utils.h>
28 // #include <resource_status_request_wrapper.h>
29 import "C"
30 import (
31         "fmt"
32         "github.com/pkg/errors"
33         "rsm/enums"
34         "unsafe"
35 )
36
37 const (
38         MaxAsn1PackedBufferSize       = 4096
39         MaxAsn1CodecMessageBufferSize = 4096
40 )
41
42 type Measurement_ID int64
43
44 type ResourceStatusRequestData struct {
45         CellIdList                   []string // PLMNIdentifier:eUTRANCellIdentifier
46         MeasurementID                Measurement_ID
47         MeasurementID2               Measurement_ID
48         PartialSuccessAllowed        bool
49         PrbPeriodic                  bool
50         TnlLoadIndPeriodic           bool
51         HwLoadIndPeriodic            bool
52         AbsStatusPeriodic            bool
53         RsrpMeasurementPeriodic      bool
54         CsiPeriodic                  bool
55         PeriodicityMS                enums.ReportingPeriodicity
56         PeriodicityRsrpMeasurementMS enums.ReportingPeriodicityRSRPMR
57         PeriodicityCsiMS             enums.ReportingPeriodicityCSIR
58 }
59
60 func BuildPackedResourceStatusRequest(registrationRequest enums.Registration_Request, request *ResourceStatusRequestData, maxAsn1PackedBufferSize int, maxAsn1CodecMessageBufferSize int, withDebug bool) ([]byte, string, error) {
61
62         packedBuf := make([]byte, maxAsn1PackedBufferSize)
63         errBuf := make([]C.char, maxAsn1CodecMessageBufferSize)
64         packedBufSize := C.ulong(len(packedBuf))
65         pduAsString := ""
66
67         pLMNIdentities := make([]*C.uchar, len(request.CellIdList))
68         eUTRANCellIdentifiers := make([]*C.uchar, len(request.CellIdList))
69
70         for i, cellID := range request.CellIdList {
71                 var pLMNIdentity, eUTRANCellIdentifier []byte
72                 if _, err := fmt.Sscanf(cellID, "%x:%x", &pLMNIdentity, &eUTRANCellIdentifier); err != nil {
73                         return nil, "", fmt.Errorf("BuildPackedResourceStatusRequest() - unexpected CellID value [%s]@%d (want: \"<PLMNIdentifier>:<eUTRANCellIdentifier>\"), err: %s", cellID, i, err)
74                 }
75                 pLMNIdentities[i], eUTRANCellIdentifiers[i] = (*C.uchar)(C.CBytes(pLMNIdentity)), (*C.uchar)(C.CBytes(eUTRANCellIdentifier))
76         }
77
78         defer func() {
79                 for _, cPtr := range pLMNIdentities {
80                         C.free(unsafe.Pointer(cPtr))
81
82                 }
83                 for _, cPtr := range eUTRANCellIdentifiers {
84                         C.free(unsafe.Pointer(cPtr))
85                 }
86
87         }()
88
89         /*
90                 9.2.0   General
91                 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:
92                 -       The first bit (leftmost bit) contains the most significant bit (MSB);
93                 -       The last bit (rightmost bit) contains the least significant bit (LSB);
94                 -       When importing bit strings from other specifications, the first bit of the bit string contains the first bit of the concerned information.
95
96         */
97         /*reportCharacteristics:
98         1)    First Bit = PRB Periodic.
99         2)    Second Bit = TNL load Ind Periodic.
100         3)    Third Bit = HW Load Ind Periodic.
101         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.
102         5)    Fifth Bit = ABPartialSuccessIndicator_tS Status Periodic.
103         6)    Sixth Bit = RSRP Measurement Report Periodic.
104         7)    Seventh Bit = CSI Report Periodic.
105         */
106         var prbPeriodic, tnlLoadIndPeriodic, hwLoadIndPeriodic, compositeAvailablCapacityPeriodic, absStatusPeriodic, rsrpMeasurementPeriodic, csiPeriodic int
107         var partialSuccessAllowed C.PartialSuccessIndicator_t
108
109         if request.PartialSuccessAllowed {
110                 partialSuccessAllowed = C.PartialSuccessIndicator_partial_success_allowed
111         } else {
112                 partialSuccessAllowed = C.PartialSuccessIndicator_t(-1)
113         }
114         if request.PrbPeriodic {
115                 prbPeriodic = 1
116         }
117         if request.TnlLoadIndPeriodic {
118                 tnlLoadIndPeriodic = 1
119         }
120         if request.HwLoadIndPeriodic {
121                 hwLoadIndPeriodic = 1
122         }
123         if request.PrbPeriodic || request.TnlLoadIndPeriodic || request.HwLoadIndPeriodic {
124                 compositeAvailablCapacityPeriodic = 1
125         }
126         if request.AbsStatusPeriodic {
127                 absStatusPeriodic = 1
128         }
129         if request.RsrpMeasurementPeriodic {
130                 rsrpMeasurementPeriodic = 1
131         }
132         if request.CsiPeriodic {
133                 csiPeriodic = 1
134         }
135         reportCharacteristics := uint32(prbPeriodic<<7 | tnlLoadIndPeriodic<<6 | hwLoadIndPeriodic<<5 | compositeAvailablCapacityPeriodic<<4 | absStatusPeriodic<<3 | rsrpMeasurementPeriodic<<2 | csiPeriodic<<1)
136
137         if !C.build_pack_resource_status_request(
138                 (**C.uchar)(unsafe.Pointer(&pLMNIdentities[0])),
139                 (**C.uchar)(unsafe.Pointer(&eUTRANCellIdentifiers[0])),
140                 C.ulong(len(request.CellIdList)),
141                 C.Measurement_ID_t(request.MeasurementID),
142                 C.Measurement_ID_t(request.MeasurementID2),
143                 C.Registration_Request_t(registrationRequest),
144                 C.uint(reportCharacteristics),
145                 C.ReportingPeriodicity_t(request.PeriodicityMS-1),
146                 partialSuccessAllowed,
147                 C.ReportingPeriodicityRSRPMR_t(request.PeriodicityRsrpMeasurementMS-1),
148                 C.ReportingPeriodicityCSIR_t(request.PeriodicityCsiMS-1),
149                 &packedBufSize,
150                 (*C.uchar)(unsafe.Pointer(&packedBuf[0])),
151                 C.ulong(len(errBuf)),
152                 &errBuf[0]) {
153                 return nil, "", errors.New(fmt.Sprintf("BuildPackedResourceStatusRequest - packing error: %s", C.GoString(&errBuf[0])))
154         }
155
156         if withDebug {
157                 pdu := C.new_pdu()
158                 defer C.delete_pdu(pdu)
159                 if C.per_unpack_pdu(pdu, packedBufSize, (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.size_t(len(errBuf)), &errBuf[0]) {
160                         C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0])
161                         pduAsString = C.GoString(&errBuf[0])
162                 }
163         }
164
165         return packedBuf[:packedBufSize], pduAsString, nil
166 }