[ONOS-5264] [ONOS-5242] Intents w/ FilteredConnectPoint
Change-Id: Ibe9062c904ad9a6c3ba001fe57be7cec49eb8a4d
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionCompiler.java
index d6db8c8..7701f29 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionCompiler.java
@@ -17,32 +17,39 @@
package org.onosproject.net.intent.impl.compiler;
import com.google.common.collect.SetMultimap;
+import com.google.common.collect.Sets;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.IpPrefix;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.PortNumber;
+import org.onosproject.net.FilteredConnectPoint;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criteria;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.MplsCriterion;
+import org.onosproject.net.flow.criteria.TunnelIdCriterion;
+import org.onosproject.net.flow.criteria.VlanIdCriterion;
import org.onosproject.net.flow.instructions.Instruction;
import org.onosproject.net.flow.instructions.L0ModificationInstruction;
import org.onosproject.net.flow.instructions.L1ModificationInstruction;
import org.onosproject.net.flow.instructions.L2ModificationInstruction;
import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
-import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
-import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
-import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
import org.onosproject.net.flow.instructions.L3ModificationInstruction;
-import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
-import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
-import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
import org.onosproject.net.flow.instructions.L4ModificationInstruction;
import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
import org.onosproject.net.intent.IntentCompilationException;
@@ -53,11 +60,19 @@
import java.util.Set;
import java.util.stream.Collectors;
+import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
+import static org.onosproject.net.flow.criteria.Criterion.Type.TUNNEL_ID;
+import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
+
+
/**
* Shared APIs and implementations for Link Collection compilers.
*/
public class LinkCollectionCompiler<T> {
+ private static final Set<Criterion.Type> TAG_CRITERION_TYPES =
+ Sets.immutableEnumSet(VLAN_VID, MPLS_LABEL, TUNNEL_ID);
+
/**
* Helper class to encapsulate treatment and selector.
*/
@@ -107,13 +122,13 @@
for (ConnectPoint egressPoint : intent.egressPoints()) {
outputPorts.put(egressPoint.deviceId(), egressPoint.port());
}
-
}
/**
- * Helper method to compute ingress and egress ports.
+ * Gets ingress and egress port number of specific device.
*
- * @param intent the related intents
+ * @param intent the related
+ * @param deviceId device Id
* @param ingressPorts the ingress ports to compute
* @param egressPorts the egress ports to compute
*/
@@ -156,11 +171,11 @@
* in the flow representation (Rule, Objective).
*
* @param intent the intent to compile
- * @param inPort the input port
+ * @param inPort the input port of this device
* @param deviceId the current device
- * @param outPorts the output ports
- * @param ingressPorts the ingress ports
- * @param egressPorts the egress ports
+ * @param outPorts the output ports of this device
+ * @param ingressPorts intent ingress ports of this device
+ * @param egressPorts intent egress ports of this device
* @return the forwarding instruction object which encapsulates treatment and selector
*/
protected ForwardingInstructions createForwardingInstructions(LinkCollectionIntent intent,
@@ -170,119 +185,214 @@
Set<PortNumber> ingressPorts,
Set<PortNumber> egressPorts) {
- TrafficTreatment.Builder defaultTreatmentBuilder = DefaultTrafficTreatment.builder();
- outPorts.forEach(defaultTreatmentBuilder::setOutput);
- TrafficTreatment outputOnlyTreatment = defaultTreatmentBuilder.build();
- TrafficSelector.Builder selectorBuilder;
- TrafficTreatment treatment;
- TrafficTreatment intentTreatment;
+ TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
+ TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(intent.selector());
+ selectorBuilder.matchInPort(inPort);
if (!intent.applyTreatmentOnEgress()) {
- TrafficTreatment.Builder ingressTreatmentBuilder = DefaultTrafficTreatment.builder(intent.treatment());
- outPorts.forEach(ingressTreatmentBuilder::setOutput);
- intentTreatment = ingressTreatmentBuilder.build();
+ // FIXME: currently, we assume this intent is compile from mp2sp intent
+ Optional<FilteredConnectPoint> filteredIngressPoint =
+ getFilteredConnectPointFromIntent(deviceId, inPort, intent);
+ Optional<FilteredConnectPoint> filteredEgressPoint =
+ intent.filteredEgressPoints().stream().findFirst();
- if (ingressPorts.contains(inPort)) {
- if (intent.ingressSelectors() != null && !intent.ingressSelectors().isEmpty()) {
- /**
- * We iterate on the ingress points looking for the connect point
- * associated to inPort.
- */
- Optional<ConnectPoint> connectPoint = intent.ingressPoints()
- .stream()
- .filter(ingressPoint -> ingressPoint.port().equals(inPort)
- && ingressPoint.deviceId().equals(deviceId))
- .findFirst();
- if (connectPoint.isPresent()) {
- selectorBuilder = DefaultTrafficSelector
- .builder(intent.ingressSelectors().get(connectPoint.get()));
- } else {
- throw new IntentCompilationException("Looking for connect point associated to the selector." +
- "inPort not in IngressPoints");
- }
+ if (filteredIngressPoint.isPresent()) {
+ // Ingress device
+ intent.treatment().allInstructions().stream()
+ .filter(inst -> inst.type() != Instruction.Type.NOACTION)
+ .forEach(treatmentBuilder::add);
+
+ if (filteredEgressPoint.isPresent()) {
+ // Apply selector from ingress point
+ filteredIngressPoint.get()
+ .trafficSelector()
+ .criteria()
+ .forEach(selectorBuilder::add);
+
+ TrafficTreatment forwardingTreatment =
+ forwardingTreatment(filteredIngressPoint.get(),
+ filteredEgressPoint.get());
+
+ forwardingTreatment.allInstructions().stream()
+ .filter(inst -> inst.type() != Instruction.Type.NOACTION)
+ .forEach(treatmentBuilder::add);
} else {
- selectorBuilder = DefaultTrafficSelector.builder(intent.selector());
+ throw new IntentCompilationException("Can't find filtered connection point");
}
- treatment = this.updateBuilder(ingressTreatmentBuilder, selectorBuilder.build()).build();
+
} else {
- selectorBuilder = this.createSelectorFromFwdInstructions(
- new ForwardingInstructions(intentTreatment, intent.selector())
- );
- treatment = outputOnlyTreatment;
+ // Not ingress device, won't apply treatments.
+ // Use selector by treatment from intent.
+ updateBuilder(selectorBuilder, intent.treatment());
+
+ // Selector should be overridden by selector from connect point.
+ if (filteredEgressPoint.isPresent()) {
+ filteredEgressPoint.get()
+ .trafficSelector()
+ .criteria()
+ .forEach(selectorBuilder::add);
+ }
+
}
+
+ outPorts.forEach(treatmentBuilder::setOutput);
+
} else {
- if (outPorts.stream().allMatch(egressPorts::contains)) {
- TrafficTreatment.Builder egressTreatmentBuilder = DefaultTrafficTreatment.builder();
- if (intent.egressTreatments() != null && !intent.egressTreatments().isEmpty()) {
- for (PortNumber outPort : outPorts) {
- Optional<ConnectPoint> connectPoint = intent.egressPoints()
- .stream()
- .filter(egressPoint -> egressPoint.port().equals(outPort)
- && egressPoint.deviceId().equals(deviceId))
- .findFirst();
- if (connectPoint.isPresent()) {
- TrafficTreatment egressTreatment = intent.egressTreatments().get(connectPoint.get());
- this.addTreatment(egressTreatmentBuilder, egressTreatment);
- egressTreatmentBuilder = this.updateBuilder(egressTreatmentBuilder, intent.selector());
- egressTreatmentBuilder.setOutput(outPort);
- } else {
- throw new IntentCompilationException("Looking for connect point associated to " +
- "the treatment. outPort not in egressPoints");
- }
- }
- } else {
- egressTreatmentBuilder = this
- .updateBuilder(DefaultTrafficTreatment.builder(intent.treatment()), intent.selector());
- outPorts.forEach(egressTreatmentBuilder::setOutput);
- }
- selectorBuilder = DefaultTrafficSelector.builder(intent.selector());
- treatment = egressTreatmentBuilder.build();
+ // FIXME: currently, we assume this intent is compile from sp2mp intent
+ Optional<FilteredConnectPoint> filteredIngressPoint =
+ intent.filteredIngressPoints().stream().findFirst();
+
+ if (filteredIngressPoint.isPresent()) {
+ // Apply selector from ingress point
+ filteredIngressPoint.get()
+ .trafficSelector()
+ .criteria()
+ .forEach(selectorBuilder::add);
} else {
- selectorBuilder = DefaultTrafficSelector.builder(intent.selector());
- treatment = outputOnlyTreatment;
+ throw new IntentCompilationException(
+ "Filtered connection point for ingress" +
+ "point does not exist");
+ }
+
+ for (PortNumber outPort : outPorts) {
+ Optional<FilteredConnectPoint> filteredEgressPoint =
+ getFilteredConnectPointFromIntent(deviceId, outPort, intent);
+
+ if (filteredEgressPoint.isPresent()) {
+ // Egress port, apply treatment + forwarding treatment
+ intent.treatment().allInstructions().stream()
+ .filter(inst -> inst.type() != Instruction.Type.NOACTION)
+ .forEach(treatmentBuilder::add);
+
+ TrafficTreatment forwardingTreatment =
+ forwardingTreatment(filteredIngressPoint.get(),
+ filteredEgressPoint.get());
+ forwardingTreatment.allInstructions().stream()
+ .filter(inst -> inst.type() != Instruction.Type.NOACTION)
+ .forEach(treatmentBuilder::add);
+ }
+
+ treatmentBuilder.setOutput(outPort);
+
+ }
+
+ }
+
+
+ return new ForwardingInstructions(treatmentBuilder.build(), selectorBuilder.build());
+
+ }
+
+ /**
+ * Get FilteredConnectPoint from LinkCollectionIntent.
+ * @param deviceId device Id for connect point
+ * @param portNumber port number
+ * @param intent source intent
+ * @return filtered connetion point
+ */
+ private Optional<FilteredConnectPoint> getFilteredConnectPointFromIntent(DeviceId deviceId,
+ PortNumber portNumber,
+ LinkCollectionIntent intent) {
+ Set<FilteredConnectPoint> filteredConnectPoints =
+ Sets.union(intent.filteredIngressPoints(), intent.filteredEgressPoints());
+ return filteredConnectPoints.stream()
+ .filter(port -> port.connectPoint().deviceId().equals(deviceId))
+ .filter(port -> port.connectPoint().port().equals(portNumber))
+ .findFirst();
+ }
+
+ /**
+ * Get tag criterion from selector.
+ * The criterion should be one of type in tagCriterionTypes.
+ *
+ * @param selector selector
+ * @return Criterion that matched, if there is no tag criterion, return null
+ */
+ private Criterion getTagCriterion(TrafficSelector selector) {
+ return selector.criteria().stream()
+ .filter(criterion -> TAG_CRITERION_TYPES.contains(criterion.type()))
+ .findFirst()
+ .orElse(Criteria.dummy());
+
+ }
+
+ /**
+ * Compares tag type between ingress and egress point and generate
+ * treatment for egress point of intent.
+ *
+ * @param ingress ingress point for the intent
+ * @param egress egress point for the intent
+ * @return Builder of TrafficTreatment
+ */
+ private TrafficTreatment forwardingTreatment(FilteredConnectPoint ingress,
+ FilteredConnectPoint egress) {
+
+
+ if (ingress.trafficSelector().equals(egress.trafficSelector())) {
+ return DefaultTrafficTreatment.emptyTreatment();
+ }
+
+ TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
+
+ /*
+ * "null" means there is no tag for the port
+ * Tag criterion will be null if port is normal connection point
+ */
+ Criterion ingressTagCriterion = getTagCriterion(ingress.trafficSelector());
+ Criterion egressTagCriterion = getTagCriterion(egress.trafficSelector());
+
+ if (ingressTagCriterion.type() != egressTagCriterion.type()) {
+
+ /*
+ * Tag type of ingress port and egress port are different.
+ * Need to remove tag from ingress, then add new tag for egress.
+ * Remove nothing if ingress port use VXLAN or there is no tag
+ * on ingress port.
+ */
+ switch (ingressTagCriterion.type()) {
+ case VLAN_VID:
+ builder.popVlan();
+ break;
+ case MPLS_LABEL:
+ builder.popMpls();
+ break;
+ default:
+ break;
+ }
+
+ /*
+ * Push new tag for egress port.
+ */
+ switch (egressTagCriterion.type()) {
+ case VLAN_VID:
+ builder.pushVlan();
+ break;
+ case MPLS_LABEL:
+ builder.pushMpls();
+ break;
+ default:
+ break;
}
}
- TrafficSelector selector = selectorBuilder.matchInPort(inPort).build();
-
- return new ForwardingInstructions(treatment, selector);
-
- }
-
- /**
- * Update a builder using a treatment.
- * @param builder the builder to update
- * @param treatment the treatment to add
- * @return the new builder
- */
- private TrafficTreatment.Builder addTreatment(TrafficTreatment.Builder builder, TrafficTreatment treatment) {
- builder.deferred();
- for (Instruction instruction : treatment.deferred()) {
- builder.add(instruction);
+ switch (egressTagCriterion.type()) {
+ case VLAN_VID:
+ VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) egressTagCriterion;
+ builder.setVlanId(vlanIdCriterion.vlanId());
+ break;
+ case MPLS_LABEL:
+ MplsCriterion mplsCriterion = (MplsCriterion) egressTagCriterion;
+ builder.setMpls(mplsCriterion.label());
+ break;
+ case TUNNEL_ID:
+ TunnelIdCriterion tunnelIdCriterion = (TunnelIdCriterion) egressTagCriterion;
+ builder.setTunnelId(tunnelIdCriterion.tunnelId());
+ break;
+ default:
+ break;
}
- builder.immediate();
- for (Instruction instruction : treatment.immediate()) {
- builder.add(instruction);
- }
- return builder;
- }
- /**
- * Update the original builder with the necessary operations
- * to have a correct forwarding given an ingress selector.
- * TODO
- * This means that if the ingress selectors match on different vlanids and
- * the egress treatment rewrite the vlanid the forwarding works
- * but if we need to push for example an mpls label at the egress
- * we need to implement properly this method.
- *
- * @param treatmentBuilder the builder to modify
- * @param intentSelector the intent selector to use as input
- * @return the new treatment created
- */
- private TrafficTreatment.Builder updateBuilder(TrafficTreatment.Builder treatmentBuilder,
- TrafficSelector intentSelector) {
- return treatmentBuilder;
+ return builder.build();
}
/**
@@ -467,46 +577,45 @@
}
/**
- * Computes the new traffic selector using the
- * forwarding instructions.
+ * Update selector builder by using treatment.
*
- * @param fwInstructions it encapsulates the instructions to compute the new selector
- * @return the traffic selector builder
+ * @param builder builder to update
+ * @param treatment traffic treatment
*/
- private TrafficSelector.Builder createSelectorFromFwdInstructions(ForwardingInstructions fwInstructions) {
- TrafficSelector.Builder defaultSelectorBuilder = DefaultTrafficSelector.builder(fwInstructions.selector());
- fwInstructions.treatment().allInstructions().forEach(instruction -> {
- switch (instruction.type()) {
- case L0MODIFICATION:
- updateBuilder(defaultSelectorBuilder, (L0ModificationInstruction) instruction);
- break;
- case L1MODIFICATION:
- updateBuilder(defaultSelectorBuilder, (L1ModificationInstruction) instruction);
- break;
- case L2MODIFICATION:
- updateBuilder(defaultSelectorBuilder, (L2ModificationInstruction) instruction);
- break;
- case L3MODIFICATION:
- updateBuilder(defaultSelectorBuilder, (L3ModificationInstruction) instruction);
- break;
- case L4MODIFICATION:
- updateBuilder(defaultSelectorBuilder, (L4ModificationInstruction) instruction);
- break;
- case NOACTION:
- case OUTPUT:
- case GROUP:
- case QUEUE:
- case TABLE:
- case METER:
- case METADATA:
- case EXTENSION: // TODO is extension no-op or unsupported?
- // Nothing to do
- break;
- default:
- throw new IntentCompilationException("Unknown instruction type");
- }
- });
- return defaultSelectorBuilder;
+ private void updateBuilder(TrafficSelector.Builder builder, TrafficTreatment treatment) {
+
+ treatment.allInstructions().forEach(instruction -> {
+ switch (instruction.type()) {
+ case L0MODIFICATION:
+ updateBuilder(builder, (L0ModificationInstruction) instruction);
+ break;
+ case L1MODIFICATION:
+ updateBuilder(builder, (L1ModificationInstruction) instruction);
+ break;
+ case L2MODIFICATION:
+ updateBuilder(builder, (L2ModificationInstruction) instruction);
+ break;
+ case L3MODIFICATION:
+ updateBuilder(builder, (L3ModificationInstruction) instruction);
+ break;
+ case L4MODIFICATION:
+ updateBuilder(builder, (L4ModificationInstruction) instruction);
+ break;
+ case NOACTION:
+ case OUTPUT:
+ case GROUP:
+ case QUEUE:
+ case TABLE:
+ case METER:
+ case METADATA:
+ case EXTENSION: // TODO is extension no-op or unsupported?
+ // Nothing to do
+ break;
+ default:
+ throw new IntentCompilationException("Unknown instruction type");
+ }
+ });
+
}
}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompiler.java
index 00ed9b2..0ee6e83 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompiler.java
@@ -119,12 +119,10 @@
Intent result = LinkCollectionIntent.builder()
.appId(intent.appId())
- .selector(intent.selector())
.treatment(intent.treatment())
.links(Sets.newHashSet(links.values()))
- .ingressPoints(intent.ingressPoints())
- .egressPoints(ImmutableSet.of(intent.egressPoint()))
- .ingressSelectors(intent.ingressSelectors())
+ .filteredIngressPoints(intent.filteredIngressPoints())
+ .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
.priority(intent.priority())
.constraints(intent.constraints())
.build();
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompiler.java
index 76640f2..dcfc6d0 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompiler.java
@@ -69,15 +69,13 @@
Intent result = LinkCollectionIntent.builder()
.appId(intent.appId())
.key(intent.key())
- .selector(intent.selector())
.treatment(intent.treatment())
.links(links)
- .ingressPoints(ImmutableSet.of(intent.ingressPoint()))
- .egressPoints(intent.egressPoints())
+ .filteredIngressPoints(ImmutableSet.of(intent.filteredIngressPoint()))
+ .filteredEgressPoints(intent.filteredEgressPoints())
.priority(intent.priority())
.applyTreatmentOnEgress(true)
.constraints(intent.constraints())
- .egressTreatments(intent.egressTreatments())
.build();
return Collections.singletonList(result);
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
index ae39fdd..e3b662a 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompilerTest.java
@@ -16,10 +16,11 @@
package org.onosproject.net.intent.impl.compiler;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Maps;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.onosproject.TestApplicationId;
import org.onosproject.cfg.ComponentConfigAdapter;
@@ -28,14 +29,15 @@
import org.onosproject.core.IdGenerator;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultLink;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.FilteredConnectPoint;
import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
-import org.onosproject.net.flow.criteria.Criteria;
-import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.intent.FlowRuleIntent;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentExtensionService;
@@ -45,20 +47,19 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.onosproject.net.Link.Type.DIRECT;
-import static org.onosproject.net.NetTestTools.APP_ID;
-import static org.onosproject.net.NetTestTools.PID;
-import static org.onosproject.net.NetTestTools.connectPoint;
+import static org.onosproject.net.NetTestTools.*;
public class LinkCollectionIntentCompilerTest {
@@ -67,87 +68,66 @@
private final ConnectPoint d1p1 = connectPoint("s1", 1);
private final ConnectPoint d2p0 = connectPoint("s2", 0);
private final ConnectPoint d2p1 = connectPoint("s2", 1);
- private final ConnectPoint d2p2 = connectPoint("s2", 2);
- private final ConnectPoint d2p3 = connectPoint("s2", 3);
private final ConnectPoint d3p1 = connectPoint("s3", 1);
- private final ConnectPoint d3p2 = connectPoint("s3", 9);
private final ConnectPoint d3p0 = connectPoint("s3", 10);
private final ConnectPoint d1p0 = connectPoint("s1", 10);
- private final ConnectPoint d4p1 = connectPoint("s4", 1);
- private final ConnectPoint d4p0 = connectPoint("s4", 10);
+ private final DeviceId of1Id = DeviceId.deviceId("of:of1");
+ private final DeviceId of2Id = DeviceId.deviceId("of:of2");
+ private final DeviceId of3Id = DeviceId.deviceId("of:of3");
+ private final DeviceId of4Id = DeviceId.deviceId("of:of4");
+
+ private final ConnectPoint of1p1 = connectPoint("of1", 1);
+ private final ConnectPoint of1p2 = connectPoint("of1", 2);
+ private final ConnectPoint of2p1 = connectPoint("of2", 1);
+ private final ConnectPoint of2p2 = connectPoint("of2", 2);
+ private final ConnectPoint of2p3 = connectPoint("of2", 3);
+ private final ConnectPoint of3p1 = connectPoint("of3", 1);
+ private final ConnectPoint of3p2 = connectPoint("of3", 2);
+ private final ConnectPoint of4p1 = connectPoint("of4", 1);
+ private final ConnectPoint of4p2 = connectPoint("of4", 2);
private final Set<Link> links = ImmutableSet.of(
DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build());
- private final Set<Link> linksMultiple = ImmutableSet.of(
- DefaultLink.builder().providerId(PID).src(d3p1).dst(d2p0).type(DIRECT).build());
-
- private final Set<Link> linksMultiple2 = ImmutableSet.of(
- DefaultLink.builder().providerId(PID).src(d2p0).dst(d1p1).type(DIRECT).build(),
- DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
- DefaultLink.builder().providerId(PID).src(d2p2).dst(d4p1).type(DIRECT).build());
-
private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
- private final VlanId ingressVlan1 = VlanId.vlanId("10");
- private final TrafficSelector selectorVlan1 = DefaultTrafficSelector
- .builder()
- .matchVlanId(ingressVlan1)
+ private final TrafficSelector vlan100Selector = DefaultTrafficSelector.builder()
+ .matchVlanId(VlanId.vlanId("100"))
.build();
- private final VlanId ingressVlan2 = VlanId.vlanId("20");
- private final TrafficSelector selectorVlan2 = DefaultTrafficSelector
- .builder()
- .matchVlanId(ingressVlan2)
+ private final TrafficSelector vlan200Selector = DefaultTrafficSelector.builder()
+ .matchVlanId(VlanId.vlanId("200"))
.build();
- private final VlanId egressVlan = VlanId.vlanId("666");
- private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment
- .builder()
- .setVlanId(egressVlan)
+ private final TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
+ .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
.build();
-private final VlanId ingressVlan = VlanId.vlanId("10");
- private final TrafficSelector vlanSelector = DefaultTrafficSelector
- .builder()
- .matchVlanId(ingressVlan)
+ private final TrafficTreatment ethDstTreatment = DefaultTrafficTreatment.builder()
+ .setEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
.build();
- private final VlanId egressVlan1 = VlanId.vlanId("20");
- private final TrafficTreatment vlanTreatment1 = DefaultTrafficTreatment
- .builder()
- .setVlanId(egressVlan1)
- .build();
-
- private final VlanId egressVlan2 = VlanId.vlanId("666");
- private final TrafficTreatment vlanTreatment2 = DefaultTrafficTreatment
- .builder()
- .setVlanId(egressVlan2)
- .build();
-
- private final VlanId egressVlan3 = VlanId.vlanId("69");
- private final TrafficTreatment vlanTreatment3 = DefaultTrafficTreatment
- .builder()
- .setVlanId(egressVlan3)
- .build();
-
-
private CoreService coreService;
private IntentExtensionService intentExtensionService;
private IntentConfigurableRegistrator registrator;
private IdGenerator idGenerator = new MockIdGenerator();
private LinkCollectionIntent intent;
- private LinkCollectionIntent intentMultipleSelectors;
- private LinkCollectionIntent intentMultipleTreatments;
- private LinkCollectionIntent intentMultipleTreatments2;
private LinkCollectionIntentCompiler sut;
+
+
+ private List<FlowRule> getFlowRulesByDevice(DeviceId deviceId, Collection<FlowRule> flowRules) {
+ return flowRules.stream()
+ .filter(fr -> fr.deviceId().equals(deviceId))
+ .collect(Collectors.toList());
+ }
+
@Before
public void setUp() {
sut = new LinkCollectionIntentCompiler();
@@ -166,32 +146,6 @@
.ingressPoints(ImmutableSet.of(d1p1))
.egressPoints(ImmutableSet.of(d3p1))
.build();
- intentMultipleSelectors = LinkCollectionIntent.builder()
- .appId(APP_ID)
- .treatment(vlanTreatment)
- .links(linksMultiple)
- .ingressPoints(ImmutableSet.of(d3p0, d3p2))
- .egressPoints(ImmutableSet.of(d2p1))
- .ingressSelectors(this.createIngressSelectors())
- .build();
- intentMultipleTreatments = LinkCollectionIntent.builder()
- .appId(APP_ID)
- .selector(vlanSelector)
- .links(linksMultiple)
- .ingressPoints(ImmutableSet.of(d3p0))
- .egressPoints(ImmutableSet.of(d2p1, d2p2))
- .egressTreatments(this.createEgressTreatments())
- .applyTreatmentOnEgress(true)
- .build();
- intentMultipleTreatments2 = LinkCollectionIntent.builder()
- .appId(APP_ID)
- .selector(vlanSelector)
- .links(linksMultiple2)
- .ingressPoints(ImmutableSet.of(d2p3))
- .egressPoints(ImmutableSet.of(d1p0, d3p0, d4p0))
- .egressTreatments(this.createEgressTreatments2())
- .applyTreatmentOnEgress(true)
- .build();
intentExtensionService = createMock(IntentExtensionService.class);
intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
@@ -205,10 +159,13 @@
sut.registrator = registrator;
replay(coreService, intentExtensionService);
+
+
}
@After
public void tearDown() {
+
Intent.unbindIdGenerator(idGenerator);
}
@@ -262,299 +219,486 @@
sut.deactivate();
}
-@Test
- public void testCompileMultipleSelectors() {
+ /**
+ * Single point to multi point case.
+ * -1 of1 2-1 of2 2--1 of3 2-
+ * 3
+ * `-1 of4 2-
+ */
+ @Test
+ public void testFilteredConnectPoint1() {
sut.activate();
+ Set<Link> testLinks = ImmutableSet.of(
+ DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
+ );
- List<Intent> compiled = sut.compile(intentMultipleSelectors, Collections.emptyList());
- assertThat(compiled, hasSize(1));
+ TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .setOutput(PortNumber.portNumber(3))
+ .build();
+
+ TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
+ .setVlanId(VlanId.vlanId("200"))
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
- assertThat(rules, hasSize((linksMultiple.size()) + intentMultipleSelectors.ingressPoints().size()));
- Set<FlowRule> d3Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d3Rules, hasSize(intentMultipleSelectors.ingressPoints().size()));
+ Set<FilteredConnectPoint> ingress = ImmutableSet.of(
+ new FilteredConnectPoint(of1p1, vlan100Selector)
+ );
- FlowRule rule1 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId())
- &&
- rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p0.port())))
- .findFirst()
- .get();
- assertThat(rule1.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleSelectors.selector())
- .matchInPort(d3p0.port())
- .matchVlanId(ingressVlan1)
- .build()
- ));
- assertThat(rule1.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleSelectors.treatment())
- .setOutput(d3p1.port())
- .build()
- ));
- assertThat(rule1.priority(), is(intentMultipleSelectors.priority()));
+ Set<FilteredConnectPoint> egress = ImmutableSet.of(
+ new FilteredConnectPoint(of3p2, vlan100Selector),
+ new FilteredConnectPoint(of4p2, vlan200Selector)
+ );
- FlowRule rule2 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId())
- &&
- rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p2.port())))
- .findFirst()
- .get();
- assertThat(rule2.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleSelectors.selector())
- .matchInPort(d3p2.port())
- .matchVlanId(ingressVlan2)
- .build()
- ));
- assertThat(rule2.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleSelectors.treatment())
- .setOutput(d3p1.port())
- .build()
- ));
- assertThat(rule2.priority(), is(intentMultipleSelectors.priority()));
+ intent = LinkCollectionIntent.builder()
+ .appId(APP_ID)
+ .filteredIngressPoints(ingress)
+ .filteredEgressPoints(egress)
+ .treatment(treatment)
+ .applyTreatmentOnEgress(true)
+ .links(testLinks)
+ .build();
- Set<FlowRule> d2Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d2Rules, hasSize(intentMultipleSelectors.egressPoints().size()));
+ assertThat(sut, is(notNullValue()));
- // We do not need in_port filter
- FlowRule rule3 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule3.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleSelectors.selector())
- .matchInPort(d2p0.port())
- .matchVlanId(egressVlan)
- .build()
- ));
- assertThat(rule3.treatment(), is(
- DefaultTrafficTreatment
- .builder()
- .setOutput(d2p1.port())
- .build()
- ));
- assertThat(rule3.priority(), is(intentMultipleSelectors.priority()));
+ List<Intent> result = sut.compile(intent, Collections.emptyList());
+
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
+
+ if (resultIntent instanceof FlowRuleIntent) {
+ FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
+
+ assertThat(frIntent.flowRules(), hasSize(4));
+
+ List<FlowRule> deviceFlowRules;
+ FlowRule flowRule;
+
+ // Of1
+ deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf1Selector));
+ assertThat(flowRule.treatment(), is(expectOf1Treatment));
+
+ // Of2
+ deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf2Selector));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+
+ // Of3
+ deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf3Selector));
+ assertThat(flowRule.treatment(), is(expectOf3Treatment));
+
+ // Of4
+ deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf4Selector));
+ assertThat(flowRule.treatment(), is(expectOf4Treatment));
+
+ }
+ sut.deactivate();
+ }
+
+ /**
+ * Multi point to single point intent with filtered connect point.
+ *
+ * -1 of1 2-1 of2 2-1 of4 2-
+ * 3
+ * -1 of3 2---/
+ */
+ @Test
+ public void testFilteredConnectPoint2() {
+ sut.activate();
+ Set<Link> testlinks = ImmutableSet.of(
+ DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
+ );
+
+ Set<FilteredConnectPoint> ingress = ImmutableSet.of(
+ new FilteredConnectPoint(of1p1, vlan100Selector),
+ new FilteredConnectPoint(of3p1, vlan100Selector)
+ );
+
+ Set<FilteredConnectPoint> egress = ImmutableSet.of(
+ new FilteredConnectPoint(of4p2, vlan200Selector)
+ );
+
+ TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
+ .setVlanId(VlanId.vlanId("200"))
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(vlan200Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(vlan200Selector)
+ .matchInPort(PortNumber.portNumber(3))
+ .build();
+
+ TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
+ .setVlanId(VlanId.vlanId("200"))
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
+ .matchInPort(PortNumber.portNumber(1))
+ .matchVlanId(VlanId.vlanId("200"))
+ .build();
+
+ TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+ intent = LinkCollectionIntent.builder()
+ .appId(APP_ID)
+ .filteredIngressPoints(ingress)
+ .filteredEgressPoints(egress)
+ .treatment(treatment)
+ .links(testlinks)
+ .build();
+
+ List<Intent> result = sut.compile(intent, Collections.emptyList());
+
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
+
+ if (resultIntent instanceof FlowRuleIntent) {
+ FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
+ assertThat(frIntent.flowRules(), hasSize(5));
+
+ List<FlowRule> deviceFlowRules;
+ FlowRule flowRule;
+
+ // Of1
+ deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf1Selector));
+ assertThat(flowRule.treatment(), is(expectOf1Treatment));
+
+ // Of2 (has 2 flows)
+ deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(2));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf2Selector1));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+ flowRule = deviceFlowRules.get(1);
+ assertThat(flowRule.selector(), is(expectOf2Selector2));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+
+ // Of3
+ deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf3Selector));
+ assertThat(flowRule.treatment(), is(expectOf3Treatment));
+
+ // Of4
+ deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf4Selector));
+ assertThat(flowRule.treatment(), is(expectOf4Treatment));
+ }
sut.deactivate();
}
+ /**
+ * Single point to multi point without filtered connect point case.
+ * -1 of1 2-1 of2 2--1 of3 2-
+ * 3
+ * `-1 of4 2-
+ */
@Test
- public void testCompileMultipleTreatments() {
+ public void nonTrivialTranslation1() {
sut.activate();
+ Set<Link> testLinks = ImmutableSet.of(
+ DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
+ );
- List<Intent> compiled = sut.compile(intentMultipleTreatments, Collections.emptyList());
- assertThat(compiled, hasSize(1));
+ TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
- Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
- assertThat(rules, hasSize(2));
+ TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Set<FlowRule> d3Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d3Rules, hasSize(intentMultipleTreatments.ingressPoints().size()));
+ TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
- FlowRule rule1 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule1.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments.selector())
- .matchInPort(d3p0.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule1.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments.treatment())
- .setOutput(d3p1.port())
- .build()
- ));
- assertThat(rule1.priority(), is(intentMultipleTreatments.priority()));
+ TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .setOutput(PortNumber.portNumber(3))
+ .build();
- Set<FlowRule> d2Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d2Rules, hasSize(1));
+ TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
- FlowRule rule2 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule2.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments.selector())
- .matchInPort(d2p0.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule2.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments.treatment())
- .setVlanId(egressVlan1)
- .setOutput(d2p1.port())
- .setVlanId(egressVlan2)
- .setOutput(d2p2.port())
- .build()
- ));
- assertThat(rule2.priority(), is(intentMultipleTreatments.priority()));
+ TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+ TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+
+ TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
+ .setOutput(PortNumber.portNumber(2))
+ .build();
+
+
+
+ Set<ConnectPoint> ingress = ImmutableSet.of(
+ of1p1
+ );
+
+ Set<ConnectPoint> egress = ImmutableSet.of(
+ of3p2,
+ of4p2
+ );
+
+ intent = LinkCollectionIntent.builder()
+ .appId(APP_ID)
+ .selector(ipPrefixSelector)
+ .treatment(ethDstTreatment)
+ .ingressPoints(ingress)
+ .egressPoints(egress)
+ .applyTreatmentOnEgress(true)
+ .links(testLinks)
+ .build();
+
+ assertThat(sut, is(notNullValue()));
+
+ List<Intent> result = sut.compile(intent, Collections.emptyList());
+
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
+
+ if (resultIntent instanceof FlowRuleIntent) {
+ FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
+
+ assertThat(frIntent.flowRules(), hasSize(4));
+
+ List<FlowRule> deviceFlowRules;
+ FlowRule flowRule;
+
+ // Of1
+ deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf1Selector));
+ assertThat(flowRule.treatment(), is(expectOf1Treatment));
+
+ // Of2
+ deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf2Selector));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+
+ // Of3
+ deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf3Selector));
+ assertThat(flowRule.treatment(), is(expectOf3Treatment));
+
+ // Of4
+ deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf4Selector));
+ assertThat(flowRule.treatment(), is(expectOf4Treatment));
+
+ }
+ sut.deactivate();
}
+ /**
+ * Multi point to single point intent without filtered connect point.
+ *
+ * -1 of1 2-1 of2 2-1 of4 2-
+ * 3
+ * -1 of3 2---/
+ */
@Test
- public void testCompileMultipleTreatments2() {
+ public void nonTrivialTranslation2() {
sut.activate();
+ Set<Link> testlinks = ImmutableSet.of(
+ DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
+ DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
+ );
- List<Intent> compiled = sut.compile(intentMultipleTreatments2, Collections.emptyList());
- assertThat(compiled, hasSize(1));
+ Set<ConnectPoint> ingress = ImmutableSet.of(
+ of1p1,
+ of3p1
+ );
+ Set<ConnectPoint> egress = ImmutableSet.of(
+ of4p2
+ );
- Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
- assertThat(rules, hasSize(4));
+ TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
+ TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Set<FlowRule> d2Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d2Rules, hasSize(1));
+ TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
+ .build();
+ TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
+ .matchInPort(PortNumber.portNumber(3))
+ .build();
- FlowRule rule1 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule1.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments.selector())
- .matchInPort(d2p3.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule1.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments.treatment())
- .setOutput(d2p0.port())
- .setOutput(d2p1.port())
- .setOutput(d2p2.port())
- .build()
- ));
- assertThat(rule1.priority(), is(intentMultipleTreatments.priority()));
+ TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Set<FlowRule> d1Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d1Rules, hasSize(1));
+ TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
- FlowRule rule2 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule2.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments2.selector())
- .matchInPort(d1p1.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule2.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments2.treatment())
- .setVlanId(egressVlan1)
- .setOutput(d1p0.port())
- .build()
- ));
- assertThat(rule2.priority(), is(intentMultipleTreatments.priority()));
+ TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Set<FlowRule> d3Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d3Rules, hasSize(1));
+ TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
+ .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
+ .matchInPort(PortNumber.portNumber(1))
+ .build();
- FlowRule rule3 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule3.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments2.selector())
- .matchInPort(d3p1.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule3.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments2.treatment())
- .setVlanId(egressVlan2)
- .setOutput(d3p0.port())
- .build()
- ));
- assertThat(rule3.priority(), is(intentMultipleTreatments.priority()));
+ TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
+ .setOutput(PortNumber.portNumber(2))
+ .build();
- Set<FlowRule> d4Rules = rules
- .parallelStream()
- .filter(rule -> rule.deviceId().equals(d4p0.deviceId()))
- .collect(Collectors.toSet());
- assertThat(d4Rules, hasSize(1));
+ intent = LinkCollectionIntent.builder()
+ .appId(APP_ID)
+ .selector(ipPrefixSelector)
+ .ingressPoints(ingress)
+ .egressPoints(egress)
+ .treatment(ethDstTreatment)
+ .links(testlinks)
+ .build();
- FlowRule rule4 = rules.stream()
- .filter(rule -> rule.deviceId().equals(d4p0.deviceId()))
- .findFirst()
- .get();
- assertThat(rule4.selector(), is(
- DefaultTrafficSelector
- .builder(intentMultipleTreatments2.selector())
- .matchInPort(d4p1.port())
- .matchVlanId(ingressVlan)
- .build()
- ));
- assertThat(rule4.treatment(), is(
- DefaultTrafficTreatment
- .builder(intentMultipleTreatments2.treatment())
- .setVlanId(egressVlan3)
- .setOutput(d4p0.port())
- .build()
- ));
- assertThat(rule4.priority(), is(intentMultipleTreatments.priority()));
+ List<Intent> result = sut.compile(intent, Collections.emptyList());
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
+
+ if (resultIntent instanceof FlowRuleIntent) {
+ FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
+ assertThat(frIntent.flowRules(), hasSize(5));
+
+ List<FlowRule> deviceFlowRules;
+ FlowRule flowRule;
+
+ // Of1
+ deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf1Selector));
+ assertThat(flowRule.treatment(), is(expectOf1Treatment));
+
+ // Of2 (has 2 flows)
+ deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(2));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf2Selector1));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+ flowRule = deviceFlowRules.get(1);
+ assertThat(flowRule.selector(), is(expectOf2Selector2));
+ assertThat(flowRule.treatment(), is(expectOf2Treatment));
+
+ // Of3
+ deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf3Selector));
+ assertThat(flowRule.treatment(), is(expectOf3Treatment));
+
+ // Of4
+ deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
+ assertThat(deviceFlowRules, hasSize(1));
+ flowRule = deviceFlowRules.get(0);
+ assertThat(flowRule.selector(), is(expectOf4Selector));
+ assertThat(flowRule.treatment(), is(expectOf4Treatment));
+ }
+
+ sut.deactivate();
}
-
- public Map<ConnectPoint, TrafficTreatment> createEgressTreatments() {
- Map<ConnectPoint, TrafficTreatment> mapToReturn = Maps.newHashMap();
- mapToReturn.put(d2p1, vlanTreatment1);
- mapToReturn.put(d2p2, vlanTreatment2);
- return mapToReturn;
- }
-
- public Map<ConnectPoint, TrafficTreatment> createEgressTreatments2() {
- Map<ConnectPoint, TrafficTreatment> mapToReturn = Maps.newHashMap();
- mapToReturn.put(d1p0, vlanTreatment1);
- mapToReturn.put(d3p0, vlanTreatment2);
- mapToReturn.put(d4p0, vlanTreatment3);
- return mapToReturn;
- }
-
- public Map<ConnectPoint, TrafficSelector> createIngressSelectors() {
- Map<ConnectPoint, TrafficSelector> mapToReturn = Maps.newHashMap();
- mapToReturn.put(d3p0, selectorVlan1);
- mapToReturn.put(d3p2, selectorVlan2);
- return mapToReturn;
- }
-
}
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java
index 58daade..8d49138 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/MultiPointToSinglePointIntentCompilerTest.java
@@ -15,15 +15,15 @@
*/
package org.onosproject.net.intent.impl.compiler;
+import com.google.common.collect.ImmutableSet;
import org.hamcrest.Matchers;
import org.junit.Test;
+import org.onlab.packet.VlanId;
import org.onosproject.TestApplicationId;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.ElementId;
-import org.onosproject.net.Path;
-import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.FilteredConnectPoint;
+import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.intent.AbstractIntentTest;
@@ -31,7 +31,6 @@
import org.onosproject.net.intent.IntentTestsMocks;
import org.onosproject.net.intent.LinkCollectionIntent;
import org.onosproject.net.intent.MultiPointToSinglePointIntent;
-import org.onosproject.net.topology.PathServiceAdapter;
import java.util.HashSet;
import java.util.List;
@@ -43,7 +42,6 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.onosproject.net.NetTestTools.connectPoint;
-import static org.onosproject.net.NetTestTools.createPath;
import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
/**
@@ -57,47 +55,6 @@
private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
/**
- * Mock path service for creating paths within the test.
- */
- private static class MockPathService extends PathServiceAdapter {
-
- final String[] pathHops;
-
- /**
- * Constructor that provides a set of hops to mock.
- *
- * @param pathHops path hops to mock
- */
- MockPathService(String[] pathHops) {
- this.pathHops = pathHops;
- }
-
- @Override
- public Set<Path> getPaths(ElementId src, ElementId dst) {
- Set<Path> result = new HashSet<>();
-
- String[] allHops = new String[pathHops.length + 1];
- allHops[0] = src.toString();
- if (pathHops.length != 0) {
- System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
- }
- result.add(createPath(allHops));
-
- return result;
- }
- }
-
- /**
- * Mocks the device service so that a device appears available in the test.
- */
- private static class MockDeviceService extends DeviceServiceAdapter {
- @Override
- public boolean isAvailable(DeviceId deviceId) {
- return true;
- }
- }
-
- /**
* Creates a MultiPointToSinglePoint intent for a group of ingress points
* and an egress point.
*
@@ -123,6 +80,23 @@
}
/**
+ * Generate MultiPointToSinglePointIntent with filtered connection point.
+ *
+ * @param ingress filtered ingress points
+ * @param egress filtered egress point
+ * @return
+ */
+ private MultiPointToSinglePointIntent makeFilteredConnectPointIntent(Set<FilteredConnectPoint> ingress,
+ FilteredConnectPoint egress) {
+ return MultiPointToSinglePointIntent.builder()
+ .appId(APPID)
+ .treatment(treatment)
+ .filteredIngressPoints(ingress)
+ .filteredEgressPoint(egress)
+ .build();
+ }
+
+ /**
* Creates a compiler for MultiPointToSinglePoint intents.
*
* @param hops hops to use while computing paths for this intent
@@ -131,8 +105,8 @@
private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
MultiPointToSinglePointIntentCompiler compiler =
new MultiPointToSinglePointIntentCompiler();
- compiler.pathService = new MockPathService(hops);
- compiler.deviceService = new MockDeviceService();
+ compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
+ compiler.deviceService = new IntentTestsMocks.MockDeviceService();
return compiler;
}
@@ -148,8 +122,7 @@
MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
assertThat(intent, is(notNullValue()));
- String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8",
- egress};
+ String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
assertThat(compiler, is(notNullValue()));
@@ -184,7 +157,7 @@
MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
assertThat(intent, is(notNullValue()));
- final String[] hops = {"inner1", "inner2", egress};
+ final String[] hops = {"inner1", "inner2"};
MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
assertThat(compiler, is(notNullValue()));
@@ -217,7 +190,7 @@
MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
assertThat(intent, is(notNullValue()));
- final String[] hops = {"n1", egress};
+ final String[] hops = {"n1"};
MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
assertThat(compiler, is(notNullValue()));
@@ -245,12 +218,12 @@
@Test
public void testSameDeviceCompilation() {
String[] ingress = {"i1", "i2"};
- String egress = "i1";
+ String egress = "i3";
MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
assertThat(intent, is(notNullValue()));
- final String[] hops = {"i1", "i2"};
+ final String[] hops = {};
MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
assertThat(compiler, is(notNullValue()));
@@ -264,7 +237,48 @@
LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
assertThat(linkIntent.links(), hasSize(ingress.length));
- assertThat(linkIntent.links(), linksHasPath("i2", "i1"));
+ assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
+ assertThat(linkIntent.links(), linksHasPath("i2", "i3"));
}
}
+
+ /**
+ * Tests filtered ingress and egress.
+ */
+ @Test
+ public void testFilteredConnectPointIntent() {
+
+ Set<FilteredConnectPoint> ingress = ImmutableSet.of(
+ new FilteredConnectPoint(connectPoint("of1", 1),
+ DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
+ new FilteredConnectPoint(connectPoint("of2", 1),
+ DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
+ );
+
+ FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
+
+ MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress);
+ String[] hops = {"of3"};
+
+ MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(3));
+ assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
+ assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
+ assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
+ }
+
+ }
+
+
}
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompilerTest.java
new file mode 100644
index 0000000..6270f5c
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/compiler/SinglePointToMultiPointIntentCompilerTest.java
@@ -0,0 +1,293 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.intent.impl.compiler;
+
+import com.google.common.collect.ImmutableSet;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.FilteredConnectPoint;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.AbstractIntentTest;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.IntentTestsMocks;
+import org.onosproject.net.intent.LinkCollectionIntent;
+import org.onosproject.net.intent.SinglePointToMultiPointIntent;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.onosproject.net.NetTestTools.connectPoint;
+import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
+
+/**
+ * Unit tests for SinglePointToMultiPointIntentCompiler.
+ */
+public class SinglePointToMultiPointIntentCompilerTest extends AbstractIntentTest {
+
+ private static final ApplicationId APPID = new TestApplicationId("foo");
+
+ private TrafficSelector selector = new IntentTestsMocks.MockSelector();
+ private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
+
+
+
+ /**
+ * Creates a SinglePointToMultiPoint intent for an ingress point
+ * and a group of egress points.
+ *
+ * @param ingressId device id of the ingress point
+ * @param egressIds array of egress device ids
+ * @return MultiPointToSinglePoint intent
+ */
+ private SinglePointToMultiPointIntent makeIntent(String ingressId, String[] egressIds) {
+ ConnectPoint ingressPoint = connectPoint(ingressId, 2);
+ Set<ConnectPoint> egressPoints = new HashSet<>();
+
+
+ for (String egressId : egressIds) {
+ egressPoints.add(connectPoint(egressId, 1));
+ }
+
+ return SinglePointToMultiPointIntent.builder()
+ .appId(APPID)
+ .selector(selector)
+ .treatment(treatment)
+ .ingressPoint(ingressPoint)
+ .egressPoints(egressPoints)
+ .build();
+ }
+
+ /**
+ * Generate SinglePointToMultiPointIntent with filtered connection point.
+ *
+ * @param ingress filtered ingress point
+ * @param egress filtered egress point
+ * @return
+ */
+ private SinglePointToMultiPointIntent makeFilteredConnectPointIntent(FilteredConnectPoint ingress,
+ Set<FilteredConnectPoint> egress) {
+ return SinglePointToMultiPointIntent.builder()
+ .appId(APPID)
+ .treatment(treatment)
+ .filteredIngressPoint(ingress)
+ .filteredEgressPoints(egress)
+ .build();
+ }
+
+ /**
+ * Creates a compiler for SinglePointToMultiPoint intents.
+ *
+ * @param hops hops to use while computing paths for this intent
+ * @return SinglePointToMultiPoint intent
+ */
+ private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops) {
+ SinglePointToMultiPointIntentCompiler compiler =
+ new SinglePointToMultiPointIntentCompiler();
+
+ compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
+ return compiler;
+ }
+
+ /**
+ * Tests a single ingress point with 8 hops to its egress point.
+ */
+ @Test
+ public void testSingleLongPathCompilation() {
+
+ String ingress = "ingress";
+ String[] egress = {"egress"};
+
+ SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
+ assertThat(intent, is(notNullValue()));
+
+ String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
+ SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(Matchers.notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(9));
+ assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
+ assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
+ assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
+ assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
+ assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
+ assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
+ assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
+ }
+ }
+
+ /**
+ * Tests a simple topology where two egress points share some path segments
+ * and some path segments are not shared.
+ */
+ @Test
+ public void testTwoIngressCompilation() {
+ String ingress = "ingress";
+ String[] egress = {"egress1", "egress2"};
+
+ SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
+ assertThat(intent, is(notNullValue()));
+
+ final String[] hops = {"inner1", "inner2"};
+ SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(4));
+ assertThat(linkIntent.links(), linksHasPath("ingress", "inner1"));
+ assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
+ assertThat(linkIntent.links(), linksHasPath("inner2", "egress1"));
+ assertThat(linkIntent.links(), linksHasPath("inner2", "egress2"));
+ }
+ }
+
+ /**
+ * Tests a large number of ingress points that share a common path to the
+ * egress point.
+ */
+ @Test
+ public void testMultiIngressCompilation() {
+ String ingress = "i";
+ String[] egress = {"e1", "e2", "e3", "e4", "e5",
+ "e6", "e7", "e8", "e9", "e10"};
+
+ SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
+ assertThat(intent, is(notNullValue()));
+
+ final String[] hops = {"n1"};
+ SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(egress.length + 1));
+ for (String egressToCheck : egress) {
+ assertThat(linkIntent.links(), linksHasPath("n1", egressToCheck));
+ }
+ assertThat(linkIntent.links(), linksHasPath(ingress, "n1"));
+ }
+ }
+
+ /**
+ * Tests ingress and egress on the same device.
+ */
+ @Test
+ public void testSameDeviceCompilation() {
+ String ingress = "i1";
+ String[] egress = {"i2", "i3"};
+
+ SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
+ assertThat(intent, is(notNullValue()));
+
+ final String[] hops = {};
+ SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(egress.length));
+
+ assertThat(linkIntent.links(), linksHasPath("i1", "i2"));
+ assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
+ }
+ }
+
+ /**
+ * Tests filtered ingress and egress.
+ */
+ @Test
+ public void testFilteredConnectPointIntent() {
+
+ FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
+
+ Set<FilteredConnectPoint> egress = ImmutableSet.of(
+ new FilteredConnectPoint(connectPoint("of3", 1),
+ DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
+ new FilteredConnectPoint(connectPoint("of4", 1),
+ DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
+ );
+
+
+ SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress);
+ String[] hops = {"of2"};
+
+ SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null);
+ assertThat(result, is(notNullValue()));
+ assertThat(result, hasSize(1));
+
+ Intent resultIntent = result.get(0);
+ assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
+
+ if (resultIntent instanceof LinkCollectionIntent) {
+ LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
+ assertThat(linkIntent.links(), hasSize(3));
+
+ assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
+ assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
+ assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
+
+ Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
+ assertThat("Link collection ingress points do not match base intent",
+ ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
+
+ assertThat("Link collection egress points do not match base intent",
+ linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
+ }
+
+ }
+}