Sho SHIMIZU
Committed by Ray Milkey

WIP: Fix ONOS-151 bug that raises exception when the same switch is designated

Now PointToPointIntentCompiler can handle the intent designates two
different ports belonging to the same switch.

Change-Id: Ie1ef4463fec0fc45a216bdb384792cc453a582c8
...@@ -18,7 +18,9 @@ package org.onlab.onos.net.intent.impl; ...@@ -18,7 +18,9 @@ package org.onlab.onos.net.intent.impl;
18 import org.apache.felix.scr.annotations.Activate; 18 import org.apache.felix.scr.annotations.Activate;
19 import org.apache.felix.scr.annotations.Component; 19 import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Deactivate; 20 import org.apache.felix.scr.annotations.Deactivate;
21 +import org.onlab.onos.net.ConnectPoint;
21 import org.onlab.onos.net.DefaultEdgeLink; 22 import org.onlab.onos.net.DefaultEdgeLink;
23 +import org.onlab.onos.net.DefaultLink;
22 import org.onlab.onos.net.DefaultPath; 24 import org.onlab.onos.net.DefaultPath;
23 import org.onlab.onos.net.Link; 25 import org.onlab.onos.net.Link;
24 import org.onlab.onos.net.Path; 26 import org.onlab.onos.net.Path;
...@@ -31,6 +33,7 @@ import java.util.ArrayList; ...@@ -31,6 +33,7 @@ import java.util.ArrayList;
31 import java.util.List; 33 import java.util.List;
32 34
33 import static java.util.Arrays.asList; 35 import static java.util.Arrays.asList;
36 +import static org.onlab.onos.net.Link.Type.DIRECT;
34 37
35 /** 38 /**
36 * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}. 39 * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}.
...@@ -55,13 +58,21 @@ public class PointToPointIntentCompiler ...@@ -55,13 +58,21 @@ public class PointToPointIntentCompiler
55 58
56 @Override 59 @Override
57 public List<Intent> compile(PointToPointIntent intent) { 60 public List<Intent> compile(PointToPointIntent intent) {
58 - Path path = getPath(intent, intent.ingressPoint().deviceId(), 61 + ConnectPoint ingressPoint = intent.ingressPoint();
59 - intent.egressPoint().deviceId()); 62 + ConnectPoint egressPoint = intent.egressPoint();
63 +
64 + if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
65 + List<Link> links = asList(new DefaultLink(PID, ingressPoint, egressPoint, DIRECT));
66 + return asList(createPathIntent(new DefaultPath(PID, links, 1), intent));
67 + }
60 68
61 List<Link> links = new ArrayList<>(); 69 List<Link> links = new ArrayList<>();
62 - links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true)); 70 + Path path = getPath(intent, ingressPoint.deviceId(),
71 + egressPoint.deviceId());
72 +
73 + links.add(DefaultEdgeLink.createEdgeLink(ingressPoint, true));
63 links.addAll(path.links()); 74 links.addAll(path.links());
64 - links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false)); 75 + links.add(DefaultEdgeLink.createEdgeLink(egressPoint, false));
65 76
66 return asList(createPathIntent(new DefaultPath(PID, links, path.cost(), 77 return asList(createPathIntent(new DefaultPath(PID, links, path.cost(),
67 path.annotations()), intent)); 78 path.annotations()), intent));
......
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
16 package org.onlab.onos.net.intent.impl; 16 package org.onlab.onos.net.intent.impl;
17 17
18 import org.hamcrest.Matchers; 18 import org.hamcrest.Matchers;
19 -//import org.junit.Test;
20 import org.junit.Test; 19 import org.junit.Test;
21 -import org.onlab.onos.core.ApplicationId;
22 import org.onlab.onos.TestApplicationId; 20 import org.onlab.onos.TestApplicationId;
21 +import org.onlab.onos.core.ApplicationId;
22 +import org.onlab.onos.net.ConnectPoint;
23 +import org.onlab.onos.net.Link;
24 +import org.onlab.onos.net.Path;
23 import org.onlab.onos.net.flow.TrafficSelector; 25 import org.onlab.onos.net.flow.TrafficSelector;
24 import org.onlab.onos.net.flow.TrafficTreatment; 26 import org.onlab.onos.net.flow.TrafficTreatment;
25 import org.onlab.onos.net.intent.Intent; 27 import org.onlab.onos.net.intent.Intent;
...@@ -29,11 +31,15 @@ import org.onlab.onos.net.intent.PointToPointIntent; ...@@ -29,11 +31,15 @@ import org.onlab.onos.net.intent.PointToPointIntent;
29 31
30 import java.util.List; 32 import java.util.List;
31 33
34 +import static org.hamcrest.CoreMatchers.instanceOf;
32 import static org.hamcrest.CoreMatchers.notNullValue; 35 import static org.hamcrest.CoreMatchers.notNullValue;
33 import static org.hamcrest.MatcherAssert.assertThat; 36 import static org.hamcrest.MatcherAssert.assertThat;
34 import static org.hamcrest.Matchers.hasSize; 37 import static org.hamcrest.Matchers.hasSize;
35 import static org.hamcrest.Matchers.is; 38 import static org.hamcrest.Matchers.is;
39 +import static org.onlab.onos.net.DeviceId.deviceId;
40 +import static org.onlab.onos.net.NetTestTools.APP_ID;
36 import static org.onlab.onos.net.NetTestTools.connectPoint; 41 import static org.onlab.onos.net.NetTestTools.connectPoint;
42 +import static org.onlab.onos.net.PortNumber.portNumber;
37 import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; 43 import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
38 44
39 /** 45 /**
...@@ -138,4 +144,28 @@ public class TestPointToPointIntentCompiler { ...@@ -138,4 +144,28 @@ public class TestPointToPointIntentCompiler {
138 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); 144 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
139 } 145 }
140 } 146 }
147 +
148 + /**
149 + * Tests compilation of the intent which designates two different ports on the same switch.
150 + */
151 + @Test
152 + public void testSameSwitchDifferentPortsIntentCompilation() {
153 + ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
154 + ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
155 + PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
156 +
157 + String[] hops = {"1"};
158 + PointToPointIntentCompiler sut = makeCompiler(hops);
159 +
160 + List<Intent> compiled = sut.compile(intent);
161 +
162 + assertThat(compiled, hasSize(1));
163 + assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
164 + Path path = ((PathIntent) compiled.get(0)).path();
165 +
166 + assertThat(path.links(), hasSize(1));
167 + Link link = path.links().get(0);
168 + assertThat(link.src(), is(src));
169 + assertThat(link.dst(), is(dst));
170 + }
141 } 171 }
......