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