Example #1
0
  @Override
  public McastRoute decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
      return null;
    }

    IpAddress source = IpAddress.valueOf(json.path(SOURCE).asText());
    IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());

    McastRoute route = new McastRoute(source, group, McastRoute.Type.STATIC);

    return route;
  }
  @Override
  public NextObjective decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
      return null;
    }

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

    final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
    final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);

    ObjectiveCodecHelper och = new ObjectiveCodecHelper();

    DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
    final DefaultNextObjective.Builder builder =
        (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);

    // decode id
    JsonNode idJson = json.get(ID);
    checkNotNull(idJson);
    builder.withId(idJson.asInt());

    // decode application id
    ApplicationId appId = coreService.registerApplication(REST_APP_ID);
    builder.fromApp(appId);

    // decode type
    String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();

    switch (typeStr) {
      case "HASHED":
        builder.withType(NextObjective.Type.HASHED);
        break;
      case "BROADCAST":
        builder.withType(NextObjective.Type.BROADCAST);
        break;
      case "FAILOVER":
        builder.withType(NextObjective.Type.FAILOVER);
        break;
      case "SIMPLE":
        builder.withType(NextObjective.Type.SIMPLE);
        break;
      default:
        log.warn(INVALID_TYPE_MESSAGE, typeStr);
        return null;
    }

    // decode treatments
    JsonNode treatmentsJson = json.get(TREATMENTS);
    checkNotNull(treatmentsJson);
    if (treatmentsJson != null) {
      IntStream.range(0, treatmentsJson.size())
          .forEach(
              i -> {
                ObjectNode treatmentJson = get(treatmentsJson, i);
                builder.addTreatment(trafficTreatmentCodec.decode(treatmentJson, context));
              });
    }

    // decode meta
    JsonNode metaJson = json.get(META);
    if (metaJson != null) {
      TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
      builder.withMeta(trafficSelector);
    }

    // decode operation
    String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
    NextObjective nextObjective;

    switch (opStr) {
      case "ADD":
        nextObjective = builder.add();
        break;
      case "REMOVE":
        nextObjective = builder.remove();
        break;
      default:
        log.warn(INVALID_OP_MESSAGE, opStr);
        return null;
    }

    return nextObjective;
  }