Slice PM Counters VES Standard defined
[o-du/l2.git] / src / o1 / ves / SliceMeasurementEventStdDef.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2022] [HCL Technologies Ltd.]                          #
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
19 /* This file contains macros and functions to support the preparation of pnf
20    Registration VES Event*/
21
22 #include <iostream>
23 #include <chrono>
24 #include <iomanip>
25 #include <sstream>
26 #include "JsonHelper.hpp"
27 #include "SliceMeasurementEventStdDef.hpp"
28 #include "CmInterface.h"
29
30 extern NRCellDU cellParams;
31
32 /* Default constructor*/
33 SliceMeasurementEventStdDef::SliceMeasurementEventStdDef()
34                : PerfMeasurementEvent(VesEventType::PM_SLICE)
35 {
36
37 }
38
39 /* Default Destructor*/
40 SliceMeasurementEventStdDef::~SliceMeasurementEventStdDef()
41 {
42
43 }
44
45 /*******************************************************************
46  *
47  * @brief Returns ISO time String 
48  *
49  * @details
50  *
51  *    Function :getISOEventTime
52  *
53  *    Functionality:
54  *      - Returns ISO time String
55  *
56  * @params[in] IN - void
57  * @return value of string     - success
58  *         empty string        - failure
59  *
60  * ****************************************************************/
61
62 std::string SliceMeasurementEventStdDef::getISOEventTime() {
63    auto now = std::chrono::system_clock::now();
64    auto itt = std::chrono::system_clock::to_time_t(now);
65    std::ostringstream os;
66    os << std::put_time(gmtime(&itt), "%FT%TZ");
67    return os.str();
68 }
69
70 /*******************************************************************
71 *
72 * @brief prepare Stnd Slice Measurement path
73 *
74 * @details
75 *
76 * Function : getMeaspath
77 *
78 * Functionality:
79 * - prepare Stnd Slice Measurement path
80 *
81 * @params[in] IN - void
82 * @return true - path on success
83 *         false - failure
84 *
85 * ****************************************************************/
86 std::string SliceMeasurementEventStdDef::getMeasPath(string str, int sd, int sst)
87 {
88    O1_LOG("\n O1 SliceMeasurementEventStdDef cellLocalId :%d",cellParams.cellLocalId);
89    std::ostringstream os;
90    os<<"/"<<YANG_NAME<<":network-function/distributed-unit-functions[id='"\ 
91            << ODU_HIGH <<"']/cell[id='"<<cellParams.cellLocalId <<\ 
92            "']/supported-measurements[performance-measurement-type='user-equipment-average-throughput-"\ 
93            <<str<<"']/supported-snssai-subcounter-instances[slice-differentiator='"\ 
94            <<sd<<"'][slice-service-type='"<<sst<<"']" ;
95    return os.str();
96
97 }
98
99 /*******************************************************************
100 *
101 * @brief prepare Stnd Slice Measurement Fields
102 *
103 * @details
104 *
105 * Function : prepareEventFields
106 *
107 * Functionality:
108 * - prepare Stnd Slice Measurement Fields  in json format
109 *
110 * @params[in] IN - const pointer of Message type
111 * @return true - success
112 *         false - failure
113 *
114 * ****************************************************************/
115
116 bool SliceMeasurementEventStdDef::prepareEventFields(const Message* msg)
117 {
118
119    const SliceMetrics* sliceMetrics = dynamic_cast<const SliceMetrics*> (msg);
120
121    bool ret = true;
122
123    cJSON* stndDefinedFields = this->mVesEventFields;
124    if(JsonHelper::addNodeToObject(stndDefinedFields, "stndDefinedFieldsVersion",\
125                            STND_DEFINED_FEILD_VERSION) == 0) 
126    {
127       ret = false;
128    }
129    else if(JsonHelper::addNodeToObject(stndDefinedFields, "schemaReference",\
130                            SLICE_SCHEMA) == 0) 
131    {
132       ret = false;
133    }
134
135    cJSON* data = JsonHelper::createNode();
136    if(data == 0)
137    {
138       O1_LOG("\nO1 SliceMeasurementEventStdDef : could not create data JSON object");
139       return false;
140    }
141    else if(JsonHelper::addJsonNodeToObject(stndDefinedFields, "data", \
142                                        data) == 0) {
143       ret = false;
144    }
145    else if(JsonHelper::addNodeToObject(data, "id", SLICE_ID) == 0)
146    {
147       ret = false;
148    }
149    else if(JsonHelper::addNodeToObject(data, "start-time",\
150                            getISOEventTime().c_str()) == 0)
151    {
152       ret = false;
153    }
154    else if(JsonHelper::addNodeToObject(data, "administrative-state",\
155                            ADMINISTRATIVE_STATE) == 0)
156    {
157       ret = false;
158    }
159    else if(JsonHelper::addNodeToObject(data, "operational-state",\
160                            OPERATIONAL_STATE) == 0)
161    {
162       ret = false;
163    }
164    else if(JsonHelper::addNodeToObject(data, "user-label", USER_LABEL) == 0)
165    {
166       ret = false;
167    }
168    else if(JsonHelper::addNodeToObject(data, "job-tag", "") == 0)
169    {
170       ret = false;
171    }
172    else if(JsonHelper::addNodeToObject(data, "granularity-period",\
173                            GRANULARITY_PERIOD) == 0)
174    {
175       ret = false;
176    }
177
178    cJSON *measure= cJSON_CreateArray();
179    if (measure == NULL)
180    {
181       ret = false;
182    }
183    else if(cJSON_AddItemToObject(data, "measurements", measure) == 0)
184    {
185       ret = false;
186    }
187
188    else if(ret)
189    {
190       const vector<SliceMetricRecord>& sliceList = sliceMetrics->getSliceMetrics();
191       for (size_t i{0}; i < sliceList.size(); i++)
192       {
193          for(int j=0;j<MAX_THP_TYPE;j++)
194          {
195             cJSON* arr = JsonHelper::createNode();
196             cJSON_AddItemToArray(measure,arr);
197
198             string str;
199             cJSON *value;
200             if(j==0)
201             {
202                str = "downlink";
203                value = cJSON_CreateNumber(sliceList[i].DRB_UEThpDl_SNSSAI);
204             }
205             else
206             {
207                str = "uplink";
208                value = cJSON_CreateNumber(sliceList[i].DRB_UEThpUl_SNSSAI);
209             }
210
211             if(JsonHelper::addNodeToObject(arr, MEAS_REF, getMeasPath(str,\
212                                             sliceList[i].networkSliceIdentifier.sd,\
213                                             sliceList[i].networkSliceIdentifier.sst).c_str()) == 0)
214             {
215                ret = false;
216             }
217
218             cJSON_AddItemToObject(arr,"value", value);
219
220             cJSON *unit = cJSON_CreateString(THROUGHPUT_UNIT);
221             cJSON_AddItemToObject(arr,"unit", unit);
222          }
223       }
224    }
225
226    return ret;
227 }
228
229 /**********************************************************************
230          End of file
231 **********************************************************************/