Beispiel #1
0
    /** Simple class representing a pair of hosts and precomputes the associated h2h intent. */
    private class HostPair {

      private final Host src;
      private final Host dst;

      private final TrafficSelector selector = DefaultTrafficSelector.emptySelector();
      private final TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
      private final List<Constraint> constraint = Lists.newArrayList();
      private final HostToHostIntent intent;

      public HostPair(Host src, Host dst) {
        this.src = src;
        this.dst = dst;
        this.intent =
            HostToHostIntent.builder()
                .appId(appId)
                .one(src.id())
                .two(dst.id())
                .selector(selector)
                .treatment(treatment)
                .constraints(constraint)
                .build();
      }

      public HostToHostIntent h2hIntent() {
        return intent;
      }

      @Override
      public boolean equals(Object o) {
        if (this == o) {
          return true;
        }
        if (o == null || getClass() != o.getClass()) {
          return false;
        }

        HostPair hostPair = (HostPair) o;

        return Objects.equals(src, hostPair.src) && Objects.equals(dst, hostPair.dst);
      }

      @Override
      public int hashCode() {
        return Objects.hash(src, dst);
      }
    }
Beispiel #2
0
 @Override
 public void run() {
   TrafficSelector selector = DefaultTrafficSelector.emptySelector();
   TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
   List<Constraint> constraint = Lists.newArrayList();
   List<Host> hosts = Lists.newArrayList(hostService.getHosts());
   while (!hosts.isEmpty()) {
     Host src = hosts.remove(0);
     for (Host dst : hosts) {
       HostToHostIntent intent =
           HostToHostIntent.builder()
               .appId(appId)
               .one(src.id())
               .two(dst.id())
               .selector(selector)
               .treatment(treatment)
               .constraints(constraint)
               .build();
       existingIntents.add(intent);
       intentService.submit(intent);
     }
   }
 }
Beispiel #3
0
/** Unit tests for the host to host intent class codec. */
public class IntentCodecTest extends AbstractIntentTest {

  private final HostId id1 = hid("12:34:56:78:91:ab/1");
  private final HostId id2 = hid("12:34:56:78:92:ab/1");
  private final ApplicationId appId = new DefaultApplicationId(3, "test");
  final TrafficSelector emptySelector = DefaultTrafficSelector.emptySelector();
  final TrafficTreatment emptyTreatment = DefaultTrafficTreatment.emptyTreatment();
  private final MockCodecContext context = new MockCodecContext();
  final CoreService mockCoreService = createMock(CoreService.class);

  @Before
  public void setUpIntentService() {
    final IntentService mockIntentService = new IntentServiceAdapter();
    context.registerService(IntentService.class, mockIntentService);
    context.registerService(CoreService.class, mockCoreService);
    expect(mockCoreService.getAppId(appId.name())).andReturn(appId);
    replay(mockCoreService);
  }

  /** 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));
  }

  /** 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));
  }

  /** 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));
  }

  /**
   * 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;
  }

  /**
   * Tests the point to point intent JSON codec.
   *
   * @throws IOException if JSON processing fails
   */
  @Test
  public void decodePointToPointIntent() throws IOException {
    JsonCodec<Intent> intentCodec = context.codec(Intent.class);
    assertThat(intentCodec, notNullValue());

    Intent intent = getIntent("PointToPointIntent.json", intentCodec);
    assertThat(intent, notNullValue());
    assertThat(intent, instanceOf(PointToPointIntent.class));

    PointToPointIntent pointIntent = (PointToPointIntent) intent;
    assertThat(pointIntent.priority(), is(55));
    assertThat(pointIntent.ingressPoint().deviceId(), is(did("0000000000000001")));
    assertThat(pointIntent.ingressPoint().port(), is(PortNumber.portNumber(1)));
    assertThat(pointIntent.egressPoint().deviceId(), is(did("0000000000000007")));
    assertThat(pointIntent.egressPoint().port(), is(PortNumber.portNumber(2)));

    assertThat(pointIntent.constraints(), hasSize(1));

    assertThat(pointIntent.selector(), notNullValue());
    assertThat(pointIntent.selector().criteria(), hasSize(1));
    Criterion criterion1 = pointIntent.selector().criteria().iterator().next();
    assertThat(criterion1, instanceOf(EthCriterion.class));
    EthCriterion ethCriterion = (EthCriterion) criterion1;
    assertThat(ethCriterion.mac().toString(), is("11:22:33:44:55:66"));
    assertThat(ethCriterion.type().name(), is("ETH_DST"));

    assertThat(pointIntent.treatment(), notNullValue());
    assertThat(pointIntent.treatment().allInstructions(), hasSize(1));
    Instruction instruction1 = pointIntent.treatment().allInstructions().iterator().next();
    assertThat(instruction1, instanceOf(ModEtherInstruction.class));
    ModEtherInstruction ethInstruction = (ModEtherInstruction) instruction1;
    assertThat(ethInstruction.mac().toString(), is("22:33:44:55:66:77"));
    assertThat(ethInstruction.type().toString(), is("L2MODIFICATION"));
    assertThat(ethInstruction.subtype().toString(), is("ETH_SRC"));
  }

  /**
   * Tests the host to host intent JSON codec.
   *
   * @throws IOException
   */
  @Test
  public void decodeHostToHostIntent() throws IOException {
    JsonCodec<Intent> intentCodec = context.codec(Intent.class);
    assertThat(intentCodec, notNullValue());

    Intent intent = getIntent("HostToHostIntent.json", intentCodec);
    assertThat(intent, notNullValue());
    assertThat(intent, instanceOf(HostToHostIntent.class));

    HostToHostIntent hostIntent = (HostToHostIntent) intent;
    assertThat(hostIntent.priority(), is(7));
    assertThat(hostIntent.constraints(), hasSize(1));
  }
}