IndividualSMContextApi.cpp 8.19 KB
Newer Older
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
1
/**
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
2
 * Nsmf_PDUSession
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
3
4
 * SMF PDU Session Service. © 2019, 3GPP Organizational Partners (ARIB, ATIS,
 * CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
5
6
7
8
 *
 * The version of the OpenAPI document: 1.1.0.alpha-1
 *
 *
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
9
10
11
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
12
 */
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
13

14
15
16
17
18
/*
 * 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
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
19
20
21
 * 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
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 *      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
 */

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
35
#include "IndividualSMContextApi.h"
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
36
37
38
39
40
41
42
43
44

#include <cassert>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>

45
46
#include "logger.hpp"
#include "Helpers.h"
47
#include "mime_parser.hpp"
48
49
50
#include "smf_config.hpp"

extern smf::smf_config smf_cfg;
51

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
52
namespace oai {
53
namespace smf_server {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
54
55
namespace api {

56
57
using namespace oai::smf_server::helpers;
using namespace oai::smf_server::model;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
58

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
59
60
IndividualSMContextApi::IndividualSMContextApi(
    std::shared_ptr<Pistache::Rest::Router> rtr) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
61
  router = rtr;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
62
63
64
}

void IndividualSMContextApi::init() {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
65
  setupRoutes();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
67
68
}

void IndividualSMContextApi::setupRoutes() {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
69
  using namespace Pistache::Rest;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
70

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
71
  Routes::Post(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
72
73
      *router,
      base + smf_cfg.sbi_api_version + "/sm-contexts/:smContextRef/release",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
74
75
      Routes::bind(&IndividualSMContextApi::release_sm_context_handler, this));
  Routes::Post(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
76
77
      *router,
      base + smf_cfg.sbi_api_version + "/sm-contexts/:smContextRef/retrieve",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
78
79
      Routes::bind(&IndividualSMContextApi::retrieve_sm_context_handler, this));
  Routes::Post(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
80
81
      *router,
      base + smf_cfg.sbi_api_version + "/sm-contexts/:smContextRef/modify",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
82
      Routes::bind(&IndividualSMContextApi::update_sm_context_handler, this));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
83

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
84
  // Default handler, called when a route is not found
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
85
86
87
  router->addCustomHandler(Routes::bind(
      &IndividualSMContextApi::individual_sm_context_api_default_handler,
      this));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
88
89
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
90
void IndividualSMContextApi::release_sm_context_handler(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
91
    const Pistache::Rest::Request& request,
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
92
    Pistache::Http::ResponseWriter response) {
93
94
95
96
  Logger::smf_api_server().debug("");
  Logger::smf_api_server().info(
      "Received a SM context Release request from AMF.");
  Logger::smf_api_server().debug("Request body: %s\n", request.body().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
97
  SmContextReleaseMessage smContextReleaseMessage = {};
98

99
  // Simple parser
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
100
  mime_parser sp = {};
101
102
103
104
  if (!sp.parse(request.body())) {
    response.send(Pistache::Http::Code::Bad_Request);
    return;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
105

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
106
  std::vector<mime_part> parts = {};
107
108
109
110
111
  sp.get_mime_parts(parts);
  uint8_t size = parts.size();
  Logger::smf_api_server().debug("Number of MIME parts %d", size);

  // Getting the body param
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
112
  SmContextReleaseData smContextReleaseData = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
113

114
  try {
115
116
117
    if (size > 0) {
      nlohmann::json::parse(parts[0].body.c_str()).get_to(smContextReleaseData);
    } else {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
118
119
      nlohmann::json::parse(request.body().c_str())
          .get_to(smContextReleaseData);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    }

    smContextReleaseMessage.setJsonData(smContextReleaseData);

    for (int i = 1; i < size; i++) {
      if (parts[i].content_type.compare("application/vnd.3gpp.ngap") == 0) {
        smContextReleaseMessage.setBinaryDataN2SmInformation(parts[i].body);
        Logger::smf_api_server().debug("N2 SM information is set");
      }
    }

    // Getting the path params
    auto smContextRef = request.param(":smContextRef").as<std::string>();
    this->release_sm_context(smContextRef, smContextReleaseMessage, response);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
135
136
  } catch (nlohmann::detail::exception& e) {
    // send a 400 error
137
    Logger::smf_api_server().warn(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
138
139
        "Error in parsing json (error: %s), send a msg with a 400 error code "
        "to AMF",
140
        e.what());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
141
142
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
143
144
  } catch (std::exception& e) {
    // send a 500 error
145
146
    Logger::smf_api_server().warn(
        "Error (%s ), send a msg with a 500 error code to AMF", e.what());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
147
148
149
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
150
}
151

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
152
void IndividualSMContextApi::retrieve_sm_context_handler(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
153
    const Pistache::Rest::Request& request,
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
154
    Pistache::Http::ResponseWriter response) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
155
156
157
158
159
  // Getting the path params
  auto smContextRef = request.param(":smContextRef").as<std::string>();

  // Getting the body param

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
160
  SmContextRetrieveData smContextRetrieveData = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
161
162
163
164

  try {
    nlohmann::json::parse(request.body()).get_to(smContextRetrieveData);
    this->retrieve_sm_context(smContextRef, smContextRetrieveData, response);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
165
166
  } catch (nlohmann::detail::exception& e) {
    // send a 400 error
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
167
168
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
169
170
  } catch (std::exception& e) {
    // send a 500 error
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
171
172
173
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
174
}
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
175
void IndividualSMContextApi::update_sm_context_handler(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
176
    const Pistache::Rest::Request& request,
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
177
    Pistache::Http::ResponseWriter response) {
178
  Logger::smf_api_server().debug("");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
179
  Logger::smf_api_server().info(
180
      "Received a SM context update request from AMF.");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
181
  Logger::smf_api_server().debug("Request body: %s\n", request.body().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
182
  SmContextUpdateMessage smContextUpdateMessage = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
183

184
  // Simple parser
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
185
  mime_parser sp = {};
186
187
188
189
  if (!sp.parse(request.body())) {
    response.send(Pistache::Http::Code::Bad_Request);
    return;
  }
190

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
191
  std::vector<mime_part> parts = {};
192
193
194
  sp.get_mime_parts(parts);
  uint8_t size = parts.size();
  Logger::smf_api_server().debug("Number of MIME parts %d", size);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
195
196

  // Getting the body param
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
197
  SmContextUpdateData smContextUpdateData = {};
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
198
  try {
199
    if (size > 0) {
200
      nlohmann::json::parse(parts[0].body.c_str()).get_to(smContextUpdateData);
201
202
203
204
    } else {
      nlohmann::json::parse(request.body().c_str()).get_to(smContextUpdateData);
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
205
206
    smContextUpdateMessage.setJsonData(smContextUpdateData);

207
208
209
    for (int i = 1; i < size; i++) {
      if (parts[i].content_type.compare("application/vnd.3gpp.5gnas") == 0) {
        smContextUpdateMessage.setBinaryDataN1SmMessage(parts[i].body);
210
        Logger::smf_api_server().debug("N1 SM message is set");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
211
212
      } else if (
          parts[i].content_type.compare("application/vnd.3gpp.ngap") == 0) {
213
214
        smContextUpdateMessage.setBinaryDataN2SmInformation(parts[i].body);
        Logger::smf_api_server().debug("N2 SM information is set");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
215
      }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
216
    }
217

218
219
    // Getting the path params
    auto smContextRef = request.param(":smContextRef").as<std::string>();
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
220
221
    this->update_sm_context(smContextRef, smContextUpdateMessage, response);

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
222
223
  } catch (nlohmann::detail::exception& e) {
    // send a 400 error
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
224
    Logger::smf_api_server().warn(
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
225
226
        "Error in parsing json (error: %s), send a msg with a 400 error code "
        "to AMF",
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
227
        e.what());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
228
229
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
230
231
  } catch (std::exception& e) {
    // send a 500 error
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
232
233
    Logger::smf_api_server().warn(
        "Error (%s ), send a msg with a 500 error code to AMF", e.what());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
234
235
236
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
237
238
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
239
240
void IndividualSMContextApi::individual_sm_context_api_default_handler(
    const Pistache::Rest::Request&, Pistache::Http::ResponseWriter response) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
241
242
  response.send(
      Pistache::Http::Code::Not_Found, "The requested method does not exist");
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
243
244
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
245
246
247
}  // namespace api
}  // namespace smf_server
}  // namespace oai