예제 #1
0
  @Test
  public void alarmCodecTestWithOptionalField() {
    final JsonCodec<Alarm> codec = context.codec(Alarm.class);
    assertThat(codec, is(notNullValue()));

    final ObjectNode alarmJson = codec.encode(alarmWithSource, context);
    assertThat(alarmJson, notNullValue());
    assertThat(alarmJson, matchesAlarm(alarmWithSource));
  }
예제 #2
0
  @Test
  public void alarmCodecTestWithOptionalFieldMissing() {
    // context.registerService(AlarmService.class, new AlarmServiceAdapter());
    final JsonCodec<Alarm> codec = context.codec(Alarm.class);
    assertThat(codec, is(notNullValue()));

    final ObjectNode alarmJson = codec.encode(alarmMinimumFields, context);
    assertThat(alarmJson, notNullValue());
    assertThat(alarmJson, matchesAlarm(alarmMinimumFields));
  }
예제 #3
0
  /** Tests the encoding of a host to host intent. */
  @Test
  public void hostToHostIntent() {
    final HostToHostIntent intent =
        HostToHostIntent.builder().appId(appId).one(id1).two(id2).build();

    final JsonCodec<HostToHostIntent> intentCodec = context.codec(HostToHostIntent.class);
    assertThat(intentCodec, notNullValue());

    final ObjectNode intentJson = intentCodec.encode(intent, context);
    assertThat(intentJson, matchesIntent(intent));
  }
예제 #4
0
  @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();
  }
예제 #5
0
  /** Tests the encoding of an intent with treatment, selector and constraints specified. */
  @Test
  public void intentWithTreatmentSelectorAndConstraints() {
    ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
    ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
    DeviceId did1 = did("device1");
    DeviceId did2 = did("device2");
    DeviceId did3 = did("device3");
    Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
    final TrafficSelector selector =
        DefaultTrafficSelector.builder()
            .matchIPProtocol((byte) 3)
            .matchMplsLabel(MplsLabel.mplsLabel(4))
            .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
            .add(Criteria.matchLambda(ochSignal))
            .matchEthDst(MacAddress.BROADCAST)
            .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
            .build();
    final TrafficTreatment treatment =
        DefaultTrafficTreatment.builder()
            .add(Instructions.modL0Lambda(new IndexedLambda(33)))
            .setMpls(MplsLabel.mplsLabel(44))
            .setOutput(PortNumber.CONTROLLER)
            .setEthDst(MacAddress.BROADCAST)
            .build();

    final List<Constraint> constraints =
        ImmutableList.of(
            new BandwidthConstraint(Bandwidth.bps(1.0)),
            new LambdaConstraint(new IndexedLambda(3)),
            new AnnotationConstraint("key", 33.0),
            new AsymmetricPathConstraint(),
            new LatencyConstraint(Duration.ofSeconds(2)),
            new ObstacleConstraint(did1, did2),
            new WaypointConstraint(did3));

    final PointToPointIntent intent =
        PointToPointIntent.builder()
            .appId(appId)
            .selector(selector)
            .treatment(treatment)
            .ingressPoint(ingress)
            .egressPoint(egress)
            .constraints(constraints)
            .build();

    final JsonCodec<PointToPointIntent> intentCodec = context.codec(PointToPointIntent.class);
    assertThat(intentCodec, notNullValue());

    final ObjectNode intentJson = intentCodec.encode(intent, context);
    assertThat(intentJson, matchesIntent(intent));
  }
예제 #6
0
  @Override
  public ObjectNode encode(TrafficSelector selector, CodecContext context) {
    checkNotNull(selector, "Traffic selector cannot be null");

    final ObjectNode result = context.mapper().createObjectNode();
    final ArrayNode jsonCriteria = result.putArray(CRITERIA);

    if (selector.criteria() != null) {
      final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);
      for (final Criterion criterion : selector.criteria()) {
        jsonCriteria.add(criterionCodec.encode(criterion, context));
      }
    }

    return result;
  }
예제 #7
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;
 }
예제 #8
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;
 }
예제 #9
0
  /** Tests the encoding of a point to point intent. */
  @Test
  public void pointToPointIntent() {
    ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
    ConnectPoint egress = NetTestTools.connectPoint("egress", 2);

    final PointToPointIntent intent =
        PointToPointIntent.builder()
            .appId(appId)
            .selector(emptySelector)
            .treatment(emptyTreatment)
            .ingressPoint(ingress)
            .egressPoint(egress)
            .build();

    final JsonCodec<PointToPointIntent> intentCodec = context.codec(PointToPointIntent.class);
    assertThat(intentCodec, notNullValue());

    final ObjectNode intentJson = intentCodec.encode(intent, context);
    assertThat(intentJson, matchesIntent(intent));
  }
예제 #10
0
  @Override
  public ObjectNode encode(NextObjective nextObjective, CodecContext context) {

    checkNotNull(nextObjective, NOT_NULL_MESSAGE);

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

    // encode common properties
    ObjectiveCodecHelper och = new ObjectiveCodecHelper();
    ObjectNode result = och.encode(nextObjective, context);

    // encode id
    result.put(ID, nextObjective.id());

    // encode type
    result.put(TYPE, nextObjective.type().toString());

    // encode operation
    result.put(OPERATION, nextObjective.op().toString());

    // encode treatments
    ArrayNode treatments = context.mapper().createArrayNode();
    nextObjective
        .next()
        .forEach(
            t -> {
              ObjectNode treatmentJson = trafficTreatmentCodec.encode(t, context);
              treatments.add(treatmentJson);
            });
    result.set(TREATMENTS, treatments);

    // encode meta
    if (nextObjective.meta() != null) {
      ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(nextObjective.meta(), context);
      result.set(META, trafficSelectorNode);
    }

    return result;
  }
예제 #11
0
  @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;
  }