udr_app.cpp 83.6 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
/*
 * 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 udr_app.cpp
 \brief
 \author  Tien-Thinh NGUYEN
 \company Eurecom
 \date 2020
 \email: Tien-Thinh.Nguyen@eurecom.fr
 */

#include "udr_app.hpp"
31
#include <mysql/mysql.h>
32
33
#include <unistd.h>

34
35
36
37
#include "AccessAndMobilitySubscriptionData.h"
#include "AuthenticationSubscription.h"
#include "ProblemDetails.h"
#include "SequenceNumber.h"
38
39
40
41
#include "logger.hpp"
#include "udr_config.hpp"

using namespace oai::udr::app;
42
using namespace oai::udr::model;
43
44
using namespace config;

45
extern udr_app *udr_app_inst;
46
47
48
extern udr_config udr_cfg;

//------------------------------------------------------------------------------
49
udr_app::udr_app(const std::string &config_file) {
50
51
  Logger::udr_app().startup("Starting...");

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
52
53
54
55
56
  if (!mysql_init(&mysql)) {
    Logger::udr_app().error("Cannot initialize MySQL");
    throw std::runtime_error("Cannot initialize MySQL");
  }

57
58
59
60
  if (!mysql_real_connect(&mysql, udr_cfg.mysql.mysql_server.c_str(),
                          udr_cfg.mysql.mysql_user.c_str(),
                          udr_cfg.mysql.mysql_pass.c_str(),
                          udr_cfg.mysql.mysql_db.c_str(), 0, 0, 0)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
61
62
63
64
    Logger::udr_app().error(
        "An error occurred while connecting to MySQL DB: %s",
        mysql_error(&mysql));
    throw std::runtime_error("Cannot connect to MySQL DB");
65
66
  }

67
68
69
70
71
  // TODO: Register to NRF
  Logger::udr_app().startup("Started");
}

//------------------------------------------------------------------------------
72
udr_app::~udr_app() { Logger::udr_app().debug("Delete UDM APP instance..."); }
73

74
//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
75
76
77
78
void udr_app::handle_query_am_data(const std::string &ue_id,
                                   const std::string &serving_plmn_id,
                                   nlohmann::json &response_data,
                                   Pistache::Http::Code &code) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
79
80
  MYSQL_RES *res = nullptr;
  MYSQL_ROW row = {};
81
  MYSQL_FIELD *field = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
82
83
  nlohmann::json j = {};
  response_data = {};
84

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
85
  oai::udr::model::AccessAndMobilitySubscriptionData subscription_data = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
86
87

  // TODO: Define query template in a header file
88
89
90
91
92
93
  const std::string query =
      "select * from AccessAndMobilitySubscriptionData WHERE ueid='" + ue_id +
      "' and servingPlmnid='" + serving_plmn_id + "'";

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
94
    code = Pistache::Http::Code::Internal_Server_Error;
95
96
97
98
99
100
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
101
    code = Pistache::Http::Code::Internal_Server_Error;
102
103
104
105
106
107
108
    return;
  }

  row = mysql_fetch_row(res);

  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
109
110
      try {
        if (!strcmp("supportedFeatures", field->name) && row[i] != NULL) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
111
          subscription_data.setSupportedFeatures(row[i]);
112
113
114
        } else if (!strcmp("gpsis", field->name) && row[i] != NULL) {
          std::vector<std ::string> gpsis;
          nlohmann::json::parse(row[i]).get_to(gpsis);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
115
          subscription_data.setGpsis(gpsis);
116
117
118
        } else if (!strcmp("internalGroupIds", field->name) && row[i] != NULL) {
          std::vector<std ::string> internalgroupids;
          nlohmann::json::parse(row[i]).get_to(internalgroupids);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
119
          subscription_data.setInternalGroupIds(internalgroupids);
120
121
122
123
        } else if (!strcmp("sharedVnGroupDataIds", field->name) &&
                   row[i] != NULL) {
          std::map<std ::string, std::string> sharedvngroupdataids;
          nlohmann::json::parse(row[i]).get_to(sharedvngroupdataids);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
124
          subscription_data.setSharedVnGroupDataIds(sharedvngroupdataids);
125
126
127
        } else if (!strcmp("subscribedUeAmbr", field->name) && row[i] != NULL) {
          AmbrRm subscribedueambr;
          nlohmann::json::parse(row[i]).get_to(subscribedueambr);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
128
          subscription_data.setSubscribedUeAmbr(subscribedueambr);
129
130
131
        } else if (!strcmp("nssai", field->name) && row[i] != NULL) {
          Nssai nssai;
          nlohmann::json::parse(row[i]).get_to(nssai);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
132
          subscription_data.setNssai(nssai);
133
134
135
        } else if (!strcmp("ratRestrictions", field->name) && row[i] != NULL) {
          std ::vector<RatType> ratrestrictions;
          nlohmann::json::parse(row[i]).get_to(ratrestrictions);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
136
          subscription_data.setRatRestrictions(ratrestrictions);
137
138
139
        } else if (!strcmp("forbiddenAreas", field->name) && row[i] != NULL) {
          std ::vector<Area> forbiddenareas;
          nlohmann::json::parse(row[i]).get_to(forbiddenareas);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
140
          subscription_data.setForbiddenAreas(forbiddenareas);
141
142
143
144
        } else if (!strcmp("serviceAreaRestriction", field->name) &&
                   row[i] != NULL) {
          ServiceAreaRestriction servicearearestriction;
          nlohmann::json::parse(row[i]).get_to(servicearearestriction);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
145
          subscription_data.setServiceAreaRestriction(servicearearestriction);
146
147
148
149
        } else if (!strcmp("coreNetworkTypeRestrictions", field->name) &&
                   row[i] != NULL) {
          std ::vector<CoreNetworkType> corenetworktyperestrictions;
          nlohmann::json::parse(row[i]).get_to(corenetworktyperestrictions);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
150
          subscription_data.setCoreNetworkTypeRestrictions(
151
152
153
              corenetworktyperestrictions);
        } else if (!strcmp("rfspIndex", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
154
          subscription_data.setRfspIndex(a);
155
156
        } else if (!strcmp("subsRegTimer", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
157
          subscription_data.setSubsRegTimer(a);
158
159
        } else if (!strcmp("ueUsageType", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
160
          subscription_data.setUeUsageType(a);
161
162
        } else if (!strcmp("mpsPriority", field->name) && row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
163
            subscription_data.setMpsPriority(true);
164
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
165
            subscription_data.setMpsPriority(false);
166
167
        } else if (!strcmp("mcsPriority", field->name) && row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
168
            subscription_data.setMcsPriority(true);
169
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
170
            subscription_data.setMcsPriority(false);
171
172
        } else if (!strcmp("activeTime", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
173
          subscription_data.setActiveTime(a);
174
175
176
        } else if (!strcmp("sorInfo", field->name) && row[i] != NULL) {
          SorInfo sorinfo;
          nlohmann::json::parse(row[i]).get_to(sorinfo);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
177
          subscription_data.setSorInfo(sorinfo);
178
179
        } else if (!strcmp("sorInfoExpectInd", field->name) && row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
180
            subscription_data.setSorInfoExpectInd(true);
181
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
182
            subscription_data.setSorInfoExpectInd(false);
183
184
        } else if (!strcmp("sorafRetrieval", field->name) && row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
185
            subscription_data.setSorafRetrieval(true);
186
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
187
            subscription_data.setSorafRetrieval(false);
188
189
190
191
        } else if (!strcmp("sorUpdateIndicatorList", field->name) &&
                   row[i] != NULL) {
          std ::vector<SorUpdateIndicator> sorupdateindicatorlist;
          nlohmann::json::parse(row[i]).get_to(sorupdateindicatorlist);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
192
          subscription_data.setSorUpdateIndicatorList(sorupdateindicatorlist);
193
194
195
        } else if (!strcmp("upuInfo", field->name) && row[i] != NULL) {
          UpuInfo upuinfo;
          nlohmann::json::parse(row[i]).get_to(upuinfo);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
196
          subscription_data.setUpuInfo(upuinfo);
197
198
        } else if (!strcmp("micoAllowed", field->name) && row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
199
            subscription_data.setMicoAllowed(true);
200
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
201
            subscription_data.setMicoAllowed(false);
202
203
204
        } else if (!strcmp("sharedAmDataIds", field->name) && row[i] != NULL) {
          std ::vector<std ::string> sharedamdataids;
          nlohmann::json::parse(row[i]).get_to(sharedamdataids);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
205
          subscription_data.setSharedAmDataIds(sharedamdataids);
206
207
208
209
        } else if (!strcmp("odbPacketServices", field->name) &&
                   row[i] != NULL) {
          OdbPacketServices odbpacketservices;
          nlohmann::json::parse(row[i]).get_to(odbpacketservices);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
210
          subscription_data.setOdbPacketServices(odbpacketservices);
211
212
        } else if (!strcmp("serviceGapTime", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
213
          subscription_data.setServiceGapTime(a);
214
215
216
        } else if (!strcmp("mdtUserConsent", field->name) && row[i] != NULL) {
          MdtUserConsent mdtuserconsent;
          nlohmann::json::parse(row[i]).get_to(mdtuserconsent);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
217
          subscription_data.setMdtUserConsent(mdtuserconsent);
218
219
220
        } else if (!strcmp("mdtConfiguration", field->name) && row[i] != NULL) {
          MdtConfiguration mdtconfiguration;
          nlohmann::json::parse(row[i]).get_to(mdtconfiguration);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
221
          subscription_data.setMdtConfiguration(mdtconfiguration);
222
223
224
        } else if (!strcmp("traceData", field->name) && row[i] != NULL) {
          TraceData tracedata;
          nlohmann::json::parse(row[i]).get_to(tracedata);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
225
          subscription_data.setTraceData(tracedata);
226
227
228
        } else if (!strcmp("cagData", field->name) && row[i] != NULL) {
          CagData cagdata;
          nlohmann::json::parse(row[i]).get_to(cagdata);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
229
          subscription_data.setCagData(cagdata);
230
        } else if (!strcmp("stnSr", field->name) && row[i] != NULL) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
231
          subscription_data.setStnSr(row[i]);
232
        } else if (!strcmp("cMsisdn", field->name) && row[i] != NULL) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
233
          subscription_data.setCMsisdn(row[i]);
234
235
        } else if (!strcmp("nbIoTUePriority", field->name) && row[i] != NULL) {
          int32_t a = std::stoi(row[i]);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
236
          subscription_data.setNbIoTUePriority(a);
237
238
239
        } else if (!strcmp("nssaiInclusionAllowed", field->name) &&
                   row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
240
            subscription_data.setNssaiInclusionAllowed(true);
241
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
242
            subscription_data.setNssaiInclusionAllowed(false);
243
244
        } else if (!strcmp("rgWirelineCharacteristics", field->name) &&
                   row[i] != NULL) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
245
          subscription_data.setRgWirelineCharacteristics(row[i]);
246
247
248
249
        } else if (!strcmp("ecRestrictionDataWb", field->name) &&
                   row[i] != NULL) {
          EcRestrictionDataWb ecrestrictiondatawb;
          nlohmann::json::parse(row[i]).get_to(ecrestrictiondatawb);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
250
          subscription_data.setEcRestrictionDataWb(ecrestrictiondatawb);
251
252
253
        } else if (!strcmp("ecRestrictionDataNb", field->name) &&
                   row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
254
            subscription_data.setEcRestrictionDataNb(true);
255
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
256
            subscription_data.setEcRestrictionDataNb(false);
257
258
259
260
        } else if (!strcmp("expectedUeBehaviourList", field->name) &&
                   row[i] != NULL) {
          ExpectedUeBehaviourData expecteduebehaviourlist;
          nlohmann::json::parse(row[i]).get_to(expecteduebehaviourlist);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
261
          subscription_data.setExpectedUeBehaviourList(expecteduebehaviourlist);
262
263
264
265
        } else if (!strcmp("primaryRatRestrictions", field->name) &&
                   row[i] != NULL) {
          std ::vector<RatType> primaryratrestrictions;
          nlohmann::json::parse(row[i]).get_to(primaryratrestrictions);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
266
          subscription_data.setPrimaryRatRestrictions(primaryratrestrictions);
267
268
269
270
        } else if (!strcmp("secondaryRatRestrictions", field->name) &&
                   row[i] != NULL) {
          std ::vector<RatType> secondaryratrestrictions;
          nlohmann::json::parse(row[i]).get_to(secondaryratrestrictions);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
271
          subscription_data.setSecondaryRatRestrictions(
272
273
274
275
276
              secondaryratrestrictions);
        } else if (!strcmp("edrxParametersList", field->name) &&
                   row[i] != NULL) {
          std ::vector<EdrxParameters> edrxparameterslist;
          nlohmann::json::parse(row[i]).get_to(edrxparameterslist);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
277
          subscription_data.setEdrxParametersList(edrxparameterslist);
278
279
280
281
        } else if (!strcmp("ptwParametersList", field->name) &&
                   row[i] != NULL) {
          std ::vector<PtwParameters> ptwparameterslist;
          nlohmann::json::parse(row[i]).get_to(ptwparameterslist);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
282
          subscription_data.setPtwParametersList(ptwparameterslist);
283
284
285
        } else if (!strcmp("iabOperationAllowed", field->name) &&
                   row[i] != NULL) {
          if (strcmp(row[i], "0"))
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
286
            subscription_data.setIabOperationAllowed(true);
287
          else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
288
            subscription_data.setIabOperationAllowed(false);
289
290
291
292
        } else if (!strcmp("wirelineForbiddenAreas", field->name) &&
                   row[i] != NULL) {
          std ::vector<WirelineArea> wirelineforbiddenareas;
          nlohmann::json::parse(row[i]).get_to(wirelineforbiddenareas);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
293
          subscription_data.setWirelineForbiddenAreas(wirelineforbiddenareas);
294
295
296
297
        } else if (!strcmp("wirelineServiceAreaRestriction", field->name) &&
                   row[i] != NULL) {
          WirelineServiceAreaRestriction wirelineservicearearestriction;
          nlohmann::json::parse(row[i]).get_to(wirelineservicearearestriction);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
298
          subscription_data.setWirelineServiceAreaRestriction(
299
300
301
302
303
              wirelineservicearearestriction);
        }
      } catch (std::exception e) {
        Logger::udr_server().error(
            " Cannot set values for Subscription Data: %s", e.what());
304
305
306
      }
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
307
    to_json(j, subscription_data);
308
309
310
311
    response_data = j;
    code = Pistache::Http::Code::Ok;

    Logger::udr_server().debug(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
312
313
        "AccessAndMobilitySubscriptionData GET - json:\n\"%s\"",
        j.dump().c_str());
314
315
  } else {
    Logger::udr_server().error("AccessAndMobilitySubscriptionData no data!");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
316
    code = Pistache::Http::Code::Internal_Server_Error;
317
318
319
320
321
322
  }

  mysql_free_result(res);
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
323
void udr_app::handle_create_amf_context_3gpp(
324
325
326
327
    const std::string &ue_id,
    Amf3GppAccessRegistration &amf3GppAccessRegistration,
    nlohmann::json &response_data, Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
328
  MYSQL_ROW row = {};
329
330
331

  const std::string select_AMF3GPPAccessRegistration =
      "select * from Amf3GppAccessRegistration WHERE ueid='" + ue_id + "'";
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
332
  std::string query = {};
333

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
334
  nlohmann::json j = {};
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

  if (mysql_real_query(
          &mysql, select_AMF3GPPAccessRegistration.c_str(),
          (unsigned long)select_AMF3GPPAccessRegistration.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               select_AMF3GPPAccessRegistration.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               select_AMF3GPPAccessRegistration.c_str());
    return;
  }
  if (mysql_num_rows(res)) {
    query =
        "update Amf3GppAccessRegistration set amfInstanceId='" +
        amf3GppAccessRegistration.getAmfInstanceId() + "'" +
        (amf3GppAccessRegistration.supportedFeaturesIsSet()
             ? ",supportedFeatures='" +
                   amf3GppAccessRegistration.getSupportedFeatures() + "'"
             : "") +
        (amf3GppAccessRegistration.purgeFlagIsSet()
             ? (amf3GppAccessRegistration.isPurgeFlag() ? ",purgeFlag=1"
                                                        : ",purgeFlag=0")
             : "") +
        (amf3GppAccessRegistration.peiIsSet()
             ? ",pei='" + amf3GppAccessRegistration.getPei() + "'"
             : "") +
        ",deregCallbackUri='" +
        amf3GppAccessRegistration.getDeregCallbackUri() + "'" +
        (amf3GppAccessRegistration.pcscfRestorationCallbackUriIsSet()
             ? ",pcscfRestorationCallbackUri='" +
                   amf3GppAccessRegistration.getPcscfRestorationCallbackUri() +
                   "'"
             : "") +
        (amf3GppAccessRegistration.initialRegistrationIndIsSet()
             ? (amf3GppAccessRegistration.isInitialRegistrationInd()
                    ? ",initialRegistrationInd=1"
                    : ",initialRegistrationInd=0")
             : "") +
        (amf3GppAccessRegistration.drFlagIsSet()
             ? (amf3GppAccessRegistration.isDrFlag() ? ",drFlag=1"
                                                     : ",drFlag=0")
             : "") +
        (amf3GppAccessRegistration.urrpIndicatorIsSet()
             ? (amf3GppAccessRegistration.isUrrpIndicator()
                    ? ",urrpIndicator=1"
                    : ",urrpIndicator=0")
             : "") +
        (amf3GppAccessRegistration.amfEeSubscriptionIdIsSet()
             ? ",amfEeSubscriptionId='" +
                   amf3GppAccessRegistration.getAmfEeSubscriptionId() + "'"
             : "") +
        (amf3GppAccessRegistration.ueSrvccCapabilityIsSet()
             ? (amf3GppAccessRegistration.isUeSrvccCapability()
                    ? ",ueSrvccCapability=1"
                    : ",ueSrvccCapability=0")
             : "") +
        (amf3GppAccessRegistration.registrationTimeIsSet()
             ? ",registrationTime='" +
                   amf3GppAccessRegistration.getRegistrationTime() + "'"
             : "") +
        (amf3GppAccessRegistration.noEeSubscriptionIndIsSet()
             ? (amf3GppAccessRegistration.isNoEeSubscriptionInd()
                    ? ",noEeSubscriptionInd=1"
                    : ",noEeSubscriptionInd=0")
             : "");

    if (amf3GppAccessRegistration.imsVoPsIsSet()) {
      to_json(j, amf3GppAccessRegistration.getImsVoPs());
      query += ",imsVoPs='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.amfServiceNameDeregIsSet()) {
      to_json(j, amf3GppAccessRegistration.getAmfServiceNameDereg());
      query += ",amfServiceNameDereg='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.amfServiceNamePcscfRestIsSet()) {
      to_json(j, amf3GppAccessRegistration.getAmfServiceNamePcscfRest());
      query += ",amfServiceNamePcscfRest='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.backupAmfInfoIsSet()) {
      nlohmann::json tmp;
      j.clear();
      std::vector<BackupAmfInfo> backupamfinfo =
          amf3GppAccessRegistration.getBackupAmfInfo();
      for (int i = 0; i < backupamfinfo.size(); i++) {
        to_json(tmp, backupamfinfo[i]);
        j += tmp;
      }
      query += ",backupAmfInfo='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.epsInterworkingInfoIsSet()) {
      to_json(j, amf3GppAccessRegistration.getEpsInterworkingInfo());
      query += ",epsInterworkingInfo='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.vgmlcAddressIsSet()) {
      to_json(j, amf3GppAccessRegistration.getVgmlcAddress());
      query += ",vgmlcAddress='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.contextInfoIsSet()) {
      to_json(j, amf3GppAccessRegistration.getContextInfo());
      query += ",contextInfo='" + j.dump() + "'";
    }

    to_json(j, amf3GppAccessRegistration.getGuami());
    query += ",guami='" + j.dump() + "'";
    to_json(j, amf3GppAccessRegistration.getRatType());
    query += ",ratType='" + j.dump() + "'";
    query += " where ueid='" + ue_id + "'";
  } else {
    query =
        "insert into Amf3GppAccessRegistration set ueid='" + ue_id + "'" +
        ",amfInstanceId='" + amf3GppAccessRegistration.getAmfInstanceId() +
        "'" +
        (amf3GppAccessRegistration.supportedFeaturesIsSet()
             ? ",supportedFeatures='" +
                   amf3GppAccessRegistration.getSupportedFeatures() + "'"
             : "") +
        (amf3GppAccessRegistration.purgeFlagIsSet()
             ? (amf3GppAccessRegistration.isPurgeFlag() ? ",purgeFlag=1"
                                                        : ",purgeFlag=0")
             : "") +
        (amf3GppAccessRegistration.peiIsSet()
             ? ",pei='" + amf3GppAccessRegistration.getPei() + "'"
             : "") +
        ",deregCallbackUri='" +
        amf3GppAccessRegistration.getDeregCallbackUri() + "'" +
        (amf3GppAccessRegistration.pcscfRestorationCallbackUriIsSet()
             ? ",pcscfRestorationCallbackUri='" +
                   amf3GppAccessRegistration.getPcscfRestorationCallbackUri() +
                   "'"
             : "") +
        (amf3GppAccessRegistration.initialRegistrationIndIsSet()
             ? (amf3GppAccessRegistration.isInitialRegistrationInd()
                    ? ",initialRegistrationInd=1"
                    : ",initialRegistrationInd=0")
             : "") +
        (amf3GppAccessRegistration.drFlagIsSet()
             ? (amf3GppAccessRegistration.isDrFlag() ? ",drFlag=1"
                                                     : ",drFlag=0")
             : "") +
        (amf3GppAccessRegistration.urrpIndicatorIsSet()
             ? (amf3GppAccessRegistration.isUrrpIndicator()
                    ? ",urrpIndicator=1"
                    : ",urrpIndicator=0")
             : "") +
        (amf3GppAccessRegistration.amfEeSubscriptionIdIsSet()
             ? ",amfEeSubscriptionId='" +
                   amf3GppAccessRegistration.getAmfEeSubscriptionId() + "'"
             : "") +
        (amf3GppAccessRegistration.ueSrvccCapabilityIsSet()
             ? (amf3GppAccessRegistration.isUeSrvccCapability()
                    ? ",ueSrvccCapability=1"
                    : ",ueSrvccCapability=0")
             : "") +
        (amf3GppAccessRegistration.registrationTimeIsSet()
             ? ",registrationTime='" +
                   amf3GppAccessRegistration.getRegistrationTime() + "'"
             : "") +
        (amf3GppAccessRegistration.noEeSubscriptionIndIsSet()
             ? (amf3GppAccessRegistration.isNoEeSubscriptionInd()
                    ? ",noEeSubscriptionInd=1"
                    : ",noEeSubscriptionInd=0")
             : "");

    if (amf3GppAccessRegistration.imsVoPsIsSet()) {
      to_json(j, amf3GppAccessRegistration.getImsVoPs());
      query += ",imsVoPs='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.amfServiceNameDeregIsSet()) {
      to_json(j, amf3GppAccessRegistration.getAmfServiceNameDereg());
      query += ",amfServiceNameDereg='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.amfServiceNamePcscfRestIsSet()) {
      to_json(j, amf3GppAccessRegistration.getAmfServiceNamePcscfRest());
      query += ",amfServiceNamePcscfRest='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.backupAmfInfoIsSet()) {
      nlohmann::json tmp;
      j.clear();
      std::vector<BackupAmfInfo> backupamfinfo =
          amf3GppAccessRegistration.getBackupAmfInfo();
      for (int i = 0; i < backupamfinfo.size(); i++) {
        to_json(tmp, backupamfinfo[i]);
        j += tmp;
      }
      query += ",backupAmfInfo='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.epsInterworkingInfoIsSet()) {
      to_json(j, amf3GppAccessRegistration.getEpsInterworkingInfo());
      query += ",epsInterworkingInfo='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.vgmlcAddressIsSet()) {
      to_json(j, amf3GppAccessRegistration.getVgmlcAddress());
      query += ",vgmlcAddress='" + j.dump() + "'";
    }
    if (amf3GppAccessRegistration.contextInfoIsSet()) {
      to_json(j, amf3GppAccessRegistration.getContextInfo());
      query += ",contextInfo='" + j.dump() + "'";
    }

    to_json(j, amf3GppAccessRegistration.getGuami());
    query += ",guami='" + j.dump() + "'";
    to_json(j, amf3GppAccessRegistration.getRatType());
    query += ",ratType='" + j.dump() + "'";
  }

  mysql_free_result(res);
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  to_json(j, amf3GppAccessRegistration);
  response_data = j;
  code = Pistache::Http::Code::Created;

  Logger::udr_server().debug("Amf3GppAccessRegistration PUT - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
556
                             j.dump().c_str());
557
558
559
}

//------------------------------------------------------------------------------
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
560
561
562
void udr_app::handle_query_amf_context_3gpp(const std::string &ue_id,
                                            nlohmann::json &response_data,
                                            Pistache::Http::Code &code) {
563
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
564
  MYSQL_ROW row = {};
565
566
  MYSQL_FIELD *field = nullptr;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
567
  nlohmann::json j = {};
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

  Amf3GppAccessRegistration amf3gppaccessregistration;
  const std::string query =
      "select * from Amf3GppAccessRegistration WHERE ueid='" + ue_id + "'";

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  row = mysql_fetch_row(res);

  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("amfInstanceId", field->name)) {
        amf3gppaccessregistration.setAmfInstanceId(row[i]);
      } else if (!strcmp("supportedFeatures", field->name) && row[i] != NULL) {
        amf3gppaccessregistration.setSupportedFeatures(row[i]);
      } else if (!strcmp("purgeFlag", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setPurgeFlag(true);
        else
          amf3gppaccessregistration.setPurgeFlag(false);
      } else if (!strcmp("pei", field->name) && row[i] != NULL) {
        amf3gppaccessregistration.setPei(row[i]);
      } else if (!strcmp("imsVoPs", field->name) && row[i] != NULL) {
        ImsVoPs imsvops;
        nlohmann::json::parse(row[i]).get_to(imsvops);
        amf3gppaccessregistration.setImsVoPs(imsvops);
      } else if (!strcmp("deregCallbackUri", field->name)) {
        amf3gppaccessregistration.setDeregCallbackUri(row[i]);
      } else if (!strcmp("amfServiceNameDereg", field->name) &&
                 row[i] != NULL) {
        ServiceName amfservicenamedereg;
        nlohmann::json::parse(row[i]).get_to(amfservicenamedereg);
        amf3gppaccessregistration.setAmfServiceNameDereg(amfservicenamedereg);
      } else if (!strcmp("pcscfRestorationCallbackUri", field->name) &&
                 row[i] != NULL) {
        amf3gppaccessregistration.setPcscfRestorationCallbackUri(row[i]);
      } else if (!strcmp("amfServiceNamePcscfRest", field->name) &&
                 row[i] != NULL) {
        ServiceName amfservicenamepcscfrest;
        nlohmann::json::parse(row[i]).get_to(amfservicenamepcscfrest);
        amf3gppaccessregistration.setAmfServiceNamePcscfRest(
            amfservicenamepcscfrest);
      } else if (!strcmp("initialRegistrationInd", field->name) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setInitialRegistrationInd(true);
        else
          amf3gppaccessregistration.setInitialRegistrationInd(false);
      } else if (!strcmp("guami", field->name)) {
        Guami guami;
        nlohmann::json::parse(row[i]).get_to(guami);
        amf3gppaccessregistration.setGuami(guami);
      } else if (!strcmp("backupAmfInfo", field->name) && row[i] != NULL) {
        std ::vector<BackupAmfInfo> backupamfinfo;
        nlohmann::json::parse(row[i]).get_to(backupamfinfo);
        amf3gppaccessregistration.setBackupAmfInfo(backupamfinfo);
      } else if (!strcmp("drFlag", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setDrFlag(true);
        else
          amf3gppaccessregistration.setDrFlag(false);
      } else if (!strcmp("ratType", field->name)) {
        RatType rattype;
        nlohmann::json::parse(row[i]).get_to(rattype);
        amf3gppaccessregistration.setRatType(rattype);
      } else if (!strcmp("urrpIndicator", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setUrrpIndicator(true);
        else
          amf3gppaccessregistration.setUrrpIndicator(false);
      } else if (!strcmp("amfEeSubscriptionId", field->name) &&
                 row[i] != NULL) {
        amf3gppaccessregistration.setAmfEeSubscriptionId(row[i]);
      } else if (!strcmp("epsInterworkingInfo", field->name) &&
                 row[i] != NULL) {
        EpsInterworkingInfo epsinterworkinginfo;
        nlohmann::json::parse(row[i]).get_to(epsinterworkinginfo);
        amf3gppaccessregistration.setEpsInterworkingInfo(epsinterworkinginfo);
      } else if (!strcmp("ueSrvccCapability", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setUeSrvccCapability(true);
        else
          amf3gppaccessregistration.setUeSrvccCapability(false);
      } else if (!strcmp("registrationTime", field->name) && row[i] != NULL) {
        amf3gppaccessregistration.setRegistrationTime(row[i]);
      } else if (!strcmp("vgmlcAddress", field->name) && row[i] != NULL) {
        VgmlcAddress vgmlcaddress;
        nlohmann::json::parse(row[i]).get_to(vgmlcaddress);
        amf3gppaccessregistration.setVgmlcAddress(vgmlcaddress);
      } else if (!strcmp("contextInfo", field->name) && row[i] != NULL) {
        ContextInfo contextinfo;
        nlohmann::json::parse(row[i]).get_to(contextinfo);
        amf3gppaccessregistration.setContextInfo(contextinfo);
      } else if (!strcmp("noEeSubscriptionInd", field->name) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          amf3gppaccessregistration.setNoEeSubscriptionInd(true);
        else
          amf3gppaccessregistration.setNoEeSubscriptionInd(false);
      }
    }
    to_json(j, amf3gppaccessregistration);
    response_data = j;
    code = Pistache::Http::Code::Ok;

    Logger::udr_server().debug("Amf3GppAccessRegistration GET - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
685
                               j.dump().c_str());
686
687
688
689
690
691
692
693
694
695
696
697
698
  } else {
    Logger::udr_server().error("Amf3GppAccessRegistration no data!SQL(%s)",
                               query.c_str());
  }

  mysql_free_result(res);
}

//------------------------------------------------------------------------------
void udr_app::handle_create_authentication_status(const std::string &ue_id,
                                                  const AuthEvent &authEvent,
                                                  nlohmann::json &response_data,
                                                  Pistache::Http::Code &code) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
699
700
701
  Logger::udr_server().info("Handle Create Authentication Status");

  MYSQL_RES *res = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
702
  MYSQL_ROW row = {};
703
704
705

  const std::string select_AuthenticationStatus =
      "select * from AuthenticationStatus WHERE ueid='" + ue_id + "'";
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
706
707
  std::string query = {};
  nlohmann::json j = {};
708

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
709
710
711
  Logger::udr_server().info("MySQL query: %s",
                            select_AuthenticationStatus.c_str());

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
  if (mysql_real_query(&mysql, select_AuthenticationStatus.c_str(),
                       (unsigned long)select_AuthenticationStatus.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               select_AuthenticationStatus.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               select_AuthenticationStatus.c_str());
    return;
  }
  if (mysql_num_rows(res)) {
    query = "update AuthenticationStatus set nfInstanceId='" +
            authEvent.getNfInstanceId() + "'" +
            ",success=" + (authEvent.isSuccess() ? "1" : "0") + ",timeStamp='" +
            authEvent.getTimeStamp() + "'" + ",authType='" +
            authEvent.getAuthType() + "'" + ",servingNetworkName='" +
            authEvent.getServingNetworkName() + "'" +
            (authEvent.authRemovalIndIsSet()
                 ? (authEvent.isAuthRemovalInd() ? ",authRemovalInd=1"
                                                 : ",authRemovalInd=0")
                 : "");
    //        to_json(j,authEvent.getAuthType());
    //        query += ",authType='"+j.dump()+"'";
    query += " where ueid='" + ue_id + "'";
  } else {
    query = "insert into AuthenticationStatus set ueid='" + ue_id + "'" +
            ",nfInstanceId='" + authEvent.getNfInstanceId() + "'" +
            ",success=" + (authEvent.isSuccess() ? "1" : "0") + ",timeStamp='" +
            authEvent.getTimeStamp() + "'" + ",authType='" +
            authEvent.getAuthType() + "'" + ",servingNetworkName='" +
            authEvent.getServingNetworkName() + "'" +
            (authEvent.authRemovalIndIsSet()
                 ? (authEvent.isAuthRemovalInd() ? ",authRemovalInd=1"
                                                 : ",authRemovalInd=0")
                 : "");
    //        to_json(j,authEvent.getAuthType());
    //        query += ",authType='"+j.dump()+"'";
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
754
755
  Logger::udr_server().info("MySQL query: %s", query.c_str());

756
757
758
759
760
761
762
763
764
765
  mysql_free_result(res);
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql create failure!SQL(%s)", query.c_str());
    return;
  }

  response_data = {};
  code = Pistache::Http::Code::No_Content;

  to_json(j, authEvent);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
766
767
  Logger::udr_server().info("AuthenticationStatus PUT - json:\n\"%s\"",
                            j.dump().c_str());
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
}

//------------------------------------------------------------------------------
void udr_app::handle_delete_authentication_status(const std::string &ue_id,
                                                  nlohmann::json &response_data,
                                                  Pistache::Http::Code &code) {
  const std::string query =
      "DELETE from AuthenticationStatus WHERE ueid='" + ue_id + "'";

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  response_data = {};
  code = Pistache::Http::Code::No_Content;
  Logger::udr_server().debug("AuthenticationStatus DELETE - successful");
}

//------------------------------------------------------------------------------
void udr_app::handle_query_authentication_status(const std::string &ue_id,
                                                 nlohmann::json &response_data,
                                                 Pistache::Http::Code &code) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
792
793
  Logger::udr_server().info("Handle Query Authentication Status");
  MYSQL_RES *res = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
794
  MYSQL_ROW row = {};
795
  MYSQL_FIELD *field = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
796
  nlohmann::json j = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
797
  AuthEvent authenticationstatus = {};
798
799
800
  const std::string query =
      "select * from AuthenticationStatus WHERE ueid='" + ue_id + "'";

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
801
  Logger::udr_server().info("MySQL query: %s", query.c_str());
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!");
    return;
  }

  row = mysql_fetch_row(res);
  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("nfInstanceId", field->name)) {
        authenticationstatus.setNfInstanceId(row[i]);
      } else if (!strcmp("success", field->name)) {
        if (strcmp(row[i], "0"))
          authenticationstatus.setSuccess(true);
        else
          authenticationstatus.setSuccess(false);
      } else if (!strcmp("timeStamp", field->name)) {
        authenticationstatus.setTimeStamp(row[i]);
      } else if (!strcmp("authType", field->name)) {
        //                AuthType authtype;
        //                nlohmann::json::parse(row[i]).get_to(authtype);
        authenticationstatus.setAuthType(row[i]);
      } else if (!strcmp("servingNetworkName", field->name)) {
        authenticationstatus.setServingNetworkName(row[i]);
      } else if (!strcmp("authRemovalInd", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          authenticationstatus.setAuthRemovalInd(true);
        else
          authenticationstatus.setAuthRemovalInd(false);
      }
    }

    to_json(j, authenticationstatus);
    response_data = j;
    code = Pistache::Http::Code::Ok;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
844
845
    Logger::udr_server().info("AuthenticationStatus GET - json:\n\"%s\"",
                              j.dump().c_str());
846
847
848
849
850
851
852
853
854
855
856
857
  } else {
    Logger::udr_server().error("AuthenticationStatus no data!SQL(%s)",
                               query.c_str());
  }

  mysql_free_result(res);
}

//------------------------------------------------------------------------------
void udr_app::handle_modify_authentication_subscription(
    const std::string &ue_id, const std::vector<PatchItem> &patchItem,
    nlohmann::json &response_data, Pistache::Http::Code &code) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
858
859
  Logger::udr_server().info("Handle Update Authentication Subscription");
  MYSQL_RES *res = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
860
  MYSQL_ROW row = {};
861
862
863

  const std::string select_Authenticationsubscription =
      "select * from AuthenticationSubscription WHERE ueid='" + ue_id + "'";
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
864
865
866
867

  Logger::udr_server().info("MySQL Query (%s)",
                            select_Authenticationsubscription.c_str());

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
868
869
870
  std::string query = {};
  nlohmann::json j = {};
  nlohmann::json tmp_j = {};
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906

  for (int i = 0; i < patchItem.size(); i++) {
    if ((!strcmp(patchItem[i].getOp().c_str(), PATCH_OPERATION_REPLACE)) &&
        patchItem[i].valueIsSet()) {
      patchItem[i].getValue();
      SequenceNumber sequencenumber;
      nlohmann::json::parse(patchItem[i].getValue().c_str())
          .get_to(sequencenumber);

      if (mysql_real_query(
              &mysql, select_Authenticationsubscription.c_str(),
              (unsigned long)select_Authenticationsubscription.size())) {
        Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                                   select_Authenticationsubscription.c_str());
        return;
      }

      res = mysql_store_result(&mysql);
      if (res == NULL) {
        Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                                   select_Authenticationsubscription.c_str());
        return;
      }
      if (mysql_num_rows(res)) {
        nlohmann::json sequencenumber_j;
        query = "update AuthenticationSubscription set sequenceNumber='";

        to_json(sequencenumber_j, sequencenumber);
        query += sequencenumber_j.dump() + "'";
        query += " where ueid='" + ue_id + "'";
      } else {
        Logger::udr_server().error(
            "AuthenticationSubscription no data!SQL(%s)",
            select_Authenticationsubscription.c_str());
      }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
907
      Logger::udr_server().info("MySQL Update cmd (%s)", query.c_str());
908
909
910
      mysql_free_result(res);

      if (mysql_real_query(&mysql, query.c_str(),
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
911
                           (unsigned long)query.size()) != 0) {
912
913
914
915
916
917
918
919
920
921
        Logger::udr_server().error("update mysql failure!SQL(%s)",
                                   query.c_str());
        return;
      }
    }

    to_json(tmp_j, patchItem[i]);
    j += tmp_j;
  }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
922
923
  Logger::udr_server().info("AuthenticationSubscription PATCH - json:\n\"%s\"",
                            j.dump().c_str());
924
925
926
927
928
929
930
931
932

  response_data = {};
  code = Pistache::Http::Code::No_Content;
}

//------------------------------------------------------------------------------
void udr_app::handle_read_authentication_subscription(
    const std::string &ue_id, nlohmann::json &response_data,
    Pistache::Http::Code &code) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
933
934
  Logger::udr_server().info("Handle Read Authentication Subscription");
  MYSQL_RES *res = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
935
  MYSQL_ROW row = {};
936
  MYSQL_FIELD *field = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
937
  nlohmann::json j = {};
938

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
939
  AuthenticationSubscription authenticationsubscription = {};
940
941
  const std::string query =
      "select * from AuthenticationSubscription WHERE ueid='" + ue_id + "'";
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
942
  Logger::udr_server().info("MySQL Query (%s)", query.c_str());
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    return;
  }

  row = mysql_fetch_row(res);

  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("authenticationMethod", field->name)) {
        //                AuthMethod authenticationmethod;
        //                nlohmann::json::parse(row[i]).get_to(authenticationmethod);
        authenticationsubscription.setAuthenticationMethod(row[i]);
      } else if (!strcmp("encPermanentKey", field->name) && row[i] != NULL) {
        authenticationsubscription.setEncPermanentKey(row[i]);
      } else if (!strcmp("protectionParameterId", field->name) &&
                 row[i] != NULL) {
        authenticationsubscription.setProtectionParameterId(row[i]);
      } else if (!strcmp("sequenceNumber", field->name) && row[i] != NULL) {
        SequenceNumber sequencenumber;
        nlohmann::json::parse(row[i]).get_to(sequencenumber);
        authenticationsubscription.setSequenceNumber(sequencenumber);
      } else if (!strcmp("authenticationManagementField", field->name) &&
                 row[i] != NULL) {
        authenticationsubscription.setAuthenticationManagementField(row[i]);
      } else if (!strcmp("algorithmId", field->name) && row[i] != NULL) {
        authenticationsubscription.setAlgorithmId(row[i]);
      } else if (!strcmp("encOpcKey", field->name) && row[i] != NULL) {
        authenticationsubscription.setEncOpcKey(row[i]);
      } else if (!strcmp("encTopcKey", field->name) && row[i] != NULL) {
        authenticationsubscription.setEncTopcKey(row[i]);
      } else if (!strcmp("vectorGenerationInHss", field->name) &&
                 row[i] != NULL) {
        std::cout << row[i] << std::endl;
        if (strcmp(row[i], "0"))
          authenticationsubscription.setVectorGenerationInHss(true);
        else
          authenticationsubscription.setVectorGenerationInHss(false);
      } else if (!strcmp("n5gcAuthMethod", field->name) && row[i] != NULL) {
        //                AuthMethod n5gcauthmethod;
        //                nlohmann::json::parse(row[i]).get_to(n5gcauthmethod);
        authenticationsubscription.setN5gcAuthMethod(row[i]);
      } else if (!strcmp("rgAuthenticationInd", field->name) &&
                 row[i] != NULL) {
        std::cout << row[i] << std::endl;
        if (strcmp(row[i], "0"))
          authenticationsubscription.setRgAuthenticationInd(true);
        else
          authenticationsubscription.setRgAuthenticationInd(false);
      } else if (!strcmp("supi", field->name) && row[i] != NULL) {
        authenticationsubscription.setSupi(row[i]);
      }
    }

    to_json(j, authenticationsubscription);
    response_data = j;
    code = Pistache::Http::Code::Ok;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1010
1011
    Logger::udr_server().info("AuthenticationSubscription GET - json:\n\"%s\"",
                              j.dump().c_str());
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  } else {
    Logger::udr_server().error("AuthenticationSubscription no data!SQL(%s)",
                               query.c_str());
  }

  mysql_free_result(res);
}

//------------------------------------------------------------------------------
void udr_app::handle_query_sdm_subscription(const std::string &ue_id,
                                            const std::string &subs_id,
                                            nlohmann::json &response_data,
                                            Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1026
  MYSQL_ROW row = {};
1027
  MYSQL_FIELD *field = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1028
  nlohmann::json j = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1029
  SdmSubscription SdmSubscriptions = {};
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
  const std::string query = "SELECT * from SdmSubscriptions WHERE ueid='" +
                            ue_id + "' AND subsId=" + subs_id;

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    return;
  }

  row = mysql_fetch_row(res);
  if (row != NULL) {
    for (int i = 0; field = mysql_fetch_field(res); i++) {
      if (!strcmp("nfInstanceId", field->name)) {
        SdmSubscriptions.setNfInstanceId(row[i]);
      } else if (!strcmp("implicitUnsubscribe", field->name) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          SdmSubscriptions.setImplicitUnsubscribe(true);
        else
          SdmSubscriptions.setImplicitUnsubscribe(false);
      } else if (!strcmp("expires", field->name) && row[i] != NULL) {
        SdmSubscriptions.setExpires(row[i]);
      } else if (!strcmp("callbackReference", field->name)) {
        SdmSubscriptions.setCallbackReference(row[i]);
      } else if (!strcmp("amfServiceName", field->name) && row[i] != NULL) {
        ServiceName amfservicename;
        nlohmann::json::parse(row[i]).get_to(amfservicename);
        SdmSubscriptions.setAmfServiceName(amfservicename);
      } else if (!strcmp("monitoredResourceUris", field->name)) {
        std::vector<std::string> monitoredresourceuris;
        nlohmann::json::parse(row[i]).get_to(monitoredresourceuris);
        SdmSubscriptions.setMonitoredResourceUris(monitoredresourceuris);
      } else if (!strcmp("singleNssai", field->name) && row[i] != NULL) {
        Snssai singlenssai;
        nlohmann::json::parse(row[i]).get_to(singlenssai);
        SdmSubscriptions.setSingleNssai(singlenssai);
      } else if (!strcmp("dnn", field->name) && row[i] != NULL) {
        SdmSubscriptions.setDnn(row[i]);
      } else if (!strcmp("subscriptionId", field->name) && row[i] != NULL) {
        SdmSubscriptions.setSubscriptionId(row[i]);
      } else if (!strcmp("plmnId", field->name) && row[i] != NULL) {
        PlmnId plmnid;
        nlohmann::json::parse(row[i]).get_to(plmnid);
        SdmSubscriptions.setPlmnId(plmnid);
      } else if (!strcmp("immediateReport", field->name) && row[i] != NULL) {
        if (strcmp(row[i], "0"))
          SdmSubscriptions.setImmediateReport(true);
        else
          SdmSubscriptions.setImmediateReport(false);
      } else if (!strcmp("report", field->name) && row[i] != NULL) {
        SubscriptionDataSets report;
        nlohmann::json::parse(row[i]).get_to(report);
        SdmSubscriptions.setReport(report);
      } else if (!strcmp("supportedFeatures", field->name) && row[i] != NULL) {
        SdmSubscriptions.setSupportedFeatures(row[i]);
      } else if (!strcmp("contextInfo", field->name) && row[i] != NULL) {
        ContextInfo contextinfo;
        nlohmann::json::parse(row[i]).get_to(contextinfo);
        SdmSubscriptions.setContextInfo(contextinfo);
      }
    }
    to_json(j, SdmSubscriptions);
    response_data = j;
    code = Pistache::Http::Code::Ok;

    Logger::udr_server().debug("SdmSubscription GET - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1103
                               j.dump().c_str());
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
  } else {
    Logger::udr_server().error("SdmSubscription no data!SQL(%s)",
                               query.c_str());
  }

  mysql_free_result(res);
}

//------------------------------------------------------------------------------
void udr_app::handle_remove_sdm_subscription(const std::string &ue_id,
                                             const std::string &subs_id,
                                             nlohmann::json &response_data,
                                             Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1118
1119
  nlohmann::json j = {};
  ProblemDetails problemdetails = {};
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

  const std::string select_query =
      "SELECT * from SdmSubscriptions WHERE ueid='" + ue_id +
      "' AND subsId=" + subs_id;

  const std::string query = "DELETE from SdmSubscriptions WHERE ueid='" +
                            ue_id + "' AND subsId=" + subs_id;

  if (mysql_real_query(&mysql, select_query.c_str(),
                       (unsigned long)select_query.size())) {
    problemdetails.setCause("USER_NOT_FOUND");
    to_json(j, problemdetails);
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    response_data = j;
    code = Pistache::Http::Code::Not_Found;
    return;
  }
  res = mysql_store_result(&mysql);
  if (res == NULL) {
    problemdetails.setCause("USER_NOT_FOUND");
    to_json(j, problemdetails);
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    response_data = j;
    code = Pistache::Http::Code::Not_Found;
    return;
  }
  if (!mysql_num_rows(res)) {
    problemdetails.setCause("DATA_NOT_FOUND");
    to_json(j, problemdetails);
    response_data = j;
    code = Pistache::Http::Code::Not_Found;
    return;
  }
  mysql_free_result(res);
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    problemdetails.setCause("USER_NOT_FOUND");
    to_json(j, problemdetails);
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    response_data = j;
    code = Pistache::Http::Code::Not_Found;
    return;
  }
1165

1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  response_data = {};
  code = Pistache::Http::Code::No_Content;

  Logger::udr_server().debug("SdmSubscription DELETE - successful");
}

//------------------------------------------------------------------------------
void udr_app::handle_update_sdm_subscription(const std::string &ue_id,
                                             const std::string &subs_id,
                                             SdmSubscription &sdmSubscription,
                                             nlohmann::json &response_data,
                                             Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1179
  MYSQL_ROW row = {};
1180
1181
1182
1183

  const std::string select_query =
      "SELECT * from SdmSubscriptions WHERE ueid='" + ue_id +
      "' AND subsId=" + subs_id;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1184
1185
  std::string query = {};
  nlohmann::json j = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1186
  ProblemDetails problemdetails = {};
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

  if (mysql_real_query(&mysql, select_query.c_str(),
                       (unsigned long)select_query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    return;
  }
  if (mysql_num_rows(res)) {
    nlohmann::json MonitoredResourceUris_json(
        sdmSubscription.getMonitoredResourceUris());

    query =
        "update SdmSubscriptions set nfInstanceId='" +
        sdmSubscription.getNfInstanceId() + "'" +
        (sdmSubscription.implicitUnsubscribeIsSet()
             ? (sdmSubscription.isImplicitUnsubscribe()
                    ? ",implicitUnsubscribe=1"
                    : ",implicitUnsubscribe=0")
             : "") +
        (sdmSubscription.expiresIsSet()
             ? ",expires='" + sdmSubscription.getExpires() + "'"
             : "") +
        ",callbackReference='" + sdmSubscription.getCallbackReference() + "'" +
        (sdmSubscription.dnnIsSet() ? ",dnn='" + sdmSubscription.getDnn() + "'"
                                    : "") +
        (sdmSubscription.subscriptionIdIsSet()
             ? ",subscriptionId='" + sdmSubscription.getSubscriptionId() + "'"
             : "") +
        (sdmSubscription.immediateReportIsSet()
             ? (sdmSubscription.isImmediateReport() ? ",immediateReport=1"
                                                    : ",immediateReport=0")
             : "") +
        (sdmSubscription.supportedFeaturesIsSet()
             ? ",supportedFeatures='" + sdmSubscription.getSupportedFeatures() +
                   "'"
             : "");

    if (sdmSubscription.amfServiceNameIsSet()) {
      to_json(j, sdmSubscription.getAmfServiceName());
      query += ",amfServiceName='" + j.dump() + "'";
    }
    if (sdmSubscription.singleNssaiIsSet()) {
      to_json(j, sdmSubscription.getSingleNssai());
      query += ",singleNssai='" + j.dump() + "'";
    }
    if (sdmSubscription.plmnIdIsSet()) {
      to_json(j, sdmSubscription.getPlmnId());
      query += ",plmnId='" + j.dump() + "'";
    }
    if (sdmSubscription.reportIsSet()) {
      to_json(j, sdmSubscription.getReport());
      query += ",report='" + j.dump() + "'";
    }
    if (sdmSubscription.contextInfoIsSet()) {
      to_json(j, sdmSubscription.getContextInfo());
      query += ",contextInfo='" + j.dump() + "'";
    }

    query +=
        ",monitoredResourceUris='" + MonitoredResourceUris_json.dump() + "'";

    query += " where ueid='" + ue_id + "' AND subsId=" + subs_id;
  } else {
    to_json(j, problemdetails);
    response_data = j;
    code = Pistache::Http::Code::Not_Found;

    mysql_free_result(res);
    return;
  }

  mysql_free_result(res);
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  response_data = {};
  code = Pistache::Http::Code::No_Content;

  to_json(j, sdmSubscription);
  Logger::udr_server().debug("SdmSubscription PUT - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1277
                             j.dump().c_str());
1278
1279
}

1280
1281
1282
1283
1284
1285
//------------------------------------------------------------------------------
void udr_app::handle_create_sdm_subscriptions(const std::string &ue_id,
                                              SdmSubscription &sdmSubscription,
                                              nlohmann::json &response_data,
                                              Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1286
1287
  MYSQL_ROW row = {};
  nlohmann::json j = {};
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
  int32_t subs_id = 0;
  int32_t count = 0;
  std::string query =
      "SELECT subsId from SdmSubscriptions WHERE ueid='" + ue_id + "'";
  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }
  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    return;
  }

  while (row = mysql_fetch_row(res)) {
    count++;
    if (strcmp(row[0], std::to_string(count).c_str())) {
      subs_id = count;
      break;
    }
  }
  mysql_free_result(res);

  query =
      "insert into SdmSubscriptions set ueid='" + ue_id + "'" +
      ",nfInstanceId='" + sdmSubscription.getNfInstanceId() + "'" +
      (sdmSubscription.implicitUnsubscribeIsSet()
           ? (sdmSubscription.isImplicitUnsubscribe()
                  ? ",implicitUnsubscribe=1"
                  : ",implicitUnsubscribe=0")
           : "") +
      (sdmSubscription.expiresIsSet()
           ? ",expires='" + sdmSubscription.getExpires() + "'"
           : "") +
      ",callbackReference='" + sdmSubscription.getCallbackReference() + "'" +
      (sdmSubscription.dnnIsSet() ? ",dnn='" + sdmSubscription.getDnn() + "'"
                                  : "") +
      (sdmSubscription.subscriptionIdIsSet()
           ? ",subscriptionId='" + sdmSubscription.getSubscriptionId() + "'"
           : "") +
      (sdmSubscription.immediateReportIsSet()
           ? (sdmSubscription.isImmediateReport() ? ",immediateReport=1"
                                                  : ",immediateReport=0")
           : "") +
      (sdmSubscription.supportedFeaturesIsSet()
           ? ",supportedFeatures='" + sdmSubscription.getSupportedFeatures() +
                 "'"
           : "");

  if (sdmSubscription.amfServiceNameIsSet()) {
    to_json(j, sdmSubscription.getAmfServiceName());
    query += ",amfServiceName='" + j.dump() + "'";
  }
  if (sdmSubscription.singleNssaiIsSet()) {
    to_json(j, sdmSubscription.getSingleNssai());
    query += ",singleNssai='" + j.dump() + "'";
  }
  if (sdmSubscription.plmnIdIsSet()) {
    to_json(j, sdmSubscription.getPlmnId());
    query += ",plmnId='" + j.dump() + "'";
  }
  if (sdmSubscription.reportIsSet()) {
    to_json(j, sdmSubscription.getReport());
    query += ",report='" + j.dump() + "'";
  }
  if (sdmSubscription.contextInfoIsSet()) {
    to_json(j, sdmSubscription.getContextInfo());
    query += ",contextInfo='" + j.dump() + "'";
  }

  nlohmann::json MonitoredResourceUris_json(
      sdmSubscription.getMonitoredResourceUris());
  query += ",monitoredResourceUris='" + MonitoredResourceUris_json.dump() + "'";

  if (subs_id && (subs_id == count)) {
    query += ",subsId=" + std::to_string(subs_id);
  }

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  to_json(j, sdmSubscription);
  response_data = j;
  code = Pistache::Http::Code::Created;

  Logger::udr_server().debug("SdmSubscriptions POST - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1379
                             j.dump().c_str());
1380
1381
1382
1383
1384
1385
1386
}

//------------------------------------------------------------------------------
void udr_app::handle_query_sdm_subscriptions(const std::string &ue_id,
                                             nlohmann::json &response_data,
                                             Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1387
  MYSQL_ROW row = {};
1388
1389
1390
  MYSQL_FIELD *field = nullptr;
  std::vector<std::string> fields;

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1391
1392
  nlohmann::json j = {};
  nlohmann::json tmp = {};
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416

  const std::string query =
      "SELECT * from SdmSubscriptions WHERE ueid='" + ue_id + "'";

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL) {
    Logger::udr_server().error("mysql_store_result failure!SQL(%s)",
                               query.c_str());
    return;
  }

  while (field = mysql_fetch_field(res)) {
    fields.push_back(field->name);
  }

  j.clear();

  while (row = mysql_fetch_row(res)) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1417
    SdmSubscription sdmsubscriptions = {};
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484

    tmp.clear();

    for (int i = 0; i < fields.size(); i++) {
      if (!strcmp("nfInstanceId", fields[i].c_str())) {
        sdmsubscriptions.setNfInstanceId(row[i]);
      } else if (!strcmp("implicitUnsubscribe", fields[i].c_str()) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          sdmsubscriptions.setImplicitUnsubscribe(true);
        else
          sdmsubscriptions.setImplicitUnsubscribe(false);
      } else if (!strcmp("expires", fields[i].c_str()) && row[i] != NULL) {
        sdmsubscriptions.setExpires(row[i]);
      } else if (!strcmp("callbackReference", fields[i].c_str())) {
        sdmsubscriptions.setCallbackReference(row[i]);
      } else if (!strcmp("amfServiceName", fields[i].c_str()) &&
                 row[i] != NULL) {
        ServiceName amfservicename;
        nlohmann::json::parse(row[i]).get_to(amfservicename);
        sdmsubscriptions.setAmfServiceName(amfservicename);
      } else if (!strcmp("monitoredResourceUris", fields[i].c_str())) {
        std::vector<std::string> monitoredresourceuris;
        nlohmann::json::parse(row[i]).get_to(monitoredresourceuris);
        sdmsubscriptions.setMonitoredResourceUris(monitoredresourceuris);
      } else if (!strcmp("singleNssai", fields[i].c_str()) && row[i] != NULL) {
        Snssai singlenssai;
        nlohmann::json::parse(row[i]).get_to(singlenssai);
        sdmsubscriptions.setSingleNssai(singlenssai);
      } else if (!strcmp("dnn", fields[i].c_str()) && row[i] != NULL) {
        sdmsubscriptions.setDnn(row[i]);
      } else if (!strcmp("subscriptionId", fields[i].c_str()) &&
                 row[i] != NULL) {
        sdmsubscriptions.setSubscriptionId(row[i]);
      } else if (!strcmp("plmnId", fields[i].c_str()) && row[i] != NULL) {
        PlmnId plmnid;
        nlohmann::json::parse(row[i]).get_to(plmnid);
        sdmsubscriptions.setPlmnId(plmnid);
      } else if (!strcmp("immediateReport", fields[i].c_str()) &&
                 row[i] != NULL) {
        if (strcmp(row[i], "0"))
          sdmsubscriptions.setImmediateReport(true);
        else
          sdmsubscriptions.setImmediateReport(false);
      } else if (!strcmp("report", fields[i].c_str()) && row[i] != NULL) {
        SubscriptionDataSets report;
        nlohmann::json::parse(row[i]).get_to(report);
        sdmsubscriptions.setReport(report);
      } else if (!strcmp("supportedFeatures", fields[i].c_str()) &&
                 row[i] != NULL) {
        sdmsubscriptions.setSupportedFeatures(row[i]);
      } else if (!strcmp("contextInfo", fields[i].c_str()) && row[i] != NULL) {
        ContextInfo contextinfo;
        nlohmann::json::parse(row[i]).get_to(contextinfo);
        sdmsubscriptions.setContextInfo(contextinfo);
      }
    }
    to_json(tmp, sdmsubscriptions);
    j += tmp;
  }

  mysql_free_result(res);

  response_data = j;
  code = Pistache::Http::Code::Ok;

  Logger::udr_server().debug("SdmSubscriptions GET - json:\n\"%s\"",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1485
                             j.dump().c_str());
1486
1487
1488
1489
1490
1491
1492
1493
}

//------------------------------------------------------------------------------
void udr_app::handle_query_sm_data(const std::string &ue_id,
                                   const std::string &serving_plmn_id,
                                   nlohmann::json &response_data,
                                   Pistache::Http::Code &code) {
  MYSQL_RES *res = NULL;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1494
  MYSQL_ROW row = {};
1495
  MYSQL_FIELD *field = nullptr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1496
  nlohmann::json j = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1497
  SessionManagementSubscriptionData sessionmanagementsubscriptiondata = {};
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
  const std::string query =
      "select * from SessionManagementSubscriptionData WHERE ueid='" + ue_id +
      "' and servingPlmnid='" + serving_plmn_id + "'";

  if (mysql_real_query(&mysql, query.c_str(), (unsigned long)query.size())) {
    Logger::udr_server().error("mysql_real_query failure!SQL(%s)",
                               query.c_str());
    return;
  }

  res = mysql_store_result(&mysql);
  if (res == NULL