Skip to content
Snippets Groups Projects
SMPoliciesCollectionApi.cpp 3.39 KiB
Newer Older
Rohan's avatar
Rohan committed
/**
 * Npcf_SMPolicyControl API
 * Session Management Policy Control Service © 2020, 3GPP Organizational
 * Partners (ARIB, ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 1.1.1.alpha-5
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */
Rohan's avatar
Rohan committed

#include "SMPoliciesCollectionApi.h"
#include "api_defs.h"
#include "pcf_config.hpp"
Rohan's avatar
Rohan committed

namespace oai {
namespace pcf {
namespace api {

using namespace oai::model::common::helpers;
using namespace oai::model::pcf;
Rohan's avatar
Rohan committed

SMPoliciesCollectionApi::SMPoliciesCollectionApi(
    const std::shared_ptr<Pistache::Rest::Router>& rtr)
    : router(rtr) {}
Rohan's avatar
Rohan committed

void SMPoliciesCollectionApi::init() {
Rohan's avatar
Rohan committed
}

void SMPoliciesCollectionApi::setupRoutes() {
  using namespace Pistache::Rest;
Rohan's avatar
Rohan committed

      *router, sm_policies::get_route(),
      Routes::bind(&SMPoliciesCollectionApi::create_sm_policy_handler, this));
Rohan's avatar
Rohan committed

  // Default handler, called when a route is not found
  router->addCustomHandler(Routes::bind(
      &SMPoliciesCollectionApi::sm_policies_collection_api_default_handler,
      this));
Rohan's avatar
Rohan committed
}

std::pair<Pistache::Http::Code, std::string>
Romain Lacroix's avatar
Romain Lacroix committed
SMPoliciesCollectionApi::handleParsingException(
    const std::exception& /* ex */) const noexcept {
  try {
    throw;
  } catch (nlohmann::detail::exception& e) {
    return std::make_pair(Pistache::Http::Code::Bad_Request, e.what());
    //    } catch (org::openapitools::server::helpers::ValidationException &e) {
    //        return std::make_pair(Pistache::Http::Code::Bad_Request,
    //        e.what());
  } catch (std::exception& e) {
    return std::make_pair(
        Pistache::Http::Code::Internal_Server_Error, e.what());
  }
Rohan's avatar
Rohan committed
}

std::pair<Pistache::Http::Code, std::string>
SMPoliciesCollectionApi::handleOperationException(
    const std::exception& ex) const noexcept {
  return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what());
Rohan's avatar
Rohan committed
}

void SMPoliciesCollectionApi::create_sm_policy_handler(
    const Pistache::Rest::Request& request,
    Pistache::Http::ResponseWriter response) {
  try {
Rohan's avatar
Rohan committed
    // Getting the body param
Rohan's avatar
Rohan committed
    SmPolicyContextData smPolicyContextData;

    try {
      nlohmann::json::parse(request.body()).get_to(smPolicyContextData);
      smPolicyContextData.validate();
    } catch (std::exception& e) {
      const std::pair<Pistache::Http::Code, std::string> errorInfo =
          this->handleParsingException(e);
      response.send(errorInfo.first, errorInfo.second);
      return;
Rohan's avatar
Rohan committed
    }

    try {
      this->create_sm_policy(smPolicyContextData, response);
    } catch (Pistache::Http::HttpError& e) {
      response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
      return;
    } catch (std::exception& e) {
      const std::pair<Pistache::Http::Code, std::string> errorInfo =
          this->handleOperationException(e);
      response.send(errorInfo.first, errorInfo.second);
      return;
Rohan's avatar
Rohan committed
    }

  } catch (std::exception& e) {
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
  }
Rohan's avatar
Rohan committed
}

void SMPoliciesCollectionApi::sm_policies_collection_api_default_handler(
    const Pistache::Rest::Request&, Pistache::Http::ResponseWriter response) {
  response.send(
      Pistache::Http::Code::Not_Found, "The requested method does not exist");
Rohan's avatar
Rohan committed
}

}  // namespace api
}  // namespace pcf
}  // namespace oai