std::shared_ptr getCommonAccessPointAttributes()

in mcrouter/routes/McRouteHandleProvider.cpp [59:151]


std::shared_ptr<CommonAccessPointAttributes> getCommonAccessPointAttributes(
    const folly::dynamic& json,
    CarbonRouterInstanceBase& router) {
  auto ret = std::make_shared<CommonAccessPointAttributes>();
  CommonAccessPointAttributes& apAttr = *ret;
  if (auto jName = json.get_ptr("name")) {
    apAttr.poolName = jName->stringPiece();
  }
  auto& protocol = apAttr.protocol;
  protocol = mc_ascii_protocol;
  if (auto jProtocol = json.get_ptr("protocol")) {
    auto str = parseString(*jProtocol, "protocol");
    if (equalStr("ascii", str, folly::AsciiCaseInsensitive())) {
      protocol = mc_ascii_protocol;
    } else if (equalStr("caret", str, folly::AsciiCaseInsensitive())) {
      protocol = mc_caret_protocol;
    } else if (equalStr("thrift", str, folly::AsciiCaseInsensitive())) {
      protocol = mc_thrift_protocol;
    } else {
      throwLogic("Unknown protocol '{}'", str);
    }
  }

  auto& enableCompression = apAttr.enableCompression;
  enableCompression = router.opts().enable_compression;
  if (auto jCompression = json.get_ptr("enable_compression")) {
    enableCompression = parseBool(*jCompression, "enable_compression");
  }

  auto& mech = apAttr.mech;
  mech = SecurityMech::NONE;
  auto& mechOverride = apAttr.mechOverride;
  auto& withinDcMech = apAttr.withinDcMech;
  auto& crossDcMech = apAttr.crossDcMech;
  auto& crossDcPort = apAttr.crossDcPort;
  auto& withinDcPort = apAttr.withinDcPort;
  auto& port = apAttr.port;
  // default to 0, which doesn't override
  port = 0;
  if (router.configApi().enableSecurityConfig()) {
    if (auto jSecurityMech = json.get_ptr("security_mech_within_dc")) {
      auto mechStr = parseString(*jSecurityMech, "security_mech_within_dc");
      withinDcMech = parseSecurityMech(mechStr);
    }

    if (auto jSecurityMech = json.get_ptr("security_mech_cross_dc")) {
      auto mechStr = parseString(*jSecurityMech, "security_mech_cross_dc");
      crossDcMech = parseSecurityMech(mechStr);
    }

    if (withinDcMech.has_value() && crossDcMech.has_value() &&
        withinDcMech.value() == crossDcMech.value()) {
      // mech is used if nothing is specified in server ap
      mech = withinDcMech.value();
      // mechOverride overrides per-server values
      mechOverride = withinDcMech.value();
      withinDcMech.reset();
      crossDcMech.reset();
    } else {
      if (auto jSecurityMech = json.get_ptr("security_mech")) {
        auto mechStr = parseString(*jSecurityMech, "security_mech");
        mech = parseSecurityMech(mechStr);
      } else if (auto jUseSsl = json.get_ptr("use_ssl")) {
        // deprecated - prefer security_mech
        auto useSsl = parseBool(*jUseSsl, "use_ssl");
        if (useSsl) {
          mech = SecurityMech::TLS;
        }
      }
    }

    if (auto jPort = json.get_ptr("port_override_within_dc")) {
      withinDcPort = parseInt(*jPort, "port_override_within_dc", 1, 65535);
    }

    if (auto jPort = json.get_ptr("port_override_cross_dc")) {
      crossDcPort = parseInt(*jPort, "port_override_cross_dc", 1, 65535);
    }

    if (withinDcPort.has_value() && crossDcPort.has_value() &&
        withinDcPort.value() == crossDcPort.value()) {
      port = withinDcPort.value();
      withinDcPort.reset();
      crossDcPort.reset();
    } else {
      // parse port override only if withinDc & crossDc are not present
      if (auto jPort = json.get_ptr("port_override")) {
        port = parseInt(*jPort, "port_override", 1, 65535);
      }
    }
  }
  return ret;
}