Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 1 | // vi: ts=4 sw=4 noet: |
| 2 | /* |
| 3 | ================================================================================== |
| 4 | Copyright (c) 2020 Nokia |
| 5 | Copyright (c) 2020 AT&T Intellectual Property. |
| 6 | |
| 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | you may not use this file except in compliance with the License. |
| 9 | You may obtain a copy of the License at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | ================================================================================== |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Mnemonic: ts_xapp.cpp |
| 23 | Abstract: Traffic Steering xApp; |
| 24 | 1. Receives A1 Policy |
| 25 | 2. Queries SDL to decide which UE to attempt Traffic Steering for |
| 26 | 3. Requests prediction for UE throughput on current and neighbor cells |
| 27 | 4. Receives prediction |
| 28 | 5. Optionally exercises Traffic Steering action over E2 |
| 29 | |
| 30 | Date: 22 April 2020 |
| 31 | Author: Ron Shacham |
| 32 | |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 39 | #include <thread> |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 40 | #include <iostream> |
| 41 | #include <memory> |
| 42 | |
| 43 | #include <sdl/syncstorage.hpp> |
| 44 | #include <set> |
| 45 | #include <map> |
| 46 | #include <vector> |
| 47 | #include <string> |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 48 | #include <unordered_map> |
| 49 | |
| 50 | #include <rapidjson/document.h> |
| 51 | #include <rapidjson/writer.h> |
| 52 | #include <rapidjson/stringbuffer.h> |
| 53 | #include <rapidjson/schema.h> |
| 54 | #include <rapidjson/reader.h> |
| 55 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 56 | |
| 57 | #include "ricxfcpp/xapp.hpp" |
| 58 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 59 | using namespace rapidjson; |
| 60 | using namespace std; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 61 | using Namespace = std::string; |
| 62 | using Key = std::string; |
| 63 | using Data = std::vector<uint8_t>; |
| 64 | using DataMap = std::map<Key, Data>; |
| 65 | using Keys = std::set<Key>; |
| 66 | |
| 67 | |
| 68 | // ---------------------------------------------------------- |
| 69 | |
| 70 | std::unique_ptr<Xapp> xfw; |
| 71 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 72 | std::string sdl_namespace_u = "TS-UE-metrics"; |
| 73 | std::string sdl_namespace_c = "TS-cell-metrics"; |
| 74 | |
| 75 | int rsrp_threshold = 0; |
| 76 | |
| 77 | std::unique_ptr<shareddatalayer::SyncStorage> sdl; |
| 78 | |
| 79 | Namespace nsu; |
| 80 | Namespace nsc; |
| 81 | |
| 82 | struct UEData { |
| 83 | string serving_cell; |
| 84 | int serving_cell_rsrp; |
| 85 | |
| 86 | }; |
| 87 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 88 | struct PolicyHandler : public BaseReaderHandler<UTF8<>, PolicyHandler> { |
| 89 | unordered_map<string, string> cell_pred; |
| 90 | std::string ue_id; |
| 91 | bool ue_id_found = false; |
| 92 | string curr_key = ""; |
| 93 | string curr_value = ""; |
| 94 | int policy_type_id; |
| 95 | int policy_instance_id; |
| 96 | int threshold; |
| 97 | std::string operation; |
| 98 | bool found_threshold = false; |
| 99 | |
| 100 | |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 101 | bool Null() { return true; } |
| 102 | bool Bool(bool b) { return true; } |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 103 | bool Int(int i) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 104 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 105 | if (curr_key.compare("policy_type_id") == 0) { |
| 106 | policy_type_id = i; |
| 107 | } else if (curr_key.compare("policy_instance_id") == 0) { |
| 108 | policy_instance_id = i; |
| 109 | } else if (curr_key.compare("threshold") == 0) { |
| 110 | found_threshold = true; |
| 111 | threshold = i; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | bool Uint(unsigned u) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 117 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 118 | if (curr_key.compare("policy_type_id") == 0) { |
| 119 | policy_type_id = u; |
| 120 | } else if (curr_key.compare("policy_instance_id") == 0) { |
| 121 | policy_instance_id = u; |
| 122 | } else if (curr_key.compare("threshold") == 0) { |
| 123 | found_threshold = true; |
| 124 | threshold = u; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 129 | bool Int64(int64_t i) { return true; } |
| 130 | bool Uint64(uint64_t u) { return true; } |
| 131 | bool Double(double d) { return true; } |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 132 | bool String(const char* str, SizeType length, bool copy) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 133 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 134 | if (curr_key.compare("operation") != 0) { |
| 135 | operation = str; |
| 136 | } |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | bool StartObject() { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 141 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | bool Key(const char* str, SizeType length, bool copy) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 145 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 146 | curr_key = str; |
| 147 | |
| 148 | return true; |
| 149 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 150 | bool EndObject(SizeType memberCount) { return true; } |
| 151 | bool StartArray() { return true; } |
| 152 | bool EndArray(SizeType elementCount) { return true; } |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 153 | |
| 154 | }; |
| 155 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 156 | struct PredictionHandler : public BaseReaderHandler<UTF8<>, PredictionHandler> { |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 157 | unordered_map<string, int> cell_pred_down; |
| 158 | unordered_map<string, int> cell_pred_up; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 159 | std::string ue_id; |
| 160 | bool ue_id_found = false; |
| 161 | string curr_key = ""; |
| 162 | string curr_value = ""; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 163 | bool down_val = true; |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 164 | bool Null() { return true; } |
| 165 | bool Bool(bool b) { return true; } |
| 166 | bool Int(int i) { return true; } |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 167 | bool Uint(unsigned u) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 168 | |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 169 | if (down_val) { |
| 170 | cell_pred_down[curr_key] = u; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 171 | down_val = false; |
| 172 | } else { |
| 173 | cell_pred_up[curr_key] = u; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 174 | down_val = true; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | |
| 179 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 180 | bool Int64(int64_t i) { return true; } |
| 181 | bool Uint64(uint64_t u) { return true; } |
| 182 | bool Double(double d) { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 183 | bool String(const char* str, SizeType length, bool copy) { |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 184 | |
| 185 | return true; |
| 186 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 187 | bool StartObject() { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 188 | bool Key(const char* str, SizeType length, bool copy) { |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 189 | if (!ue_id_found) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 190 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 191 | ue_id = str; |
| 192 | ue_id_found = true; |
| 193 | } else { |
| 194 | curr_key = str; |
| 195 | } |
| 196 | return true; |
| 197 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 198 | bool EndObject(SizeType memberCount) { return true; } |
| 199 | bool StartArray() { return true; } |
| 200 | bool EndArray(SizeType elementCount) { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | |
| 204 | struct UEDataHandler : public BaseReaderHandler<UTF8<>, UEDataHandler> { |
| 205 | unordered_map<string, string> cell_pred; |
| 206 | std::string serving_cell_id; |
| 207 | int serving_cell_rsrp; |
| 208 | int serving_cell_rsrq; |
| 209 | int serving_cell_sinr; |
| 210 | bool in_serving_array = false; |
| 211 | int rf_meas_index = 0; |
| 212 | |
| 213 | string curr_key = ""; |
| 214 | string curr_value = ""; |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 215 | bool Null() { return true; } |
| 216 | bool Bool(bool b) { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 217 | bool Int(int i) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 218 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 219 | if (in_serving_array) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 220 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 221 | switch(rf_meas_index) { |
| 222 | case 0: |
| 223 | serving_cell_rsrp = i; |
| 224 | break; |
| 225 | case 1: |
| 226 | serving_cell_rsrq = i; |
| 227 | break; |
| 228 | case 2: |
| 229 | serving_cell_sinr = i; |
| 230 | break; |
| 231 | } |
| 232 | rf_meas_index++; |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | bool Uint(unsigned u) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 237 | return true; } |
| 238 | bool Int64(int64_t i) { return true; } |
| 239 | bool Uint64(uint64_t u) { return true; } |
| 240 | bool Double(double d) { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 241 | bool String(const char* str, SizeType length, bool copy) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 242 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 243 | if (curr_key.compare("ServingCellID") == 0) { |
| 244 | serving_cell_id = str; |
| 245 | } |
| 246 | |
| 247 | return true; |
| 248 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 249 | bool StartObject() { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 250 | bool Key(const char* str, SizeType length, bool copy) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 251 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 252 | curr_key = str; |
| 253 | return true; |
| 254 | } |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 255 | bool EndObject(SizeType memberCount) { return true; } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 256 | bool StartArray() { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 257 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 258 | if (curr_key.compare("ServingCellRF") == 0) { |
| 259 | in_serving_array = true; |
| 260 | } |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | bool EndArray(SizeType elementCount) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 265 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 266 | if (curr_key.compare("servingCellRF") == 0) { |
| 267 | in_serving_array = false; |
| 268 | rf_meas_index = 0; |
| 269 | } |
| 270 | |
| 271 | return true; } |
| 272 | }; |
| 273 | |
| 274 | |
| 275 | unordered_map<string, UEData> get_sdl_ue_data() { |
| 276 | |
| 277 | fprintf(stderr, "In get_sdl_ue_data()\n"); |
| 278 | |
| 279 | unordered_map<string, string> ue_data; |
| 280 | |
| 281 | unordered_map<string, UEData> return_ue_data_map; |
| 282 | |
| 283 | std::string prefix3="12"; |
| 284 | Keys K2 = sdl->findKeys(nsu, prefix3); |
| 285 | DataMap Dk2 = sdl->get(nsu, K2); |
| 286 | |
| 287 | string ue_json; |
| 288 | string ue_id; |
| 289 | |
| 290 | for(auto si=K2.begin();si!=K2.end();++si){ |
| 291 | std::vector<uint8_t> val_v = Dk2[(*si)]; // 4 lines to unpack a string |
| 292 | char val[val_v.size()+1]; // from Data |
| 293 | int i; |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 294 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 295 | for(i=0;i<val_v.size();++i) val[i] = (char)(val_v[i]); |
| 296 | val[i]='\0'; |
| 297 | ue_id.assign((std::string)*si); |
| 298 | |
| 299 | ue_json.assign(val); |
| 300 | ue_data[ue_id] = ue_json; |
| 301 | } |
| 302 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 303 | for (auto map_iter = ue_data.begin(); map_iter != ue_data.end(); map_iter++) { |
| 304 | UEDataHandler handler; |
| 305 | Reader reader; |
| 306 | StringStream ss(map_iter->second.c_str()); |
| 307 | reader.Parse(ss,handler); |
| 308 | |
| 309 | string ueID = map_iter->first; |
| 310 | string serving_cell_id = handler.serving_cell_id; |
| 311 | int serv_rsrp = handler.serving_cell_rsrp; |
| 312 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 313 | return_ue_data_map[ueID] = {serving_cell_id, serv_rsrp}; |
| 314 | |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 317 | return return_ue_data_map; |
| 318 | } |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 319 | |
| 320 | void policy_callback( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) { |
| 321 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 322 | int response_to = 0; // max timeout wating for a response |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 323 | int rmtype; // received message type |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 324 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 325 | |
Ron Shacham | 8a53fc4 | 2020-05-26 16:47:33 -0400 | [diff] [blame] | 326 | cout << "Policy Callback got a message, type=" << mtype << " , length=" << len << endl; |
| 327 | cout << "payload is " << payload.get() << endl; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 328 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 329 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 330 | const char *arg = (const char*)payload.get(); |
| 331 | |
| 332 | PolicyHandler handler; |
| 333 | Reader reader; |
| 334 | StringStream ss(arg); |
| 335 | reader.Parse(ss,handler); |
| 336 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 337 | //Set the threshold value |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 338 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 339 | if (handler.found_threshold) { |
Ron Shacham | b12b0d3 | 2020-05-26 16:35:06 -0400 | [diff] [blame] | 340 | cout << "Setting RSRP Threshold to A1-P value: " << handler.threshold << endl; |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 341 | rsrp_threshold = handler.threshold; |
| 342 | } |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 343 | |
| 344 | mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" ); // validate that we can use the same buffer for 2 rts calls |
| 345 | mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" ); |
| 346 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 347 | |
| 348 | } |
| 349 | |
| 350 | void send_prediction_request(vector<string> ues_to_predict) { |
| 351 | |
| 352 | std::unique_ptr<Message> msg; |
| 353 | Msg_component payload; // special type of unique pointer to the payload |
| 354 | |
| 355 | int nthreads = 1; |
| 356 | int response_to = 0; // max timeout wating for a response |
| 357 | int mtype = 30000; |
| 358 | int sz; |
| 359 | int i; |
| 360 | Msg_component send_payload; |
| 361 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 362 | msg = xfw->Alloc_msg( 2048 ); |
| 363 | |
| 364 | sz = msg->Get_available_size(); // we'll reuse a message if we received one back; ensure it's big enough |
| 365 | if( sz < 2048 ) { |
| 366 | fprintf( stderr, "<SNDR> fail: message returned did not have enough size: %d [%d]\n", sz, i ); |
| 367 | exit( 1 ); |
| 368 | } |
| 369 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 370 | string ues_list = "["; |
| 371 | |
| 372 | for (int i = 0; i < ues_to_predict.size(); i++) { |
| 373 | if (i == ues_to_predict.size() - 1) { |
| 374 | ues_list = ues_list + " \"" + ues_to_predict.at(i) + "\""; |
| 375 | } else { |
| 376 | ues_list = ues_list + " \"" + ues_to_predict.at(i) + "\"" + ","; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | string message_body = "{\"UEPredictionSet\": " + ues_list + "}"; |
| 381 | |
| 382 | const char *body = message_body.c_str(); |
| 383 | |
| 384 | // char *body = "{\"UEPredictionSet\": [\"12345\"]}"; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 385 | |
| 386 | send_payload = msg->Get_payload(); // direct access to payload |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 387 | // snprintf( (char *) send_payload.get(), 2048, '{"UEPredictionSet" : ["12345"]}', 1 ); |
| 388 | // snprintf( (char *) send_payload.get(), 2048, body); |
| 389 | snprintf( (char *) send_payload.get(), 2048, "{\"UEPredictionSet\": [\"12345\"]}"); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 390 | |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 391 | fprintf(stderr, "message body %s\n", send_payload.get()); |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 392 | fprintf(stderr, "payload length %d\n", strlen( (char *) send_payload.get() )); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 393 | |
| 394 | // payload updated in place, nothing to copy from, so payload parm is nil |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 395 | if ( ! msg->Send_msg( mtype, Message::NO_SUBID, strlen( (char *) send_payload.get() ), NULL )) { |
E. Scott Daniels | f7b9695 | 2020-04-29 10:07:53 -0400 | [diff] [blame] | 396 | fprintf( stderr, "<SNDR> send failed: %d\n", msg->Get_state() ); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 397 | } |
| 398 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 399 | /* |
| 400 | msg = xfw->Receive( response_to ); |
| 401 | if( msg != NULL ) { |
| 402 | rmtype = msg->Get_mtype(); |
| 403 | send_payload = msg->Get_payload(); |
| 404 | fprintf( stderr, "got: mtype=%d payload=(%s)\n", rmtype, (char *) send_payload.get() ); |
| 405 | } |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 406 | */ |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 407 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 408 | } |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 409 | |
| 410 | void prediction_callback( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) { |
| 411 | |
| 412 | long now; |
| 413 | long total_count; |
| 414 | |
| 415 | int sz; |
| 416 | int i; |
| 417 | |
| 418 | int response_to = 0; // max timeout wating for a response |
| 419 | |
| 420 | int send_mtype = 0; |
| 421 | int rmtype; // received message type |
| 422 | int delay = 1000000; // mu-sec delay; default 1s |
| 423 | |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 424 | cout << "Prediction Callback got a message, type=" << mtype << " , length=" << len << "\n"; |
| 425 | cout << "payload is " << payload.get() << "\n"; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 426 | |
| 427 | mtype = 0; |
| 428 | |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 429 | const char* arg = (const char*)payload.get(); |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 430 | PredictionHandler handler; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 431 | |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 432 | try { |
| 433 | |
| 434 | Reader reader; |
| 435 | StringStream ss(arg); |
| 436 | reader.Parse(ss,handler); |
| 437 | } catch (...) { |
| 438 | cout << "got an exception on stringstream read parse\n"; |
| 439 | } |
| 440 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 441 | std::string pred_ue_id = handler.ue_id; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 442 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 443 | cout << "Prediction for " << pred_ue_id << endl; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 444 | |
| 445 | unordered_map<string, int> throughput_map = handler.cell_pred_down; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 446 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 447 | cout << endl; |
| 448 | |
| 449 | unordered_map<string, UEData> sdl_data = get_sdl_ue_data(); |
| 450 | |
| 451 | //Decision about CONTROL message |
| 452 | //(1) Identify UE Id in Prediction message |
| 453 | //(2) Get UEData struct for this UE Id |
| 454 | //(3) Identify the UE's service cell ID |
| 455 | //(4) Iterate through Prediction message. |
| 456 | // If one of the cells, have a higher throughput prediction than serving cell, log a CONTROL request |
| 457 | |
| 458 | UEData pred_ue_data = sdl_data[pred_ue_id]; |
| 459 | std::string serving_cell_id = pred_ue_data.serving_cell; |
| 460 | |
| 461 | int serving_cell_throughput; |
| 462 | int highest_throughput; |
| 463 | std::string highest_throughput_cell_id; |
| 464 | std::string::size_type str_size; |
| 465 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 466 | for (auto map_iter = throughput_map.begin(); map_iter != throughput_map.end(); map_iter++) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 467 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 468 | std::string curr_cellid = map_iter->first; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 469 | int curr_throughput = map_iter->second; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 470 | |
| 471 | if (curr_cellid.compare(serving_cell_id) == 0) { |
| 472 | serving_cell_throughput = curr_throughput; |
| 473 | highest_throughput = serving_cell_throughput; |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | //Iterating again to identify the highest throughput prediction |
| 479 | |
| 480 | for (auto map_iter = throughput_map.begin(); map_iter != throughput_map.end(); map_iter++) { |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 481 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 482 | std::string curr_cellid = map_iter->first; |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 483 | int curr_throughput = map_iter->second; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 484 | |
| 485 | if (curr_throughput > serving_cell_throughput) { |
| 486 | highest_throughput = curr_throughput; |
| 487 | highest_throughput_cell_id = curr_cellid; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (highest_throughput > serving_cell_throughput) { |
| 492 | cout << "WE WOULD SEND A CONTROL REQUEST NOW" << endl; |
| 493 | cout << "UE ID: " << pred_ue_id << endl; |
| 494 | cout << "Source cell " << serving_cell_id << endl; |
| 495 | cout << "Target cell " << highest_throughput_cell_id << endl; |
| 496 | } |
Ron Shacham | 3be133f | 2020-05-26 13:43:44 -0400 | [diff] [blame] | 497 | |
| 498 | mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" ); // validate that we can use the same buffer for 2 rts calls |
| 499 | mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" ); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 500 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 501 | |
| 502 | } |
| 503 | |
| 504 | |
| 505 | //This function runs a loop that continuously checks SDL for any UE |
| 506 | |
| 507 | void run_loop() { |
| 508 | |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 509 | cout << "in Traffic Steering run_loop()\n"; |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 510 | |
| 511 | unordered_map<string, UEData> uemap; |
| 512 | |
| 513 | vector<string> prediction_ues; |
| 514 | |
| 515 | while (1) { |
| 516 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 517 | uemap = get_sdl_ue_data(); |
| 518 | |
| 519 | for (auto map_iter = uemap.begin(); map_iter != uemap.end(); map_iter++) { |
| 520 | string ueid = map_iter->first; |
| 521 | UEData data = map_iter->second; |
| 522 | if (data.serving_cell_rsrp < rsrp_threshold) { |
| 523 | prediction_ues.push_back(ueid); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (prediction_ues.size() > 0) { |
| 528 | send_prediction_request(prediction_ues); |
| 529 | } |
| 530 | |
| 531 | sleep(20); |
| 532 | } |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Deepanshu Karnwal | 008ad98 | 2020-11-18 12:35:12 +0530 | [diff] [blame^] | 535 | /* This function works with Anomaly Detection(AD) xApp. It is invoked when anomalous UEs are send by AD xApp. |
| 536 | * It just print the payload received from AD xApp and send an ACK with same UEID as payload to AD xApp. |
| 537 | */ |
| 538 | void ad_callback( Message& mbuf, int mtype, int subid, int len, Msg_component payload, void* data ) { |
| 539 | cout << "payload is " << payload.get() << "\n"; |
| 540 | mbuf.Send_response(30004, -1, strlen((char *) payload.get()), (unsigned char *) payload.get()); |
| 541 | } |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 542 | |
| 543 | extern int main( int argc, char** argv ) { |
| 544 | |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 545 | int nthreads = 1; |
| 546 | |
E. Scott Daniels | f7b9695 | 2020-04-29 10:07:53 -0400 | [diff] [blame] | 547 | char* port = (char *) "4560"; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 548 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 549 | sdl = shareddatalayer::SyncStorage::create(); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 550 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 551 | nsu = Namespace(sdl_namespace_u); |
| 552 | nsc = Namespace(sdl_namespace_c); |
Ron Shacham | 085267b | 2020-05-20 22:09:23 -0400 | [diff] [blame] | 553 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 554 | |
| 555 | fprintf( stderr, "<XAPP> listening on port: %s\n", port ); |
Ron Shacham | ee5e434 | 2020-06-11 12:34:46 -0400 | [diff] [blame] | 556 | xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) ); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 557 | |
| 558 | xfw->Add_msg_cb( 20010, policy_callback, NULL ); |
| 559 | xfw->Add_msg_cb( 30002, prediction_callback, NULL ); |
Deepanshu Karnwal | 008ad98 | 2020-11-18 12:35:12 +0530 | [diff] [blame^] | 560 | xfw->Add_msg_cb( 30003, ad_callback, NULL ); /*Register a callback function for msg type 30003*/ |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 561 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 562 | std::thread loop_thread; |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 563 | |
Ron Shacham | e187d50 | 2020-05-08 12:14:44 -0400 | [diff] [blame] | 564 | loop_thread = std::thread(&run_loop); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 565 | |
| 566 | xfw->Run( nthreads ); |
Ron Shacham | e7dfeb8 | 2020-04-24 14:46:48 -0400 | [diff] [blame] | 567 | |
| 568 | } |