nrf_profile.cpp 42.3 KB
Newer Older
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1
2
3
4
5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
7
8
 * file except in compliance with the License. You may obtain a copy of the
 * License at
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file nrf_profile.cpp
 \brief
 \author  Tien-Thinh NGUYEN
 \company Eurecom
 \date 2020
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
27
 \email: Tien-Thinh.Nguyen@eurecom.fr
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
28
29
30
 */

#include "nrf_profile.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
31

32
#include <unistd.h>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
33
#include <boost/algorithm/string/classification.hpp>
34
#include <boost/algorithm/string/split.hpp>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
35

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
36
#include "api_conversions.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
37
38
#include "logger.hpp"
#include "string.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
39
40

using namespace std;
41
using namespace oai::nrf::app;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
42

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
43
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
44
void nrf_profile::set_nf_instance_id(const std::string& instance_id) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
45
  nf_instance_id = instance_id;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
46
47
48
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
49
void nrf_profile::get_nf_instance_id(std::string& instance_id) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
50
  instance_id = nf_instance_id;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
51
52
}

53
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
54
55
56
std::string nrf_profile::get_nf_instance_id() const {
  return nf_instance_id;
}
57

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
58
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
59
void nrf_profile::set_nf_instance_name(const std::string& instance_name) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
60
  nf_instance_name = instance_name;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
61
62
63
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
64
void nrf_profile::get_nf_instance_name(std::string& instance_name) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
65
  instance_name = nf_instance_name;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
67

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
68
//------------------------------------------------------------------------------
69
std::string nrf_profile::get_nf_instance_name() const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
70
  return nf_instance_name;
71
72
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
73
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
75
76
void nrf_profile::set_nf_type(const nf_type_t& type) {
  nf_type = type;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
77

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
78
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
79
80
81
nf_type_t nrf_profile::get_nf_type() const {
  return nf_type;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
82

83
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
84
void nrf_profile::set_nf_status(const std::string& status) {
85
  Logger::nrf_app().debug("Set NF status to %s", status.c_str());
86
  std::unique_lock lock(heartbeart_mutex);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
87
  nf_status = status;
88
89
90
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
91
void nrf_profile::get_nf_status(std::string& status) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
92
  status = nf_status;
93
94
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
95
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
96
97
98
std::string nrf_profile::get_nf_status() const {
  return nf_status;
}
99

100
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
101
void nrf_profile::set_nf_heartBeat_timer(const int32_t& timer) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
102
  heartBeat_timer = timer;
103
104
105
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
106
void nrf_profile::get_nf_heartBeat_timer(int32_t& timer) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
107
  timer = heartBeat_timer;
108
109
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
110
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
111
112
113
int32_t nrf_profile::get_nf_heartBeat_timer() const {
  return heartBeat_timer;
}
114

115
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
116
117
118
void nrf_profile::set_nf_priority(const uint16_t& p) {
  priority = p;
}
119
120

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
121
122
123
void nrf_profile::get_nf_priority(uint16_t& p) const {
  p = priority;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
124
125

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
126
127
128
uint16_t nrf_profile::get_nf_priority() const {
  return priority;
}
129

130
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
131
132
133
void nrf_profile::set_nf_capacity(const uint16_t& c) {
  capacity = c;
}
134
135

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
136
137
138
void nrf_profile::get_nf_capacity(uint16_t& c) const {
  c = capacity;
}
139

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
140
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
141
142
143
uint16_t nrf_profile::get_nf_capacity() const {
  return capacity;
}
144

145
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
146
void nrf_profile::set_nf_snssais(const std::vector<snssai_t>& s) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
147
  snssais = s;
148
149
150
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
151
void nrf_profile::get_nf_snssais(std::vector<snssai_t>& s) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
152
  s = snssais;
153
154
}

155
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
156
157
158
void nrf_profile::add_snssai(const snssai_t& s) {
  snssais.push_back(s);
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
159

Rohan's avatar
Rohan committed
160
161
162
163
164
165
166
167
168
//------------------------------------------------------------------------------
void nrf_profile::set_fqdn(const std::string& fqdN) {
  fqdn = fqdN;
}

//------------------------------------------------------------------------------
std::string nrf_profile::get_fqdn() const {
  return fqdn;
}
Rohan's avatar
Rohan committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

//------------------------------------------------------------------------------
void nrf_profile::set_plmn_list(const std::vector<plmn_t>& s) {
  plmn_list = s;
}

//------------------------------------------------------------------------------
void nrf_profile::get_plmn_list(std::vector<plmn_t>& s) const {
  s = plmn_list;
}

//------------------------------------------------------------------------------
void nrf_profile::add_plmn_list(const plmn_t& s) {
  plmn_list.push_back(s);
}

185
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
186
void nrf_profile::set_nf_ipv4_addresses(const std::vector<struct in_addr>& a) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
187
  ipv4_addresses = a;
188
189
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
190
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
191
void nrf_profile::add_nf_ipv4_addresses(const struct in_addr& a) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
192
  ipv4_addresses.push_back(a);
193
}
194
//------------------------------------------------------------------------------
Rohan's avatar
Rohan committed
195
196
197
198
199
200
201
202
203
void nrf_profile::set_nf_ipv6_addresses(const std::vector<struct in6_addr>& a) {
  ipv6_addresses = a;
}

//------------------------------------------------------------------------------
void nrf_profile::add_nf_ipv6_addresses(const struct in6_addr& a) {
  ipv6_addresses.push_back(a);
}
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
204
void nrf_profile::get_nf_ipv4_addresses(std::vector<struct in_addr>& a) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
205
  a = ipv4_addresses;
206
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
207

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
208
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
209
void nrf_profile::set_json_data(const nlohmann::json& data) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
210
211
212
213
  json_data = data;
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
214
void nrf_profile::get_json_data(nlohmann::json& data) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
215
216
217
  data = json_data;
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
218
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
219
void nrf_profile::set_nf_services(const std::vector<nf_service_t>& n) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
220
221
222
223
  nf_services = n;
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
224
void nrf_profile::add_nf_service(const nf_service_t& n) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
225
226
227
228
  nf_services.push_back(n);
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
229
void nrf_profile::get_nf_services(std::vector<nf_service_t>& n) const {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
230
231
232
  n = nf_services;
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
233
//------------------------------------------------------------------------------
234
void nrf_profile::display() {
235
  Logger::nrf_app().debug("NF instance info");
236
237
  Logger::nrf_app().debug("\tInstance ID: %s", nf_instance_id.c_str());
  Logger::nrf_app().debug("\tInstance name: %s", nf_instance_name.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
238
239
  Logger::nrf_app().debug(
      "\tInstance type: %s", nf_type_e2str[nf_type].c_str());
240
241
242
243
  Logger::nrf_app().debug("\tStatus: %s", nf_status.c_str());
  Logger::nrf_app().debug("\tHeartBeat timer: %d", heartBeat_timer);
  Logger::nrf_app().debug("\tPriority: %d", priority);
  Logger::nrf_app().debug("\tCapacity: %d", capacity);
244
  // SNSSAIs
Rohan's avatar
Rohan committed
245
  if (!plmn_list.empty()) {
Rohan's avatar
Rohan committed
246
247
248
249
    for (auto s : plmn_list) {
      Logger::nrf_app().debug("\tPLMN List(MCC, MNC): %d, %s", s.mcc, s.mnc);
    }
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
250
  for (auto s : snssais) {
251
    Logger::nrf_app().debug("\tNNSSAI(SST, SD): %d, %s", s.sST, s.sD.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
252
  }
Rohan's avatar
Rohan committed
253
254
255
  if (!fqdn.empty()) {
    Logger::nrf_app().debug("\tFQDN: %s", fqdn.c_str());
  }
256
  // IPv4 Addresses
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
257
  for (auto address : ipv4_addresses) {
258
    Logger::nrf_app().debug("\tIPv4 Addr: %s", inet_ntoa(address));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
259
  }
Rohan's avatar
Rohan committed
260
  // ToDo : For ipv6 addresses
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
261
  if (!json_data.empty()) {
262
    Logger::nrf_app().debug("\tJson Data: %s", json_data.dump().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
263
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
264
265
266
267
268

  // NF services
  for (auto service : nf_services) {
    Logger::nrf_app().debug("\tNF Service: %s", service.to_string().c_str());
  }
269
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
270

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
271
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
272
273
274
275
bool nrf_profile::replace_profile_info(
    const std::string& path, const std::string& value) {
  Logger::nrf_app().debug(
      "Replace member %s with new value %s", path.c_str(), value.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
276
277
278
279
  if (path.compare("nfInstanceName") == 0) {
    nf_instance_name = value;
    return true;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
280

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
281
282
283
284
  if (path.compare("nfStatus") == 0) {
    nf_status = value;
    return true;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
285

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
286
287
288
289
290
291
292
293
294
  if (path.compare("nfType") == 0) {
    nf_type = api_conv::string_to_nf_type(value);
    return true;
  }

  if (path.compare("heartBeatTimer") == 0) {
    try {
      heartBeat_timer = std::stoi(value);
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
295
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
296
      Logger::nrf_app().debug("Bad value!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
297
298
299
300
301
302
      return false;
    }
  }

  if (path.compare("priority") == 0) {
    try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
303
      priority = (uint16_t) std::stoi(value);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
304
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
305
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
306
      Logger::nrf_app().debug("Bad value!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
307
308
309
310
311
312
      return false;
    }
  }

  if (path.compare("capacity") == 0) {
    try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
313
      capacity = (uint16_t) std::stoi(value);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
314
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
315
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
316
      Logger::nrf_app().debug("Bad value!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
317
318
319
320
      return false;
    }
  }

Rohan's avatar
Rohan committed
321
322
323
324
325
  if (path.compare("fqdn") == 0) {
    fqdn = value;
    return true;
  }

326
  // Replace an array
Rohan's avatar
Rohan committed
327
328
329
330
331
  if (path.compare("plmnList") == 0) {
    Logger::nrf_app().info("Does not support this operation for ipv4Addresses");
    return false;
  }

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
332
333
334
335
336
  if (path.compare("ipv4Addresses") == 0) {
    Logger::nrf_app().info("Does not support this operation for ipv4Addresses");
    return false;
  }

Rohan's avatar
Rohan committed
337
338
339
340
341
  if (path.compare("ipv6Addresses") == 0) {
    Logger::nrf_app().info("Does not support this operation for ipv6Addresses");
    return false;
  }

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
342
343
344
345
346
  if (path.compare("sNssais") == 0) {
    Logger::nrf_app().info("Does not support this operation for sNssais");
    return false;
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
347
348
349
350
351
  if (path.compare("nfServices") == 0) {
    Logger::nrf_app().info("Does not support this operation for nfServices");
    return false;
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
352
353
354
  return false;
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
355
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
356
357
bool nrf_profile::add_profile_info(
    const std::string& path, const std::string& value) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
358
  Logger::nrf_app().debug(
359
360
      "Add an array element (value, array member), or a new member (value, "
      "member):  %s, %s",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
361
362
      value.c_str(), path.c_str());

363
  // update an existing member
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  if (path.compare("nfInstanceName") == 0) {
    nf_instance_name = value;
    return true;
  }

  if (path.compare("nfStatus") == 0) {
    nf_status = value;
    return true;
  }

  if (path.compare("nfType") == 0) {
    nf_type = api_conv::string_to_nf_type(value);
    return true;
  }

  if (path.compare("heartBeatTimer") == 0) {
    try {
      heartBeat_timer = std::stoi(value);
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
383
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
384
385
386
387
388
389
390
      Logger::nrf_app().debug("Bad value!");
      return false;
    }
  }

  if (path.compare("priority") == 0) {
    try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
391
      priority = (uint16_t) std::stoi(value);
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
392
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
393
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
394
395
396
397
398
399
400
      Logger::nrf_app().debug("Bad value!");
      return false;
    }
  }

  if (path.compare("capacity") == 0) {
    try {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
401
      capacity = (uint16_t) std::stoi(value);
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
402
      return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
403
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
404
405
406
407
408
      Logger::nrf_app().debug("Bad value!");
      return false;
    }
  }

Rohan's avatar
Rohan committed
409
410
411
412
413
  if (path.compare("fqdn") == 0) {
    fqdn = value;
    return true;
  }

414
  // add an element to a list
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
415
  if (path.compare("ipv4Addresses") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
416
    std::string address  = value;
417
    struct in_addr addr4 = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
418
419
420
421
    unsigned char buf_in_addr[sizeof(struct in_addr)];
    if (inet_pton(AF_INET, util::trim(address).c_str(), buf_in_addr) == 1) {
      memcpy(&addr4, buf_in_addr, sizeof(struct in_addr));
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
422
423
      Logger::nrf_app().warn(
          "Address conversion: Bad value %s", util::trim(address).c_str());
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
424
      return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
425
426
427
428
429
430
    }
    Logger::nrf_app().debug("Added IPv4 Addr: %s", address.c_str());
    ipv4_addresses.push_back(addr4);
    return true;
  }

Rohan's avatar
Rohan committed
431
432
433
434
435
  // add an element to a list
  if (path.compare("ipv6Addresses") == 0) {
    std::string address   = value;
    struct in6_addr addr6 = {};
    unsigned char buf_in_addr[sizeof(struct in6_addr)];
Rohan's avatar
Rohan committed
436
    if (inet_pton(AF_INET6, util::trim(address).c_str(), buf_in_addr) == 1) {
Rohan's avatar
Rohan committed
437
438
439
440
441
442
443
444
445
446
447
      memcpy(&addr6, buf_in_addr, sizeof(struct in6_addr));
    } else {
      Logger::nrf_app().warn(
          "Address conversion: Bad value %s", util::trim(address).c_str());
      return false;
    }
    Logger::nrf_app().debug("Added IPv6 Addr: %s", address.c_str());
    ipv6_addresses.push_back(addr6);
    return true;
  }

448
  // add an element to a list of json object
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
449
  if (path.compare("sNssais") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
450
451
452
453
454
455
    Logger::nrf_app().info("Does not support this operation for sNssais");
    return false;
  }

  if (path.compare("nfServices") == 0) {
    Logger::nrf_app().info("Does not support this operation for nfServices");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
456
457
458
    return false;
  }

Rohan's avatar
Rohan committed
459
460
461
462
463
  if (path.compare("plmnList") == 0) {
    Logger::nrf_app().info("Does not support this operation for plmnList");
    return false;
  }

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
464
  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
465
466
467
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
468
469
470
bool nrf_profile::remove_profile_info(const std::string& path) {
  Logger::nrf_app().debug(
      "Remove an array element or a member: %s", path.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
  if (path.compare("nfInstanceName") == 0) {
    nf_instance_name = "";
    return true;
  }

  if (path.compare("nfStatus") == 0) {
    nf_status = "";
    return true;
  }

  if (path.compare("nfType") == 0) {
    nf_type = NF_TYPE_UNKNOWN;
    return true;
  }

  if (path.compare("heartBeatTimer") == 0) {
    heartBeat_timer = 0;
    return true;
  }

  if (path.compare("priority") == 0) {
    priority = 0;
    return true;
  }

  if (path.compare("capacity") == 0) {
    priority = 0;
    return true;
  }

Rohan's avatar
Rohan committed
501
502
503
504
505
  if (path.compare("fqdn") == 0) {
    fqdn = "";
    return true;
  }

506
  // path: e.g., /ipv4Addresses/4
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
507
  if (path.find("ipv4Addresses") != std::string::npos) {
508
    std::vector<std::string> parts;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
509
510
511
512
513
    boost::split(parts, path, boost::is_any_of("/"), boost::token_compress_on);
    if (parts.size() != 2) {
      Logger::nrf_app().warn("Bad value for path: %s ", path.c_str());
      return false;
    }
514
    // get and check index
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
515
516
517
    uint32_t index = 0;
    try {
      index = std::stoi(parts.at(1));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
518
    } catch (const std::exception& err) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
519
520
521
522
523
524
525
526
      Logger::nrf_app().warn("Bad value for path: %s ", path.c_str());
      return false;
    }

    if (index >= ipv4_addresses.size()) {
      Logger::nrf_app().warn("Bad value for path: %s ", path.c_str());
      return false;
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
527
528
      Logger::nrf_app().debug(
          "Removed IPv4 Addr: %s", inet_ntoa(ipv4_addresses[index]));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
529
530
531
532
533
      ipv4_addresses.erase(ipv4_addresses.begin() + index);
      return true;
    }
  }

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
534
  if (path.find("sNssais") != std::string::npos) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
535
536
537
538
539
540
    Logger::nrf_app().info("Does not support this operation for sNssais");
    return false;
  }

  if (path.find("nfServices") != std::string::npos) {
    Logger::nrf_app().info("Does not support this operation for nfServices");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
541
542
543
    return false;
  }

Rohan's avatar
Rohan committed
544
545
546
547
548
  if (path.find("plmnList") != std::string::npos) {
    Logger::nrf_app().info("Does not support this operation for plmnList");
    return false;
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
549
550
551
552
  Logger::nrf_app().debug("Member (%s) not found!", path.c_str());
  return false;
}

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
553
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
554
555
void nrf_profile::to_json(nlohmann::json& data) const {
  data["nfInstanceId"]   = nf_instance_id;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
556
  data["nfInstanceName"] = nf_instance_name;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
557
558
  data["nfType"]         = nf_type_e2str[nf_type];
  data["nfStatus"]       = nf_status;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
559
  data["heartBeatTimer"] = heartBeat_timer;
560
  // SNSSAIs
Rohan's avatar
Rohan committed
561
  if (!snssais.empty()) {
Rohan's avatar
Rohan committed
562
563
564
565
566
567
568
    data["sNssais"] = nlohmann::json::array();
    for (auto s : snssais) {
      nlohmann::json tmp = {};
      tmp["sst"]         = s.sST;
      tmp["sd"]          = s.sD;
      data["sNssais"].push_back(tmp);
    }
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
569
  }
Rohan's avatar
Rohan committed
570
571
572
  if (!fqdn.empty()) {
    data["fqdn"] = fqdn;
  }
Rohan's avatar
Rohan committed
573
574
  if (!plmn_list.empty()) {
    data["plmnList"] = nlohmann::json::array();
Rohan's avatar
Rohan committed
575
576
577
578
579
580
581
    for (auto s : plmn_list) {
      nlohmann::json tmp = {};
      tmp["mcc"]         = s.mcc;
      tmp["mnc"]         = s.mnc;
      data["plmnList"].push_back(tmp);
    }
  }
582
  // ipv4_addresses
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
583
584
585
586
587
  data["ipv4Addresses"] = nlohmann::json::array();
  for (auto address : ipv4_addresses) {
    nlohmann::json tmp = inet_ntoa(address);
    data["ipv4Addresses"].push_back(tmp);
  }
Rohan's avatar
Rohan committed
588
589
590
591
592
593
  // // ipv6_addresses
  // data["ipv6Addresses"] = nlohmann::json::array();
  // for (auto address : ipv6_addresses) {
  //   nlohmann::json tmp = inet_ntoa(address);
  //   data["ipv6Addresses"].push_back(tmp);
  // }
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
594
595
  data["priority"] = priority;
  data["capacity"] = capacity;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
596
597
598
  // NF services
  data["nfServices"] = nlohmann::json::array();
  for (auto service : nf_services) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
599
    nlohmann::json srv_tmp       = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
600
    srv_tmp["serviceInstanceId"] = service.service_instance_id;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
601
602
    srv_tmp["serviceName"]       = service.service_name;
    srv_tmp["versions"]          = nlohmann::json::array();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
603
    for (auto v : service.versions) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
604
      nlohmann::json v_tmp     = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
605
      v_tmp["apiVersionInUri"] = v.api_version_in_uri;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
606
      v_tmp["apiFullVersion"]  = v.api_full_version;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
607
608
      srv_tmp["versions"].push_back(v_tmp);
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
609
    srv_tmp["scheme"]          = service.scheme;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
610
611
612
    srv_tmp["nfServiceStatus"] = service.nf_service_status;
    data["nfServices"].push_back(srv_tmp);
  }
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
613
614
615
  data["json_data"] = json_data;
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
616
//------------------------------------------------------------------------------
617
void nrf_profile::subscribe_heartbeat_timeout_nfregistration(uint64_t ms) {
618
  // For the first timeout, we use 2*HEART_BEAT_TIMER as interval
619
  struct itimerspec its;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
620
621
  its.it_value.tv_sec  = 2 * HEART_BEAT_TIMER;  // seconds
  its.it_value.tv_nsec = 0;                     // 100 * 1000 * 1000; //100ms
622
623
624
  const uint64_t interval =
      its.it_value.tv_sec * 1000 +
      its.it_value.tv_nsec / 1000000;  // convert sec, nsec to msec
625

626
  Logger::nrf_app().debug(
627
628
629
      "Subscribe to the HeartBeartTimer expire event (after NF "
      "registration): interval %d, current time %ld",
      2 * HEART_BEAT_TIMER, ms);
630
  first_hb_connection = m_event_sub.subscribe_task_tick(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
631
632
      boost::bind(
          &nrf_profile::handle_heartbeart_timeout_nfregistration, this, _1),
633
      interval, ms + interval);
634
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
635

636
637
//------------------------------------------------------------------------------
void nrf_profile::subscribe_heartbeat_timeout_nfupdate(uint64_t ms) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
638
  struct itimerspec its;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
639
640
  its.it_value.tv_sec  = HEART_BEAT_TIMER;  // Second
  its.it_value.tv_nsec = 0;                 // 100 * 1000 * 1000; //100ms
641
642
643
644
645
  const uint64_t interval =
      its.it_value.tv_sec * 1000 +
      its.it_value.tv_nsec / 1000000;  // convert sec, nsec to msec

  Logger::nrf_app().debug(
646
647
648
      "Subscribe to HeartbeatTimer expire event (after NF update): interval "
      "%d, current time %ld",
      HEART_BEAT_TIMER, ms);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
649

650
  if (first_update) {
651
652
653
654
655
    ms = ms + 2000;  // Not a realtime NF: adding 2000ms interval between the
                     // expected NF update message and HBT
    task_connection = m_event_sub.subscribe_task_tick(
        boost::bind(&nrf_profile::handle_heartbeart_timeout_nfupdate, this, _1),
        interval, ms + interval);
656
    first_update = false;
657
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
658
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
659

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
660
//------------------------------------------------------------------------------
661
662
663
bool nrf_profile::unsubscribe_heartbeat_timeout_nfupdate() {
  if (task_connection.connected()) {
    task_connection.disconnect();
664
665
    Logger::nrf_app().debug(
        "Unsubscribe to the Heartbeat Timer timeout event (after NF Update)");
666
667
668
669
670
671
672
673
674
675
676
    return true;
  } else {
    return false;
  }
}

//------------------------------------------------------------------------------
bool nrf_profile::unsubscribe_heartbeat_timeout_nfregistration() {
  if (first_hb_connection.connected()) {
    first_hb_connection.disconnect();
    Logger::nrf_app().debug(
677
678
        "Unsubscribe to the first Heartbeat Timer timeout event (after NF "
        "Registration)");
679
680
681
682
    return true;
  } else {
    return false;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
683
684
685
686
}

//------------------------------------------------------------------------------
void nrf_profile::handle_heartbeart_timeout(uint64_t ms) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
687
688
689
  Logger::nrf_app().info(
      "\nHandle heartbeart timeout, NF instance ID %s, time %d",
      nf_instance_id.c_str(), ms);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
690
  set_nf_status("SUSPENDED");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
691
692
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
693
//------------------------------------------------------------------------------
694
695
void nrf_profile::handle_heartbeart_timeout_nfregistration(uint64_t ms) {
  Logger::nrf_app().info(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
696
697
      "\nHandle the first Heartbeat timeout, NF instance ID %s, current time "
      "%d",
698
      nf_instance_id.c_str(), ms);
699
  // Set status to SUSPENDED and unsubscribe to the HBT
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
700
701
702
  if (!is_updated) {
    set_nf_status("SUSPENDED");
  }
703
704

  set_status_updated(false);
705
  unsubscribe_heartbeat_timeout_nfregistration();
706
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
707
708

//------------------------------------------------------------------------------
709
710
711
712
713
void nrf_profile::handle_heartbeart_timeout_nfupdate(uint64_t ms) {
  uint64_t current_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                            std::chrono::system_clock::now().time_since_epoch())
                            .count();
  Logger::nrf_app().info(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
714
715
      "\nHandle heartbeart timeout (NF update), NF instance ID %s, time %ld, "
      "current "
716
717
718
719
720
      "ms %ld",
      nf_instance_id.c_str(), ms, current_ms);
  if (!is_updated) {
    set_nf_status("SUSPENDED");
  }
721
  set_status_updated(false);
722
723
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
724
//------------------------------------------------------------------------------
725
726
727
728
void nrf_profile::set_status_updated(bool status) {
  std::unique_lock lock(heartbeart_mutex);
  is_updated = status;
}
729

730
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
731
732
733
void amf_profile::add_amf_info(const amf_info_t& info) {
  amf_info = info;
}
734
735

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
736
737
738
void amf_profile::get_amf_info(amf_info_t& info) const {
  info = amf_info;
}
739
740
741

//------------------------------------------------------------------------------
void amf_profile::display() {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
742
  nrf_profile::display();
743
  Logger::nrf_app().debug("\tAMF Info");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
744
745
746
  Logger::nrf_app().debug(
      "\t\tAMF Set ID: %s, AMF Region ID: %s", amf_info.amf_set_id.c_str(),
      amf_info.amf_region_id.c_str());
747

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
748
  for (auto g : amf_info.guami_list) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
749
    Logger::nrf_app().debug("\t\tAMF GUAMI List, AMF_ID: %s", g.amf_id.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
750
751
752
    Logger::nrf_app().debug(
        "\t\tAMF GUAMI List, PLMN (MCC: %s, MNC: %s)", g.plmn.mcc.c_str(),
        g.plmn.mnc.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
753
  }
754
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
755

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
756
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
757
758
bool amf_profile::replace_profile_info(
    const std::string& path, const std::string& value) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
759
  bool result = nrf_profile::replace_profile_info(path, value);
760
761
  if (result) return true;
  // for AMF info
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
762
  if (path.compare("amfInfo") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
763
    Logger::nrf_app().debug("Do not support this operation for amfInfo");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
764
765
766
    return false;
  }

767
768
769
770
771
772
773
774
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
      (path.compare("priority") != 0) and (path.compare("amfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
775
776
777
778
779
    Logger::nrf_app().debug("Member (%s) not found!", path.c_str());
    return false;
  }

  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
780
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
781
782

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
783
784
bool amf_profile::add_profile_info(
    const std::string& path, const std::string& value) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
785
  bool result = nrf_profile::add_profile_info(path, value);
786
  if (result) return true;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
787

788
  // add an element to a list of json object
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
789
  if (path.compare("amfInfo") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
790
    Logger::nrf_app().info("Do not support this operation for amfInfo");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
791
792
793
    return false;
  }

794
795
796
797
798
799
800
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
801
802
      (path.compare("priority") != 0) and (path.compare("nfServices") != 0) and
      (path.compare("amfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
803
    Logger::nrf_app().debug("Add new member: %s", path.c_str());
804
    // add new member
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
805
806
807
808
809
810
811
812
    json_data[path] = value;
    return true;
  }

  return false;
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
813
bool amf_profile::remove_profile_info(const std::string& path) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
814
  bool result = nrf_profile::remove_profile_info(path);
815
816
  if (result) return true;
  // for AMF info
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
817
  if (path.compare("amfInfo") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
818
    Logger::nrf_app().debug("Do not support this operation for amfInfo");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
819
820
821
    return false;
  }

822
823
824
825
826
827
828
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
829
830
      (path.compare("priority") != 0) and (path.compare("nfServices") != 0) and
      (path.compare("amfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
831
832
833
834
835
    Logger::nrf_app().debug("Member (%s) not found!", path.c_str());
    return false;
  }

  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
836
837
838
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
839
void amf_profile::to_json(nlohmann::json& data) const {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
840
  nrf_profile::to_json(data);
841
  // AMF Info
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
842
  data["amfInfo"]["amfSetId"]    = amf_info.amf_set_id;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
843
  data["amfInfo"]["amfRegionId"] = amf_info.amf_region_id;
844
  // guamiList
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
845
846
  data["amfInfo"]["guamiList"] = nlohmann::json::array();
  for (auto guami : amf_info.guami_list) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
847
848
    nlohmann::json tmp   = {};
    tmp["amfId"]         = guami.amf_id;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
849
850
851
852
    tmp["plmnId"]["mnc"] = guami.plmn.mnc;
    tmp["plmnId"]["mcc"] = guami.plmn.mcc;
    data["amfInfo"]["guamiList"].push_back(tmp);
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
853
854
855
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
856
857
858
void smf_profile::add_smf_info(const smf_info_t& info) {
  smf_info = info;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
859
860

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
861
862
863
void smf_profile::get_smf_info(smf_info_t& infos) const {
  infos = smf_info;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
864
865
866
867

//------------------------------------------------------------------------------
void smf_profile::display() {
  nrf_profile::display();
868
  Logger::nrf_app().debug("\tSMF Info");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
869
870
  for (auto s : smf_info.snssai_smf_info_list) {
    Logger::nrf_app().debug(
871
        "\t\tSNSSAI SMF Info List, SNSSAI (SD: %s, SST: %d)",
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
872
873
        s.snssai.sD.c_str(), s.snssai.sST);
    for (auto d : s.dnn_smf_info_list) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
874
875
      Logger::nrf_app().debug(
          "\t\tSNSSAI SMF Info List, DNN List: %s", d.dnn.c_str());
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
876
877
    }
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
878
879
880
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
881
882
bool smf_profile::add_profile_info(
    const std::string& path, const std::string& value) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
883
  bool result = nrf_profile::add_profile_info(path, value);
884
  if (result) return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
885

886
  // add an element to a list of json object
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
887
888
889
890
891
  if (path.compare("smfInfo") == 0) {
    Logger::nrf_app().info("Does not support this operation for smfInfo");
    return false;
  }

892
893
894
895
896
897
898
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
899
900
      (path.compare("priority") != 0) and (path.compare("nfServices") != 0) and
      (path.compare("smfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
901
    Logger::nrf_app().debug("Add new member: %s", path.c_str());
902
    // add new member
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
903
904
905
906
    json_data[path] = value;
    return true;
  }
  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
907
908
}

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
909
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
910
911
bool smf_profile::replace_profile_info(
    const std::string& path, const std::string& value) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
912
  bool result = nrf_profile::replace_profile_info(path, value);
913
914
  if (result) return true;
  // for SMF info
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
915
916
917
918
919
  if (path.compare("smfInfo") == 0) {
    Logger::nrf_app().debug("Does not support this operation for amfInfo");
    return false;
  }

920
921
922
923
924
925
926
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
927
928
      (path.compare("priority") != 0) and (path.compare("nfServices") != 0) and
      (path.compare("amfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
929
930
931
932
933
    Logger::nrf_app().debug("Member (%s) not found!", path.c_str());
    return false;
  }

  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
934
935
936
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
937
bool smf_profile::remove_profile_info(const std::string& path) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
938
  bool result = nrf_profile::remove_profile_info(path);
939
940
  if (result) return true;
  // for SMF info
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
941
  if (path.compare("smfInfo") == 0) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
942
    Logger::nrf_app().debug("Do not support this operation for smfInfo");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
943
944
945
    return false;
  }

946
947
948
949
950
951
952
  if ((path.compare("nfInstanceId") != 0) and
      (path.compare("nfInstanceName") != 0) and
      (path.compare("nfType") != 0) and (path.compare("nfStatus") != 0) and
      (path.compare("heartBeatTimer") != 0) and
      (path.compare("sNssais") != 0) and
      (path.compare("ipv4Addresses") != 0) and
      (path.compare("priority") != 0) and (path.compare("capacity") != 0) and
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
953
954
      (path.compare("priority") != 0) and (path.compare("nfServices") != 0) and
      (path.compare("smfInfo") != 0)) {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
955
956
957
958
959
    Logger::nrf_app().debug("Member (%s) not found!", path.c_str());
    return false;
  }

  return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
960
961
}

Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
962
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
963
void smf_profile::to_json(nlohmann::json& data) const {
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
964
  nrf_profile::to_json(data);
965
  // SMF Info
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
966
967
  data["smfInfo"]["sNssaiSmfInfoList"] = nlohmann::json::array();
  for (auto snssai : smf_info.snssai_smf_info_list) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
968
969
970
    nlohmann::json tmp    = {};
    tmp["sNssai"]["sst"]  = snssai.snssai.sST;
    tmp["sNssai"]["sd"]   = snssai.snssai.sD;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
971
972
    tmp["dnnSmfInfoList"] = nlohmann::json::array();
    for (auto d : snssai.dnn_smf_info_list) {
973
      nlohmann::json tmp_dnn = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
974
      tmp_dnn["dnn"]         = d.dnn;
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
975
976
977
978
979
      tmp["dnnSmfInfoList"].push_back(tmp_dnn);
    }
    data["smfInfo"]["sNssaiSmfInfoList"].push_back(tmp);
  }
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
980
981

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar