Exemple #1
0
 /**
  * Reads in a rule from the given resource and decodes it.
  *
  * @param resourceName resource to use to read the JSON for the rule
  * @return decoded flow rule
  * @throws IOException if processing the resource fails
  */
 private Intent getIntent(String resourceName, JsonCodec intentCodec) throws IOException {
   InputStream jsonStream = IntentCodecTest.class.getResourceAsStream(resourceName);
   JsonNode json = context.mapper().readTree(jsonStream);
   assertThat(json, notNullValue());
   Intent intent = (Intent) intentCodec.decode((ObjectNode) json, context);
   assertThat(intent, notNullValue());
   return intent;
 }
Exemple #2
0
 /**
  * Reads in a rule from the given resource and decodes it.
  *
  * @param resourceName resource to use to read the JSON for the rule
  * @return decoded flow rule
  * @throws IOException if processing the resource fails to decode
  */
 private Alarm getDecodedAlarm(JsonCodec<Alarm> codec, String resourceName) throws IOException {
   final InputStream jsonStream = AlarmCodecTest.class.getResourceAsStream(resourceName);
   final JsonNode json = context.mapper().readTree(jsonStream);
   assertThat(json, notNullValue());
   final Alarm result = codec.decode((ObjectNode) json, context);
   assertThat(result, notNullValue());
   return result;
 }
  @Override
  public TrafficSelector decode(ObjectNode json, CodecContext context) {
    final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);

    JsonNode criteriaJson = json.get(CRITERIA);
    TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
    if (criteriaJson != null) {
      IntStream.range(0, criteriaJson.size())
          .forEach(i -> builder.add(criterionCodec.decode(get(criteriaJson, i), context)));
    }
    return builder.build();
  }
  @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;
  }