Java Code Examples for com.fasterxml.jackson.databind.node.ObjectNode#isObject()

The following examples show how to use com.fasterxml.jackson.databind.node.ObjectNode#isObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Note: ProviderId is not part of JSON representation.
 *       Returned object will have random ProviderId set.
 */
@Override
public Link decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class);
    // TODO: add providerId to JSON if we need to recover them.
    ProviderId pid = new ProviderId("json", "LinkCodec");

    ConnectPoint src = codec.decode(get(json, SRC), context);
    ConnectPoint dst = codec.decode(get(json, DST), context);
    Type type = Type.valueOf(json.get(TYPE).asText());
    Annotations annotations = extractAnnotations(json, context);

    return DefaultLink
            .builder()
            .providerId(pid)
            .src(src)
            .dst(dst)
            .type(type)
            .annotations(annotations)
            .build();
}
 
Example 2
Source File: StopTimeCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public StopTime decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    if (json.get("none") != null) {
        return StopTime.none();
    } else if (json.get("absolute") != null) {
        if (json.get("absolute").get("stop-time") != null) {
            return StopTime.absolute(OffsetDateTime
                    .parse(json.get("absolute").get("stop-time").asText())
                    .toInstant());
        }
        throw new IllegalArgumentException("StopTime absolute must contain "
                + "a stop-time in date-time format with offset");
    } else if (json.get("relative") != null) {
        if (json.get("relative").get("stop-time") != null) {
            return StopTime.relative(Duration.parse(json.get("relative").get("stop-time").asText()));
        }
        throw new IllegalArgumentException("StopTime relative must contain a stop-time duration");
    } else {
        throw new IllegalArgumentException("StopTime must be either none, absolute or relative");
    }
}
 
Example 3
Source File: DpiStatisticsCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public DpiStatistics decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    log.debug("receivedTime={}, full json={} ", json.get("receivedTime"), json);
    JsonNode receivedTimeJson = json.get(RECEIVED_TIME);
    String receivedTime = receivedTimeJson == null ? "" : receivedTimeJson.asText();

    final JsonCodec<DpiStatInfo> dpiStatInfoCodec =
            context.codec(DpiStatInfo.class);

    DpiStatInfo dpiStatInfo =
            dpiStatInfoCodec.decode(get(json, DPI_STATISTICS), context);

    return new DpiStatistics(receivedTime, dpiStatInfo);
}
 
Example 4
Source File: ExternalPeerRouterCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public ExternalPeerRouter decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
            IP_ADDRESS + MISSING_MESSAGE);
    String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
            MAC_ADDRESS + MISSING_MESSAGE);
    String vlanId = nullIsIllegal(json.get(VLAN_ID).asText(),
            VLAN_ID + MISSING_MESSAGE);

    return DefaultExternalPeerRouter.builder()
            .ipAddress(IpAddress.valueOf(ipAddress))
            .macAddress(MacAddress.valueOf(macAddress))
            .vlanId(VlanId.vlanId(vlanId)).build();
}
 
Example 5
Source File: StatsInfoJsonCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public StatsInfo decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    return new DefaultStatsInfo.DefaultBuilder()
            .withStartupTime(json.get(STARTUP_TIME).asLong())
            .withFstPktArrTime(json.get(FST_PKT_ARR_TIME).asLong())
            .withLstPktOffset(json.get(LST_PKT_OFFSET).asInt())
            .withPrevAccBytes(json.get(PREV_ACC_BYTES).asLong())
            .withPrevAccPkts(json.get(PREV_ACC_PKTS).asInt())
            .withCurrAccBytes(json.get(CURR_ACC_BYTES).asLong())
            .withCurrAccPkts(json.get(CURR_ACC_PKTS).asInt())
            .withErrorPkts((short) json.get(ERROR_PKTS).asInt())
            .withDropPkts((short) json.get(DROP_PKTS).asInt())
            .build();
}
 
Example 6
Source File: PortCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Note: Result of {@link Port#element()} returned Port object,
 *       is not a full Device object.
 *       Only it's DeviceId can be used.
 */
@Override
public Port decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    DeviceId did = DeviceId.deviceId(json.get(ELEMENT).asText());
    Device device = new DummyDevice(did);
    PortNumber number = portNumber(json.get(PORT_NAME).asText());
    boolean isEnabled = json.get(IS_ENABLED).asBoolean();
    Type type = Type.valueOf(json.get(TYPE).asText().toUpperCase());
    long portSpeed = json.get(PORT_SPEED).asLong();
    Annotations annotations = extractAnnotations(json, context);

    return new DefaultPort(device, number, isEnabled, type, portSpeed, annotations);
}
 
Example 7
Source File: KeystoneConfigCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public KeystoneConfig decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    String endpoint = nullIsIllegal(json.get(ENDPOINT).asText(),
            ENDPOINT + MISSING_MESSAGE);

    // parse authentication
    JsonNode authJson = nullIsIllegal(json.get(AUTHENTICATION),
            AUTHENTICATION + MISSING_MESSAGE);


    final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class);
    OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context);

    return DefaultKeystoneConfig.builder()
            .endpoint(endpoint)
            .authentication(auth)
            .build();
}
 
Example 8
Source File: StreamsPigResourceGenerator.java    From streams with Apache License 2.0 5 votes vote down vote up
protected StringBuilder appendRootObject(StringBuilder builder, Schema schema, String resourceId, Character separator) {
  ObjectNode propertiesNode = schemaStore.resolveProperties(schema, null, resourceId);
  if (propertiesNode != null && propertiesNode.isObject() && propertiesNode.size() > 0) {
    builder = appendPropertiesNode(builder, schema, propertiesNode, separator);
  }
  return builder;
}
 
Example 9
Source File: NiciraMatchNshSpiCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public NiciraMatchNshSpi decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    // parse nsh path id
    int nshSpiInt = nullIsIllegal(json.get(NSH_PATH_ID),
            NSH_PATH_ID + MISSING_MEMBER_MESSAGE).asInt();
    NshServicePathId nshSpi = NshServicePathId.of(nshSpiInt);

    return new NiciraMatchNshSpi(nshSpi);
}
 
Example 10
Source File: PortPairCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public PortPair decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    PortPair.Builder resultBuilder = new DefaultPortPair.Builder();

    CoreService coreService = context.getService(CoreService.class);

    String id = nullIsIllegal(json.get(ID),
                              ID + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setId(PortPairId.of(id));

    String tenantId = nullIsIllegal(json.get(TENANT_ID),
                                    TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setTenantId(TenantId.tenantId(tenantId));

    String name = nullIsIllegal(json.get(NAME),
                                NAME + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setName(name);

    String description = nullIsIllegal(json.get(DESCRIPTION),
                                       DESCRIPTION + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setDescription(description);

    String ingressPort = nullIsIllegal(json.get(INGRESS),
                                       INGRESS + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setIngress(ingressPort);

    String egressPort = nullIsIllegal(json.get(EGRESS),
                                      EGRESS + MISSING_MEMBER_MESSAGE).asText();
    resultBuilder.setEgress(egressPort);

    return resultBuilder.build();
}
 
Example 11
Source File: MappingInstructionCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public MappingInstruction decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    return new DecodeMappingInstructionCodecHelper(json, context).decode();
}
 
Example 12
Source File: MappingActionCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public MappingAction decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    DecodeMappingActionCodecHelper decoder =
            new DecodeMappingActionCodecHelper(json);
    return decoder.decode();
}
 
Example 13
Source File: OfdpaMatchVlanVidCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public OfdpaMatchVlanVid decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    // parse ofdpa match vlan vid
    short vlanVid = (short) nullIsIllegal(json.get(VLAN_ID),
            VLAN_ID + MISSING_MEMBER_MESSAGE).asInt();
    return new OfdpaMatchVlanVid(VlanId.vlanId(vlanVid));
}
 
Example 14
Source File: MaintenanceDomainCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the MaintenanceDomain entity from JSON.
 *
 * @param json    JSON to decode
 * @param context decoding context
 * @return decoded MaintenanceDomain
 * @throws java.lang.UnsupportedOperationException if the codec does not
 *                                                 support decode operations
 */
@Override
public MaintenanceDomain decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    JsonNode mdNode = json.get(MD);

    String mdName = nullIsIllegal(mdNode.get(MD_NAME), "mdName is required").asText();
    String mdNameType = MdId.MdNameType.CHARACTERSTRING.name();
    if (mdNode.get(MD_NAME_TYPE) != null) {
        mdNameType = mdNode.get(MD_NAME_TYPE).asText();
    }

    try {
        MdId mdId = MdMaNameUtil.parseMdName(mdNameType, mdName);
        MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain.builder(mdId);
        JsonNode mdLevelNode = mdNode.get(MD_LEVEL);
        if (mdLevelNode != null) {
            MdLevel mdLevel = MdLevel.valueOf(mdLevelNode.asText());
            builder = builder.mdLevel(mdLevel);
        }
        JsonNode mdNumericIdNode = mdNode.get(MD_NUMERIC_ID);
        if (mdNumericIdNode != null) {
            short mdNumericId = (short) mdNumericIdNode.asInt();
            builder = builder.mdNumericId(mdNumericId);
        }

        return builder.build();
    } catch (CfmConfigException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example 15
Source File: InstancePortCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InstancePort decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
            NETWORK_ID + MISSING_MESSAGE);
    String portId = nullIsIllegal(json.get(PORT_ID).asText(),
            PORT_ID + MISSING_MESSAGE);
    String macAddress = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
            MAC_ADDRESS + MISSING_MESSAGE);
    String ipAddress = nullIsIllegal(json.get(IP_ADDRESS).asText(),
            IP_ADDRESS + MISSING_MESSAGE);
    String deviceId = nullIsIllegal(json.get(DEVICE_ID).asText(),
            DEVICE_ID + MISSING_MESSAGE);
    String portNumber = nullIsIllegal(json.get(PORT_NUMBER).asText(),
            PORT_NUMBER + MISSING_MESSAGE);
    String state = nullIsIllegal(json.get(STATE).asText(),
            STATE + MISSING_MESSAGE);

    return DefaultInstancePort.builder()
            .networkId(networkId)
            .portId(portId)
            .macAddress(MacAddress.valueOf(macAddress))
            .ipAddress(IpAddress.valueOf(ipAddress))
            .deviceId(DeviceId.deviceId(deviceId))
            .portNumber(PortNumber.fromString(portNumber))
            .state(InstancePort.State.valueOf(state)).build();
}
 
Example 16
Source File: HostCodec.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public Host decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    MacAddress mac = MacAddress.valueOf(nullIsIllegal(
            json.get(MAC), MAC + MISSING_MEMBER_MESSAGE).asText());
    VlanId vlanId = VlanId.vlanId(nullIsIllegal(
            json.get(VLAN), VLAN + MISSING_MEMBER_MESSAGE).asText());
    HostId id = HostId.hostId(mac, vlanId);

    ArrayNode locationNodes = nullIsIllegal(
            (ArrayNode) json.get(HOST_LOCATIONS), HOST_LOCATIONS + MISSING_MEMBER_MESSAGE);
    Set<HostLocation> hostLocations =
            context.codec(HostLocation.class).decode(locationNodes, context)
            .stream().collect(Collectors.toSet());

    ArrayNode ipNodes = nullIsIllegal(
            (ArrayNode) json.get(IP_ADDRESSES), IP_ADDRESSES + MISSING_MEMBER_MESSAGE);
    Set<IpAddress> ips = new HashSet<>();
    ipNodes.forEach(ipNode -> {
        ips.add(IpAddress.valueOf(ipNode.asText()));
    });

    // check optional fields
    JsonNode innerVlanIdNode = json.get(INNER_VLAN);
    VlanId innerVlanId = (null == innerVlanIdNode) ? VlanId.NONE :
            VlanId.vlanId(innerVlanIdNode.asText());
    JsonNode outerTpidNode = json.get(OUTER_TPID);
    EthType outerTpid = (null == outerTpidNode) ? EthType.EtherType.UNKNOWN.ethType() :
            EthType.EtherType.lookup((short) (Integer.decode(outerTpidNode.asText()) & 0xFFFF)).ethType();
    JsonNode configuredNode = json.get(IS_CONFIGURED);
    boolean configured = (null == configuredNode) ? false : configuredNode.asBoolean();
    JsonNode suspendedNode = json.get(IS_SUSPENDED);
    boolean suspended = (null == suspendedNode) ? false : suspendedNode.asBoolean();

    ArrayNode auxLocationNodes = (ArrayNode) json.get(AUX_LOCATIONS);
    Set<HostLocation> auxHostLocations = (null == auxLocationNodes) ? null :
            context.codec(HostLocation.class).decode(auxLocationNodes, context)
                    .stream().collect(Collectors.toSet());

    Annotations annotations = extractAnnotations(json, context);

    return new DefaultHost(ProviderId.NONE, id, mac, vlanId,
                           hostLocations, auxHostLocations, ips, innerVlanId,
                           outerTpid, configured, suspended, annotations);
}
 
Example 17
Source File: FlowRuleCodec.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public FlowRule decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();

    CoreService coreService = context.getService(CoreService.class);
    JsonNode appIdJson = json.get(APP_ID);
    String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
    resultBuilder.fromApp(coreService.registerApplication(appId));

    int priority = nullIsIllegal(json.get(PRIORITY),
            PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
    resultBuilder.withPriority(priority);

    boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT),
            IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
    if (isPermanent) {
        resultBuilder.makePermanent();
    } else {
        resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT),
                        TIMEOUT
                        + MISSING_MEMBER_MESSAGE
                        + " if the flow is temporary").asInt());
    }

    JsonNode tableIdJson = json.get(TABLE_ID);
    if (tableIdJson != null) {
        String tableId = tableIdJson.asText();
        try {
            int tid = Integer.parseInt(tableId);
            resultBuilder.forTable(IndexTableId.of(tid));
        } catch (NumberFormatException e) {
            resultBuilder.forTable(PiTableId.of(tableId));
        }
    }

    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
            DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    resultBuilder.forDevice(deviceId);

    ObjectNode treatmentJson = get(json, TREATMENT);
    if (treatmentJson != null) {
        JsonCodec<TrafficTreatment> treatmentCodec =
                context.codec(TrafficTreatment.class);
        resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
    }

    ObjectNode selectorJson = get(json, SELECTOR);
    if (selectorJson != null) {
        JsonCodec<TrafficSelector> selectorCodec =
                context.codec(TrafficSelector.class);
        resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
    }

    return resultBuilder.build();
}
 
Example 18
Source File: K8sNodeCodec.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public K8sNode decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    String hostname = nullIsIllegal(json.get(HOSTNAME).asText(),
            HOSTNAME + MISSING_MESSAGE);
    String type = nullIsIllegal(json.get(TYPE).asText(),
            TYPE + MISSING_MESSAGE);
    String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
            MANAGEMENT_IP + MISSING_MESSAGE);

    DefaultK8sNode.Builder nodeBuilder = DefaultK8sNode.builder()
            .hostname(hostname)
            .type(K8sNode.Type.valueOf(type))
            .state(K8sNodeState.INIT)
            .managementIp(IpAddress.valueOf(mIp));

    if (json.get(DATA_IP) != null) {
        nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
    }

    JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
    if (intBridgeJson != null) {
        nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
    }

    JsonNode extBridgeJson = json.get(EXTERNAL_BRIDGE);
    if (extBridgeJson != null) {
        nodeBuilder.extBridge(DeviceId.deviceId(extBridgeJson.asText()));
    }

    JsonNode localBridgeJson = json.get(LOCAL_BRIDGE);
    if (localBridgeJson != null) {
        nodeBuilder.localBridge(DeviceId.deviceId(localBridgeJson.asText()));
    }

    JsonNode extIntfJson = json.get(EXTERNAL_INTF);
    if (extIntfJson != null) {
        nodeBuilder.extIntf(extIntfJson.asText());
    }

    JsonNode extBridgeIpJson = json.get(EXTERNAL_BRIDGE_IP);
    if (extBridgeIpJson != null) {
        nodeBuilder.extBridgeIp(IpAddress.valueOf(extBridgeIpJson.asText()));
    }

    JsonNode extGatewayIpJson = json.get(EXTERNAL_GATEWAY_IP);
    if (extGatewayIpJson != null) {
        nodeBuilder.extGatewayIp(IpAddress.valueOf(extGatewayIpJson.asText()));
    }

    log.trace("node is {}", nodeBuilder.build().toString());

    return nodeBuilder.build();
}
 
Example 19
Source File: NiciraExtensionTreatmentInterpreter.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public ExtensionTreatment decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    // parse extension type
    int typeInt = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asInt();
    ExtensionTreatmentType type = new ExtensionTreatmentType(typeInt);

    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
        return context.codec(NiciraSetTunnelDst.class).decode(json, context);
    }

    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
        return context.codec(NiciraResubmit.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
        return context.codec(NiciraResubmitTable.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
        return context.codec(NiciraSetNshSpi.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
        return context.codec(NiciraSetNshSi.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
        return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
        return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
        return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
        return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
    }
    if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())
            || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())
            || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())
            || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
        return context.codec(MoveExtensionTreatment.class).decode(json, context);
    }
    throw new UnsupportedOperationException(
            "Driver does not support extension type " + type.toString());
}
 
Example 20
Source File: K8sApiConfigCodec.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public K8sApiConfig decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }

    Scheme scheme = Scheme.valueOf(nullIsIllegal(
            json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
    IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(
            json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
    int port = json.get(PORT).asInt();

    K8sApiConfig.Builder builder = DefaultK8sApiConfig.builder()
            .scheme(scheme)
            .ipAddress(ipAddress)
            .port(port)
            .state(DISCONNECTED);

    JsonNode tokenJson = json.get(TOKEN);
    JsonNode caCertDataJson = json.get(CA_CERT_DATA);
    JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
    JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);

    String token = "";
    String caCertData = "";
    String clientCertData = "";
    String clientKeyData = "";

    if (scheme == HTTPS) {
        caCertData = nullIsIllegal(caCertDataJson.asText(),
                CA_CERT_DATA + MISSING_MESSAGE);
        clientCertData = nullIsIllegal(clientCertDataJson.asText(),
                CLIENT_CERT_DATA + MISSING_MESSAGE);
        clientKeyData = nullIsIllegal(clientKeyDataJson.asText(),
                CLIENT_KEY_DATA + MISSING_MESSAGE);

        if (tokenJson != null) {
            token = tokenJson.asText();
        }

    } else {
        if (tokenJson != null) {
            token = tokenJson.asText();
        }

        if (caCertDataJson != null) {
            caCertData = caCertDataJson.asText();
        }

        if (clientCertDataJson != null) {
            clientCertData = clientCertDataJson.asText();
        }

        if (clientKeyDataJson != null) {
            clientKeyData = clientKeyDataJson.asText();
        }
    }

    if (StringUtils.isNotEmpty(token)) {
        builder.token(token);
    }

    if (StringUtils.isNotEmpty(caCertData)) {
        builder.caCertData(caCertData);
    }

    if (StringUtils.isNotEmpty(clientCertData)) {
        builder.clientCertData(clientCertData);
    }

    if (StringUtils.isNotEmpty(clientKeyData)) {
        builder.clientKeyData(clientKeyData);
    }

    return builder.build();
}