ONOS-631 #Initial MPLS intent implementation
Change-Id: I6f906b953f06f395cc67e612648802e333c0e581
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/MplsIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/MplsIntentCompiler.java
new file mode 100644
index 0000000..b8796d97
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/MplsIntentCompiler.java
@@ -0,0 +1,85 @@
+package org.onosproject.net.intent.impl;
+
+import static java.util.Arrays.asList;
+import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultPath;
+import org.onosproject.net.Link;
+import org.onosproject.net.Path;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.MplsIntent;
+import org.onosproject.net.intent.MplsPathIntent;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.net.resource.LinkResourceAllocations;
+
+
+@Component(immediate = true)
+public class MplsIntentCompiler extends ConnectivityIntentCompiler<MplsIntent> {
+
+ // TODO: use off-the-shell core provider ID
+ private static final ProviderId PID =
+ new ProviderId("core", "org.onosproject.core", true);
+ // TODO: consider whether the default cost is appropriate or not
+ public static final int DEFAULT_COST = 1;
+
+
+ @Activate
+ public void activate() {
+ intentManager.registerCompiler(MplsIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterCompiler(MplsIntent.class);
+ }
+
+ @Override
+ public List<Intent> compile(MplsIntent intent, List<Intent> installable,
+ Set<LinkResourceAllocations> resources) {
+ ConnectPoint ingressPoint = intent.ingressPoint();
+ ConnectPoint egressPoint = intent.egressPoint();
+
+ if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
+ List<Link> links = asList(createEdgeLink(ingressPoint, true), createEdgeLink(egressPoint, false));
+ return asList(createPathIntent(new DefaultPath(PID, links, DEFAULT_COST), intent));
+ }
+
+ List<Link> links = new ArrayList<>();
+ Path path = getPath(intent, ingressPoint.deviceId(),
+ egressPoint.deviceId());
+
+ links.add(createEdgeLink(ingressPoint, true));
+ links.addAll(path.links());
+
+ links.add(createEdgeLink(egressPoint, false));
+
+ return asList(createPathIntent(new DefaultPath(PID, links, path.cost(),
+ path.annotations()), intent));
+ }
+
+ /**
+ * Creates a path intent from the specified path and original
+ * connectivity intent.
+ *
+ * @param path path to create an intent for
+ * @param intent original intent
+ */
+ private Intent createPathIntent(Path path,
+ MplsIntent intent) {
+ return new MplsPathIntent(intent.appId(),
+ intent.selector(), intent.treatment(), path,
+ intent.ingressLabel(), intent.egressLabel(),
+ intent.constraints());
+ }
+
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/MplsPathIntentInstaller.java b/core/net/src/main/java/org/onosproject/net/intent/impl/MplsPathIntentInstaller.java
new file mode 100644
index 0000000..09c6551
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/MplsPathIntentInstaller.java
@@ -0,0 +1,296 @@
+package org.onosproject.net.intent.impl;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.Ethernet;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleBatchEntry;
+import org.onosproject.net.flow.FlowRuleBatchOperation;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
+import org.onosproject.net.flow.criteria.Criteria.EthTypeCriterion;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.Criterion.Type;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.MplsPathIntent;
+import org.onosproject.net.link.LinkStore;
+import org.onosproject.net.resource.DefaultLinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceAllocations;
+import org.onosproject.net.resource.LinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceService;
+import org.onosproject.net.resource.MplsLabel;
+import org.onosproject.net.resource.MplsLabelResourceAllocation;
+import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceType;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+/**
+ * Installer for {@link MplsPathIntent packet path connectivity intents}.
+ */
+@Component(immediate = true)
+public class MplsPathIntentInstaller implements IntentInstaller<MplsPathIntent> {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkResourceService resourceService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkStore linkStore;
+
+ protected ApplicationId appId;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication("org.onosproject.net.intent");
+ intentManager.registerInstaller(MplsPathIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterInstaller(MplsPathIntent.class);
+ }
+
+ @Override
+ public List<FlowRuleBatchOperation> install(MplsPathIntent intent) {
+ LinkResourceAllocations allocations = assignMplsLabel(intent);
+ return generateRules(intent, allocations, FlowRuleOperation.ADD);
+
+ }
+
+ @Override
+ public List<FlowRuleBatchOperation> uninstall(MplsPathIntent intent) {
+ LinkResourceAllocations allocations = resourceService
+ .getAllocations(intent.id());
+ resourceService.releaseResources(allocations);
+
+ List<FlowRuleBatchOperation> rules = generateRules(intent,
+ allocations,
+ FlowRuleOperation.REMOVE);
+ return rules;
+ }
+
+ @Override
+ public List<FlowRuleBatchOperation> replace(MplsPathIntent oldIntent,
+ MplsPathIntent newIntent) {
+
+ List<FlowRuleBatchOperation> batches = Lists.newArrayList();
+ batches.addAll(uninstall(oldIntent));
+ batches.addAll(install(newIntent));
+ return batches;
+ }
+
+ private LinkResourceAllocations assignMplsLabel(MplsPathIntent intent) {
+
+ // TODO: do it better... Suggestions?
+ Set<Link> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
+ .links().size() - 2);
+ for (int i = 1; i <= intent.path().links().size() - 2; i++) {
+ Link link = intent.path().links().get(i);
+ linkRequest.add(link);
+ // add the inverse link. I want that the label is reserved both for
+ // the direct and inverse link
+ linkRequest.add(linkStore.getLink(link.dst(), link.src()));
+ }
+
+ LinkResourceRequest.Builder request = DefaultLinkResourceRequest
+ .builder(intent.id(), linkRequest).addMplsRequest();
+ LinkResourceAllocations reqMpls = resourceService
+ .requestResources(request.build());
+ return reqMpls;
+ }
+
+ private MplsLabel getMplsLabel(LinkResourceAllocations allocations,
+ Link link) {
+
+ for (ResourceAllocation allocation : allocations
+ .getResourceAllocation(link)) {
+ if (allocation.type() == ResourceType.MPLS_LABEL) {
+ return ((MplsLabelResourceAllocation) allocation).mplsLabel();
+
+ }
+ }
+ log.warn("MPLS label was not assigned successfully");
+ return null;
+ }
+
+ private List<FlowRuleBatchOperation> generateRules(MplsPathIntent intent,
+ LinkResourceAllocations allocations,
+ FlowRuleOperation operation) {
+
+ Iterator<Link> links = intent.path().links().iterator();
+ Link srcLink = links.next();
+ ConnectPoint prev = srcLink.dst();
+
+ Link link = links.next();
+ // List of flow rules to be installed
+ List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
+
+ // Ingress traffic
+ // Get the new MPLS label
+ MplsLabel mpls = getMplsLabel(allocations, link);
+ checkNotNull(mpls);
+ MplsLabel prevLabel = mpls;
+ rules.add(ingressFlow(prev.port(), link, intent, mpls, operation));
+
+ prev = link.dst();
+
+ while (links.hasNext()) {
+
+ link = links.next();
+
+ if (links.hasNext()) {
+ // Transit traffic
+ // Get the new MPLS label
+ mpls = getMplsLabel(allocations, link);
+ checkNotNull(mpls);
+ rules.add(transitFlow(prev.port(), link, intent,
+ prevLabel, mpls, operation));
+ prevLabel = mpls;
+
+ } else {
+ // Egress traffic
+ rules.add(egressFlow(prev.port(), link, intent,
+ prevLabel, operation));
+ }
+
+ prev = link.dst();
+ }
+ return Lists.newArrayList(new FlowRuleBatchOperation(rules, null, 0));
+ }
+
+ private FlowRuleBatchEntry ingressFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel label,
+ FlowRuleOperation operation) {
+
+ TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
+ .builder(intent.selector());
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
+ ingressSelector.matchInPort(inPort);
+
+ if (intent.ingressLabel().isPresent()) {
+ ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(intent.ingressLabel().get());
+
+ // Swap the MPLS label
+ treat.setMpls(label.label());
+ } else {
+ // Push and set the MPLS label
+ treat.pushMpls().setMpls(label.label());
+ }
+ // Add the output action
+ treat.setOutput(link.src().port());
+
+ return flowRuleBatchEntry(intent, link.src().deviceId(),
+ ingressSelector.build(), treat.build(),
+ operation);
+ }
+
+ private FlowRuleBatchEntry transitFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel prevLabel,
+ MplsLabel outLabel,
+ FlowRuleOperation operation) {
+
+ // Ignore the ingress Traffic Selector and use only the MPLS label
+ // assigned in the previous link
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(prevLabel.label());
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
+
+ // Set the new label only if the label on the packet is
+ // different
+ if (prevLabel.equals(outLabel)) {
+ treat.setMpls(outLabel.label());
+ }
+
+ treat.setOutput(link.src().port());
+ return flowRuleBatchEntry(intent, link.src().deviceId(),
+ selector.build(), treat.build(), operation);
+ }
+
+ private FlowRuleBatchEntry egressFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel prevLabel,
+ FlowRuleOperation operation) {
+ // egress point: either set the egress MPLS label or pop the
+ // MPLS label based on the intent annotations
+
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(prevLabel.label());
+
+ // apply the intent's treatments
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
+ .treatment());
+
+ if (intent.egressLabel().isPresent()) {
+ treat.setMpls(intent.egressLabel().get());
+ } else {
+ // if the ingress ethertype is defined, the egress traffic
+ // will be use that value, otherwise the IPv4 ethertype is used.
+ Criterion c = intent.selector().getCriterion(Type.ETH_TYPE);
+ if (c != null && c instanceof EthTypeCriterion) {
+ EthTypeCriterion ethertype = (EthTypeCriterion) c;
+ treat.popMpls((short) ethertype.ethType());
+ } else {
+ treat.popMpls(Ethernet.TYPE_IPV4);
+ }
+
+ }
+ treat.setOutput(link.src().port());
+ return flowRuleBatchEntry(intent, link.src().deviceId(),
+ selector.build(), treat.build(), operation);
+ }
+
+ protected FlowRuleBatchEntry flowRuleBatchEntry(MplsPathIntent intent,
+ DeviceId deviceId,
+ TrafficSelector selector,
+ TrafficTreatment treat,
+ FlowRuleOperation operation) {
+ FlowRule rule = new DefaultFlowRule(
+ deviceId,
+ selector,
+ treat,
+ 123, // FIXME 123
+ appId,
+ 0,
+ true);
+ return new FlowRuleBatchEntry(operation, rule, intent.id()
+ .fingerprint());
+
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java b/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
index f583ebd..fadb152 100644
--- a/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/resource/impl/LinkResourceManager.java
@@ -49,6 +49,9 @@
import org.onosproject.net.resource.LinkResourceService;
import org.onosproject.net.resource.LinkResourceStore;
import org.onosproject.net.resource.LinkResourceStoreDelegate;
+import org.onosproject.net.resource.MplsLabel;
+import org.onosproject.net.resource.MplsLabelResourceAllocation;
+import org.onosproject.net.resource.MplsLabelResourceRequest;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.resource.ResourceRequest;
import org.onosproject.net.resource.ResourceType;
@@ -104,6 +107,7 @@
return lambdas;
}
+
/**
* Returns available lambdas on specified links.
*
@@ -121,13 +125,36 @@
return lambdas;
}
+
+ /**
+ * Returns available MPLS label on specified link.
+ *
+ * @param link the link
+ * @return available MPLS labels on specified link
+ */
+ private Iterable<MplsLabel> getAvailableMplsLabels(Link link) {
+ Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
+ if (resAllocs == null) {
+ return Collections.emptySet();
+ }
+ Set<MplsLabel> mplsLabels = new HashSet<>();
+ for (ResourceAllocation res : resAllocs) {
+ if (res.type() == ResourceType.MPLS_LABEL) {
+
+ mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel());
+ }
+ }
+
+ return mplsLabels;
+ }
+
@Override
public LinkResourceAllocations requestResources(LinkResourceRequest req) {
// TODO Concatenate multiple bandwidth requests.
// TODO Support multiple lambda resource requests.
// TODO Throw appropriate exception.
-
Set<ResourceAllocation> allocs = new HashSet<>();
+ Map<Link, Set<ResourceAllocation>> allocsPerLink = new HashMap<>();
for (ResourceRequest r : req.resources()) {
switch (r.type()) {
case BANDWIDTH:
@@ -144,6 +171,24 @@
return null;
}
break;
+ case MPLS_LABEL:
+ for (Link link : req.links()) {
+ if (allocsPerLink.get(link) == null) {
+ allocsPerLink.put(link,
+ new HashSet<ResourceAllocation>());
+ }
+ Iterator<MplsLabel> mplsIter = getAvailableMplsLabels(link)
+ .iterator();
+ if (mplsIter.hasNext()) {
+ allocsPerLink.get(link)
+ .add(new MplsLabelResourceAllocation(mplsIter
+ .next()));
+ } else {
+ log.info("Failed to allocate MPLS resource.");
+ break;
+ }
+ }
+ break;
default:
break;
}
@@ -151,7 +196,8 @@
Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
for (Link link : req.links()) {
- allocations.put(link, allocs);
+ allocations.put(link, new HashSet<ResourceAllocation>(allocs));
+ allocations.get(link).addAll(allocsPerLink.get(link));
}
LinkResourceAllocations result =
new DefaultLinkResourceAllocations(req, allocations);
@@ -203,6 +249,8 @@
case LAMBDA:
result.add(new LambdaResourceRequest());
break;
+ case MPLS_LABEL:
+ result.add(new MplsLabelResourceRequest());
default:
break;
}
diff --git a/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java b/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
index 2e18430..7099464 100644
--- a/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/group/impl/GroupManagerTest.java
@@ -24,6 +24,7 @@
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.MacAddress;
+import org.onlab.packet.MplsLabel;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.DefaultGroupId;
@@ -162,7 +163,7 @@
.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
.setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
.pushMpls()
- .setMpls(106);
+ .setMpls(MplsLabel.mplsLabel(106));
buckets.add(DefaultGroupBucket.createSelectGroupBucket(
tBuilder.build()));
}
@@ -244,7 +245,7 @@
.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
.setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
.pushMpls()
- .setMpls(106);
+ .setMpls(MplsLabel.mplsLabel(106));
addBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
tBuilder.build()));
buckets.add(DefaultGroupBucket.createSelectGroupBucket(
@@ -280,7 +281,7 @@
.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
.setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
.pushMpls()
- .setMpls(106);
+ .setMpls(MplsLabel.mplsLabel(106));
removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
tBuilder.build()));
buckets.remove(DefaultGroupBucket.createSelectGroupBucket(
@@ -338,7 +339,7 @@
.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
.setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
.pushMpls()
- .setMpls(106);
+ .setMpls(MplsLabel.mplsLabel(106));
buckets.add(DefaultGroupBucket.createSelectGroupBucket(
tBuilder.build()));
}
@@ -411,7 +412,7 @@
.setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
.setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
.pushMpls()
- .setMpls(106);
+ .setMpls(MplsLabel.mplsLabel(106));
buckets.add(DefaultGroupBucket.createSelectGroupBucket(
tBuilder.build()));
}
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/MplsIntentCompilerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/MplsIntentCompilerTest.java
new file mode 100644
index 0000000..1a81225
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/MplsIntentCompilerTest.java
@@ -0,0 +1,175 @@
+package org.onosproject.net.intent.impl;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+
+import org.onlab.packet.MplsLabel;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Link;
+import org.onosproject.net.Path;
+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.MplsIntent;
+import org.onosproject.net.intent.MplsPathIntent;
+
+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.DefaultEdgeLink.createEdgeLink;
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.NetTestTools.APP_ID;
+import static org.onosproject.net.NetTestTools.connectPoint;
+import static org.onosproject.net.PortNumber.portNumber;
+import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
+
+/**
+ * Unit tests for the HostToHost intent compiler.
+ */
+public class MplsIntentCompilerTest extends AbstractIntentTest {
+
+ private static final ApplicationId APPID = new TestApplicationId("foo");
+
+ private TrafficSelector selector = new IntentTestsMocks.MockSelector();
+ private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
+
+ /**
+ * Creates a PointToPoint intent based on ingress and egress device Ids.
+ *
+ * @param ingressIdString string for id of ingress device
+ * @param egressIdString string for id of egress device
+ * @return PointToPointIntent for the two devices
+ */
+ private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel,
+ String egressIdString, Optional<MplsLabel> egressLabel) {
+
+ return new MplsIntent(APPID, selector, treatment,
+ connectPoint(ingressIdString, 1),
+ ingressLabel,
+ connectPoint(egressIdString, 1),
+ egressLabel);
+ }
+ /**
+ * Creates a compiler for HostToHost intents.
+ *
+ * @param hops string array describing the path hops to use when compiling
+ * @return HostToHost intent compiler
+ */
+ private MplsIntentCompiler makeCompiler(String[] hops) {
+ MplsIntentCompiler compiler =
+ new MplsIntentCompiler();
+ compiler.pathService = new IntentTestsMocks.MockPathService(hops);
+ return compiler;
+ }
+
+
+ /**
+ * Tests a pair of devices in an 8 hop path, forward direction.
+ */
+ @Test
+ public void testForwardPathCompilation() {
+ Optional<MplsLabel> ingressLabel = Optional.ofNullable(MplsLabel.mplsLabel(10));
+ Optional<MplsLabel> egressLabel = Optional.ofNullable(MplsLabel.mplsLabel(20));
+
+ MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel);
+ assertThat(intent, is(notNullValue()));
+
+ String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
+ MplsIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null, null);
+ assertThat(result, is(Matchers.notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent forwardResultIntent = result.get(0);
+ assertThat(forwardResultIntent instanceof MplsPathIntent, is(true));
+
+ if (forwardResultIntent instanceof MplsIntent) {
+ MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent;
+ // 7 links for the hops, plus one default lnk on ingress and egress
+ assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
+ assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
+ assertEquals(forwardPathIntent.egressLabel(), egressLabel);
+ assertEquals(forwardPathIntent.ingressLabel(), ingressLabel);
+ }
+ }
+
+ /**
+ * Tests a pair of devices in an 8 hop path, forward direction.
+ */
+ @Test
+ public void testReversePathCompilation() {
+ Optional<MplsLabel> ingressLabel = Optional.ofNullable(MplsLabel.mplsLabel(10));
+ Optional<MplsLabel> egressLabel = Optional.ofNullable(MplsLabel.mplsLabel(20));
+
+ MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel);
+ assertThat(intent, is(notNullValue()));
+
+ String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
+ MplsIntentCompiler compiler = makeCompiler(hops);
+ assertThat(compiler, is(notNullValue()));
+
+ List<Intent> result = compiler.compile(intent, null, null);
+ assertThat(result, is(Matchers.notNullValue()));
+ assertThat(result, hasSize(1));
+ Intent reverseResultIntent = result.get(0);
+ assertThat(reverseResultIntent instanceof MplsPathIntent, is(true));
+
+ if (reverseResultIntent instanceof MplsIntent) {
+ MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent;
+ assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
+ assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
+ assertEquals(reversePathIntent.egressLabel(), egressLabel);
+ assertEquals(reversePathIntent.ingressLabel(), ingressLabel);
+ }
+ }
+
+ /**
+ * Tests compilation of the intent which designates two different ports on the same switch.
+ */
+ @Test
+ public void testSameSwitchDifferentPortsIntentCompilation() {
+ ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
+ ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
+ MplsIntent intent = new MplsIntent(APP_ID, selector, treatment, src, Optional.empty(), dst, Optional.empty());
+
+ String[] hops = {"1"};
+ MplsIntentCompiler sut = makeCompiler(hops);
+
+ List<Intent> compiled = sut.compile(intent, null, null);
+
+ assertThat(compiled, hasSize(1));
+ assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class)));
+ Path path = ((MplsPathIntent) compiled.get(0)).path();
+
+ assertThat(path.links(), hasSize(2));
+ Link firstLink = path.links().get(0);
+ assertThat(firstLink, is(createEdgeLink(src, true)));
+ Link secondLink = path.links().get(1);
+ assertThat(secondLink, is(createEdgeLink(dst, false)));
+ }
+}