api_conversions.cpp 21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * 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
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *      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 api_conversions.cpp
 \brief
 \author  Tien-Thinh NGUYEN
 \company Eurecom
 \date 2020
 \email: tien-thinh.nguyen@eurecom.fr
 */

#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
38
39
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/time_parsers.hpp>
40
#include <nlohmann/json.hpp>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
41
#include <regex>
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
42

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
43
#include "3gpp_29.510.h"
44
#include "AmfInfo.h"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
45
#include "api_conversions.hpp"
46
#include "logger.hpp"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
47
48
#include "nrf.h"
#include "string.hpp"
49
50
51
52

using namespace oai::nrf::model;
using namespace oai::nrf::app;
using namespace oai::nrf;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
53
54

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
55
bool api_conv::profile_api_to_nrf_profile(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
56
    const NFProfile& api_profile, std::shared_ptr<nrf_profile>& profile) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
57
  Logger::nrf_app().debug(
58
      "Convert a json-type profile to a NF profile (profile ID: %s)",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
59
60
61
      api_profile.getNfInstanceId().c_str());

  profile.get()->set_nf_instance_id(api_profile.getNfInstanceId());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
62
  profile.get()->set_nf_instance_name(api_profile.getNfInstanceName());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
63
64
  Logger::nrf_app().debug(
      "\tInstance name: %s", profile.get()->get_nf_instance_name().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
65

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
  profile.get()->set_nf_status(api_profile.getNfStatus());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
67
68
  Logger::nrf_app().debug(
      "\tStatus: %s", profile.get()->get_nf_status().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
69
  profile.get()->set_nf_heartBeat_timer(api_profile.getHeartBeatTimer());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
70
71
  Logger::nrf_app().debug(
      "\tHeartBeart timer: %d", profile.get()->get_nf_heartBeat_timer());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
72
  profile.get()->set_nf_priority(api_profile.getPriority());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
73
  Logger::nrf_app().debug("\tPriority: %d", profile.get()->get_nf_priority());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
  profile.get()->set_nf_capacity(api_profile.getCapacity());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
75
  Logger::nrf_app().debug("\tCapacity: %d", profile.get()->get_nf_capacity());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
76
  // SNSSAIs
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
77
78
  std::vector<Snssai> snssai = api_profile.getSNssais();
  for (auto s : snssai) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
79
    snssai_t sn = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
80
81
    sn.sD       = s.getSd();
    sn.sST      = s.getSst();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
82
    profile.get()->add_snssai(sn);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
83
84
    Logger::nrf_app().debug(
        "\tSNSSAI (SD, SST): %d, %s", sn.sST, sn.sD.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
85
  }
Rohan's avatar
Rohan committed
86
87
88
89
90
91
92
93
94
95
96
97
  if (api_profile.plmnListIsSet()) {
    NFProfile nf_profile        = api_profile;
    std::vector<PlmnId>& plmnid = nf_profile.getPlmnList();
    for (auto s : plmnid) {
      plmn_t sn = {};
      sn.mcc    = s.getMcc();
      sn.mnc    = s.getMnc();
      profile.get()->add_plmn_list(sn);
      Logger::nrf_app().debug(
          "\tPLMN_List (MCC, MNC): %s, %s", sn.mcc.c_str(), sn.mnc.c_str());
    }
  }
Rohan's avatar
Rohan committed
98
99
100
101
  if (api_profile.fqdnIsSet()) {
    profile.get()->set_fqdn(api_profile.getFqdn());
    Logger::nrf_app().debug("\tFQDN: %s", api_profile.getFqdn().c_str());
  }
Rohan's avatar
Rohan committed
102
103
104
105
106
107
108
109
110
111
112
  if (api_profile.ipv4AddressesIsSet()) {
    std::vector<std::string> ipv4_addr_str = api_profile.getIpv4Addresses();
    for (auto address : ipv4_addr_str) {
      struct in_addr addr4 = {};
      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 {
        Logger::nrf_app().warn(
            "Address conversion: Bad value %s", util::trim(address).c_str());
      }
113

Rohan's avatar
Rohan committed
114
115
116
      Logger::nrf_app().debug("\tIPv4 Addr: %s", address.c_str());
      profile.get()->add_nf_ipv4_addresses(addr4);
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
117
  }
Rohan's avatar
Rohan committed
118
119
120
121
122
123
  // ToDo: For ipv6
  // if (api_profile.ipv6AddressesIsSet()) {
  //   std::vector<Ipv6Addr> ipv6_addr = api_profile.getIpv6Addresses();
  //   for (auto address : ipv6_addr) {
  //     struct in6_addr addr6 = {};
  //     unsigned char buf_in_addr[sizeof(struct in6_addr)];
Rohan's avatar
Rohan committed
124
125
  //     if (inet_pton(AF_INET6, util::trim(address).c_str(), buf_in_addr) ==
  //     1)
Rohan's avatar
Rohan committed
126
127
128
129
  //     {
  //       memcpy(&addr6, buf_in_addr, sizeof(struct in6_addr));
  //     } else {
  //       Logger::nrf_app().warn(
Rohan's avatar
Rohan committed
130
131
  //           "Address conversion: Bad value %s",
  //           util::trim(address).c_str());
Rohan's avatar
Rohan committed
132
133
134
135
136
137
  //     }

  //     Logger::nrf_app().debug("\tIPv6 Addr: %s", address.c_str());
  //     profile.get()->add_nf_ipv6_addresses(addr6);
  //   }
  // }
138

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
139
140
141
142
  nf_type_t nf_type = string_to_nf_type(api_profile.getNfType());

  switch (nf_type) {
    case NF_TYPE_AMF: {
143
      Logger::nrf_app().debug("\tAMF profile, AMF Info");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
144
      profile.get()->set_nf_type(NF_TYPE_AMF);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
145
      amf_info_t info      = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
146
      AmfInfo amf_info_api = api_profile.getAmfInfo();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
147
148
      info.amf_region_id   = amf_info_api.getAmfRegionId();
      info.amf_set_id      = amf_info_api.getAmfSetId();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
149

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
150
151
152
      Logger::nrf_app().debug(
          "\t\tAMF Set ID: %s, AMF Region ID: %s", info.amf_set_id.c_str(),
          info.amf_region_id.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
153
154

      for (auto g : amf_info_api.getGuamiList()) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
155
156
        guami_t guami  = {};
        guami.amf_id   = g.getAmfId();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
157
158
159
        guami.plmn.mcc = g.getPlmnId().getMcc();
        guami.plmn.mnc = g.getPlmnId().getMnc();
        info.guami_list.push_back(guami);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
160
161
162
163
164
        Logger::nrf_app().debug(
            "\t\tAMF GUAMI, AMF_ID:  %s", guami.amf_id.c_str());
        Logger::nrf_app().debug(
            "\t\tAMF GUAMI, PLMN (MCC: %s, MNC: %s)", guami.plmn.mcc.c_str(),
            guami.plmn.mnc.c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
165
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
166
167
168
169
      (std::static_pointer_cast<amf_profile>(profile))
          .get()
          ->add_amf_info(info);
    } break;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
170
    case NF_TYPE_SMF: {
171
      Logger::nrf_app().debug("\tSMF profile, SMF Info");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
172
      profile.get()->set_nf_type(NF_TYPE_SMF);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
173
      smf_info_t info      = {};
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
174
175
176
      SmfInfo smf_info_api = api_profile.getSmfInfo();

      for (auto s : smf_info_api.getSNssaiSmfInfoList()) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
177
        snssai_smf_info_item_t snssai = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
178
179
180
181
182
        snssai.snssai.sD              = s.getSNssai().getSd();
        snssai.snssai.sST             = s.getSNssai().getSst();
        Logger::nrf_app().debug(
            "\t\tNSSAI SD: %s, SST: %d", snssai.snssai.sD.c_str(),
            snssai.snssai.sST);
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
183
184
        for (auto d : s.getDnnSmfInfoList()) {
          dnn_smf_info_item_t dnn = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
185
          dnn.dnn                 = d.getDnn();
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
186
          snssai.dnn_smf_info_list.push_back(dnn);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
187
          Logger::nrf_app().debug("\t\tDNN: %s", dnn.dnn.c_str());
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
188
189
190
191
        }
        info.snssai_smf_info_list.push_back(snssai);
      }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
192
193
194
      (std::static_pointer_cast<smf_profile>(profile))
          .get()
          ->add_smf_info(info);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
195

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
196
    } break;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    case NF_TYPE_UPF: {
      Logger::nrf_app().debug("\tUPF profile, UPF Info");
      profile.get()->set_nf_type(NF_TYPE_UPF);
      upf_info_t info      = {};
      UpfInfo upf_info_api = api_profile.getUpfInfo();

      for (auto s : upf_info_api.getSNssaiUpfInfoList()) {
        snssai_upf_info_item_t snssai = {};
        snssai.snssai.sD              = s.getSNssai().getSd();
        snssai.snssai.sST             = s.getSNssai().getSst();
        Logger::nrf_app().debug(
            "\t\tNSSAI SD: %s, SST: %d", snssai.snssai.sD.c_str(),
            snssai.snssai.sST);
        for (auto d : s.getDnnUpfInfoList()) {
          dnn_upf_info_item_t dnn = {};
          dnn.dnn                 = d.getDnn();
          snssai.dnn_upf_info_list.push_back(dnn);
          Logger::nrf_app().debug("\t\tDNN: %s", dnn.dnn.c_str());
        }
        info.snssai_upf_info_list.push_back(snssai);
      }

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
      if (upf_info_api.interfaceUpfInfoListIsSet()) {
        for (auto s : upf_info_api.getInterfaceUpfInfoList()) {
          interface_upf_info_item_t interface = {};
          nf_up_interface_type_t up_interface_type =
              string_to_up_interface_type(s.getInterfaceType());
          switch (up_interface_type) {
            case N3: {
              interface.interface_type = "N3";
            } break;
            case N6: {
              interface.interface_type = "N6";
            } break;
            case N9: {
              interface.interface_type = "N9";
            } break;
            case DATA_FORWARDING: {
              interface.interface_type = "DATA_FORWARDING";
            } break;
            default: {
              interface.interface_type = "UNKNOWN";
            }
          }
          if (s.endpointFqdnIsSet())
            interface.endpoint_fqdn = s.getEndpointFqdn();
          if (s.networkInstanceIsSet())
            interface.network_instance = s.getNetworkInstance();
          if (s.ipv4EndpointAddressesIsSet()) {
            std::vector<std::string> ipv4_addr_str =
                s.getIpv4EndpointAddresses();
            for (auto address : ipv4_addr_str) {
              struct in_addr addr4 = {};
              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 {
                Logger::nrf_app().warn(
                    "Address conversion: Bad value %s",
                    util::trim(address).c_str());
              }
              Logger::nrf_app().debug(
                  "\t\tEndpoint: %s, IPv4 Addr: %s, FQDN: %s, NWI: %s",
                  interface.interface_type.c_str(), address.c_str(),
Rohan's avatar
Rohan committed
262
263
                  interface.endpoint_fqdn.c_str(),
                  interface.network_instance.c_str());
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
              interface.ipv4_addresses.push_back(addr4);
            }
          }
          // ToDo for ipv6
          // if (s.ipv6EndpointAddressesIsSet()) {
          //   std::vector<Ipv6Addr> ipv6_addr_str =
          //       s.getIpv6EndpointAddresses();
          //   for (auto address : ipv6_addr_str) {
          //     struct in6_addr addr6 = {};
          //     unsigned char buf_in_addr[sizeof(struct in6_addr)];
          //     if (inet_pton(
          //             AF_INET, util::trim(address).c_str(), buf_in_addr) ==
          //             1) {
          //       memcpy(&addr6, buf_in_addr, sizeof(struct in6_addr));
          //     } else {
          //       Logger::nrf_app().warn(
          //           "Address conversion: Bad value %s",
          //           util::trim(address).c_str());
          //     }
          //     Logger::nrf_app().debug("\tIPv4 Addr: %s", address.c_str());
          //     interface.ipv6_addresses.push_back(addr6);
          //   }
          // }
          info.interface_upf_info_list.push_back(interface);
        }
      }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
291
292
293
      (std::static_pointer_cast<upf_profile>(profile))
          .get()
          ->add_upf_info(info);
Rohan's avatar
Rohan committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    } break;
    case NF_TYPE_AUSF: {
      Logger::nrf_app().debug("\tAUSF profile, AUSFF Info");
      profile.get()->set_nf_type(NF_TYPE_AUSF);
      ausf_info_t info       = {};
      AusfInfo ausf_info_api = api_profile.getAusfInfo();
      info.groupid           = ausf_info_api.getGroupId();
      for (auto s : ausf_info_api.getSupiRanges()) {
        supi_range_ausf_info_item_t supiRange = {};
        supiRange.supi_range.start            = s.getStart();
        supiRange.supi_range.end              = s.getEnd();
        supiRange.supi_range.pattern          = s.getPattern();
        info.supi_ranges.push_back(supiRange);
      }
      for (auto s : ausf_info_api.getRoutingIndicators()) {
        info.routing_indicator.push_back(s);
      }
      (std::static_pointer_cast<ausf_profile>(profile))
          .get()
          ->add_ausf_info(info);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
315
316
317
    } break;
    default: {
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
318
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
319

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
320
321
322
323
  // nf_services
  if (api_profile.nfServicesIsSet()) {
    std::vector<NFService> nf_services = api_profile.getNfServices();
    for (auto service : nf_services) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
324
      nf_service_t ns        = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
325
      ns.service_instance_id = service.getServiceInstanceId();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
326
327
      ns.service_name        = service.getServiceName();
      ns.scheme              = service.getScheme();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
328
329
      for (auto v : service.getVersions()) {
        nf_service_version_t version = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
330
331
        version.api_full_version     = v.getApiFullVersion();
        version.api_version_in_uri   = v.getApiVersionInUri();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
332
333
334
335
336
337
        ns.versions.push_back(version);
      }
      ns.nf_service_status = service.getNfServiceStatus();
      profile.get()->add_nf_service(ns);
    }
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
338
  return true;
339
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
340

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
341
342
//------------------------------------------------------------------------------
bool api_conv::subscription_api_to_nrf_subscription(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
343
    const SubscriptionData& api_sub, std::shared_ptr<nrf_subscription>& sub) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
344
345
346
  Logger::nrf_app().debug(
      "Convert a json-type Subscription data a NRF subscription data");
  sub.get()->set_notification_uri(api_sub.getNfStatusNotificationUri());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
347
348
  subscription_condition_t sub_condition = {};

349
  /*
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
350
  if (api_sub.subscrCondIsSet()) {
351
352
    subscription_condition_api_t sub_condition_api = {};
    api_sub.getSubscrCond(sub_condition_api);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
353
354
    switch (sub_condition_api.type) {
      case NF_INSTANCE_ID_COND: {
355
356
        Logger::nrf_app().debug(
            "Subscription condition type: NF_INSTANCE_ID_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
357
358
359
360
361
        sub_condition.type = NF_INSTANCE_ID_COND;
        sub_condition.nf_instance_id =
            sub_condition_api.nfInstanceIdCond.getNfInstanceId();
      } break;
      case NF_TYPE_COND: {
362
        Logger::nrf_app().debug("Subscription condition type: NF_TYPE_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
363
364
365
366
        sub_condition.type = NF_TYPE_COND;
        sub_condition.nf_type = sub_condition_api.nfTypeCond.getNfType();
      } break;
      case SERVICE_NAME_COND: {
367
368
        Logger::nrf_app().debug(
            "Subscription condition type: SERVICE_NAME_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
369
370
371
372
373
        sub_condition.type = SERVICE_NAME_COND;
        sub_condition.service_name =
            sub_condition_api.serviceNameCond.getServiceName();
      } break;
      case AMF_COND: {
374
        Logger::nrf_app().debug("Subscription condition type: AMF_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
375
376
377
378
379
380
381
        sub_condition.type = AMF_COND;
        sub_condition.amf_info.amf_set_id =
            sub_condition_api.amfCond.getAmfSetId();
        sub_condition.amf_info.amf_region_id =
            sub_condition_api.amfCond.getAmfRegionId();
      } break;
      case GUAMI_LIST_COND: {
Rohan's avatar
Rohan committed
382
383
        Logger::nrf_app().debug("Subscription condition type:
  GUAMI_LIST_COND"); sub_condition.type = GUAMI_LIST_COND;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
384
385
386
        // TODO:
      } break;
      case NETWOTK_SLICE_COND: {
387
388
        Logger::nrf_app().debug(
            "Subscription condition type: NETWOTK_SLICE_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
389
390
391
392
        sub_condition.type = NETWOTK_SLICE_COND;
        // TODO:
      } break;
      case NF_GROUP_COND: {
393
        Logger::nrf_app().debug("Subscription condition type: NF_GROUP_COND");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
394
395
396
397
        sub_condition.type = NF_GROUP_COND;
        // TODO:
      } break;
      default: {
398
399
        Logger::nrf_app().debug("Subscription condition type: Unknown");
        return false;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
400
401
402
      }
    }
  }
403
404
405
406
407
408
409
410
  */

  if (api_sub.subscrCondIsSet()) {
    nlohmann::json sub_condition_api = {};
    api_sub.getSubscrCond(sub_condition_api);

    if (sub_condition_api.find("NfInstanceIdCond") != sub_condition_api.end()) {
      sub_condition.type = NF_INSTANCE_ID_COND;
411
412
413
      sub_condition.nf_instance_id =
          sub_condition_api["NfInstanceIdCond"]["nfInstanceId"]
              .get<std::string>();
414
415
416
417
      Logger::nrf_app().debug(
          "Subscription condition type: NfInstanceIdCond, nfInstanceId: %s",
          sub_condition.nf_instance_id.c_str());
    }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
418

419
420
421
    if (sub_condition_api.find("NfTypeCond") != sub_condition_api.end()) {
      sub_condition.type = NF_TYPE_COND;
      sub_condition.nf_type =
422
          sub_condition_api["NfTypeCond"]["nfType"].get<std::string>();
423
424
425
426
427
428
429
      Logger::nrf_app().debug(
          "Subscription condition type: NfTypeCond, nf_type: %s",
          sub_condition.nf_type.c_str());
    }

    if (sub_condition_api.find("ServiceNameCond") != sub_condition_api.end()) {
      sub_condition.type = SERVICE_NAME_COND;
430
431
432
      sub_condition.service_name =
          sub_condition_api["ServiceNameCond"]["serviceName"]
              .get<std::string>();
433
434
435
436
437
438
439
      Logger::nrf_app().debug(
          "Subscription condition type: ServiceNameCond, serviceName: %s",
          sub_condition.service_name.c_str());
    }
    if (sub_condition_api.find("AmfCond") != sub_condition_api.end()) {
      sub_condition.type = AMF_COND;
      sub_condition.amf_info.amf_set_id =
440
          sub_condition_api["AmfCond"]["amfSetId"].get<std::string>();
441
      sub_condition.amf_info.amf_region_id =
442
          sub_condition_api["AmfCond"]["amfRegionId"].get<std::string>();
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
      Logger::nrf_app().debug(
          "Subscription condition type: AmfCond, amfSetId: %s, amfRegionId: "
          "%s ",
          sub_condition.amf_info.amf_set_id.c_str(),
          sub_condition.amf_info.amf_region_id.c_str());
    }

    if (sub_condition_api.find("GuamiListCond") != sub_condition_api.end()) {
      sub_condition.type = GUAMI_LIST_COND;
      // TODO
    }

    Logger::nrf_app().debug(
        "Subscription condition type: %s",
        subscription_condition_type_e2str[sub_condition.type].c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
458

459
    if (sub_condition.type != UNKNOWN_CONDITION) {
460
      sub.get()->set_sub_condition(sub_condition);
461
    }
462
  }
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  // NotificationEventType
  if (api_sub.reqNotifEventsIsSet()) {
    for (auto n : api_sub.getReqNotifEvents()) {
      if (n.compare("NF_REGISTERED") == 0) {
        sub.get()->add_notif_event(NOTIFICATION_TYPE_NF_REGISTERED);
        Logger::nrf_app().debug("ReqNotifEvents: %s", n.c_str());
      } else if (n.compare("NF_DEREGISTERED") == 0) {
        sub.get()->add_notif_event(NOTIFICATION_TYPE_NF_DEREGISTERED);
        Logger::nrf_app().debug("ReqNotifEvents: %s", n.c_str());
      } else if (n.compare("NF_PROFILE_CHANGED") == 0) {
        sub.get()->add_notif_event(NOTIFICATION_TYPE_NF_PROFILE_CHANGED);
      } else {
        sub.get()->add_notif_event(NOTIFICATION_TYPE_UNKNOWN_EVENT);
      }
    }
  }

481
  if (api_sub.validityTimeIsSet()) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
482
483
484
485
    std::string str = api_sub.getValidityTime();
    boost::posix_time::ptime p(boost::posix_time::from_iso_string(str));
    sub.get()->set_validity_time(p);
    Logger::nrf_app().debug("Validity Time: %s", str.c_str());
486
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
487
  // TODO:
488
  return true;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
489
490
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
491
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
492
nf_type_t api_conv::string_to_nf_type(const std::string& str) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  if (str.compare("NRF") == 0) return NF_TYPE_NRF;
  if (str.compare("AMF") == 0) return NF_TYPE_AMF;
  if (str.compare("SMF") == 0) return NF_TYPE_SMF;
  if (str.compare("AUSF") == 0) return NF_TYPE_AUSF;
  if (str.compare("NEF") == 0) return NF_TYPE_NEF;
  if (str.compare("PCP") == 0) return NF_TYPE_PCF;
  if (str.compare("SMSF") == 0) return NF_TYPE_SMSF;
  if (str.compare("NSSF") == 0) return NF_TYPE_NSSF;
  if (str.compare("UDR") == 0) return NF_TYPE_UDR;
  if (str.compare("LMF") == 0) return NF_TYPE_LMF;
  if (str.compare("GMLC") == 0) return NF_TYPE_GMLC;
  if (str.compare("5G_EIR") == 0) return NF_TYPE_5G_EIR;
  if (str.compare("SEPP") == 0) return NF_TYPE_SEPP;
  if (str.compare("UPF") == 0) return NF_TYPE_UPF;
  if (str.compare("N3IWF") == 0) return NF_TYPE_N3IWF;
  if (str.compare("AF") == 0) return NF_TYPE_AF;
  if (str.compare("UDSF") == 0) return NF_TYPE_UDSF;
  if (str.compare("BSF") == 0) return NF_TYPE_BSF;
  if (str.compare("CHF") == 0) return NF_TYPE_CHF;
  if (str.compare("NWDAF") == 0) return NF_TYPE_NWDAF;
  // default
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
514
515
  return NF_TYPE_UNKNOWN;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
516

517
518
519
520
521
522
523
524
525
526
//------------------------------------------------------------------------------
nf_up_interface_type_t api_conv::string_to_up_interface_type(
    const std::string& str) {
  if (str.compare("N3") == 0) return N3;
  if (str.compare("N6") == 0) return N6;
  if (str.compare("N9") == 0) return N9;
  if (str.compare("DATA_FORWARDING") == 0) return DATA_FORWARDING;
  // default
  return TYPE_UNKNOWN;
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
527
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
528
patch_op_type_t api_conv::string_to_patch_operation(const std::string& str) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
529
530
531
532
533
534
535
  if (str.compare("add") == 0) return PATCH_OP_ADD;
  if (str.compare("copy") == 0) return PATCH_OP_COPY;
  if (str.compare("move") == 0) return PATCH_OP_MOVE;
  if (str.compare("remove") == 0) return PATCH_OP_REMOVE;
  if (str.compare("replace") == 0) return PATCH_OP_REPLACE;
  if (str.compare("test") == 0) return PATCH_OP_TEST;
  // default
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
536
537
  return PATCH_OP_UNKNOWN;
}
538

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
539
bool api_conv::validate_uuid(const std::string& str) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
540
  // should be verified with Capital letter
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
541
  static const std::regex e(
Raphael Defosseux's avatar
Raphael Defosseux committed
542
543
      "[a-f0-9]{8}-[a-f0-9]{4}-[0-5][a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{"
      "12}");
Tien-Thinh Nguyen's avatar
Add smf    
Tien-Thinh Nguyen committed
544
  return regex_match(str, e);
545
}