1 package org.onosproject.net.intent.impl.compiler;
4 import java.util.Optional;
6 import org.hamcrest.Matchers;
9 import static org.junit.Assert.assertEquals;
12 import org.onlab.packet.MplsLabel;
13 import org.onosproject.TestApplicationId;
14 import org.onosproject.core.ApplicationId;
15 import org.onosproject.net.ConnectPoint;
16 import org.onosproject.net.Link;
17 import org.onosproject.net.Path;
18 import org.onosproject.net.flow.TrafficSelector;
19 import org.onosproject.net.flow.TrafficTreatment;
20 import org.onosproject.net.intent.AbstractIntentTest;
21 import org.onosproject.net.intent.Intent;
22 import org.onosproject.net.intent.IntentTestsMocks;
23 import org.onosproject.net.intent.MplsIntent;
24 import org.onosproject.net.intent.MplsPathIntent;
26 import static org.hamcrest.CoreMatchers.instanceOf;
27 import static org.hamcrest.CoreMatchers.notNullValue;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.Matchers.hasSize;
30 import static org.hamcrest.Matchers.is;
31 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
32 import static org.onosproject.net.DeviceId.deviceId;
33 import static org.onosproject.net.NetTestTools.APP_ID;
34 import static org.onosproject.net.NetTestTools.connectPoint;
35 import static org.onosproject.net.PortNumber.portNumber;
36 import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
39 * Unit tests for the HostToHost intent compiler.
41 public class MplsIntentCompilerTest extends AbstractIntentTest {
43 private static final ApplicationId APPID = new TestApplicationId("foo");
45 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
46 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
49 * Creates a PointToPoint intent based on ingress and egress device Ids.
51 * @param ingressIdString string for id of ingress device
52 * @param egressIdString string for id of egress device
53 * @return PointToPointIntent for the two devices
55 private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel,
56 String egressIdString, Optional<MplsLabel> egressLabel) {
58 return MplsIntent.builder()
62 .ingressPoint(connectPoint(ingressIdString, 1))
63 .ingressLabel(ingressLabel)
64 .egressPoint(connectPoint(egressIdString, 1))
65 .egressLabel(egressLabel).build();
68 * Creates a compiler for HostToHost intents.
70 * @param hops string array describing the path hops to use when compiling
71 * @return HostToHost intent compiler
73 private MplsIntentCompiler makeCompiler(String[] hops) {
74 MplsIntentCompiler compiler =
75 new MplsIntentCompiler();
76 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
82 * Tests a pair of devices in an 8 hop path, forward direction.
85 public void testForwardPathCompilation() {
86 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
87 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
89 MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel);
90 assertThat(intent, is(notNullValue()));
92 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
93 MplsIntentCompiler compiler = makeCompiler(hops);
94 assertThat(compiler, is(notNullValue()));
96 List<Intent> result = compiler.compile(intent, null, null);
97 assertThat(result, is(Matchers.notNullValue()));
98 assertThat(result, hasSize(1));
99 Intent forwardResultIntent = result.get(0);
100 assertThat(forwardResultIntent instanceof MplsPathIntent, is(true));
102 // if statement suppresses static analysis warnings about unchecked cast
103 if (forwardResultIntent instanceof MplsPathIntent) {
104 MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent;
105 // 7 links for the hops, plus one default lnk on ingress and egress
106 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
107 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
108 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
109 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
110 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
111 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
112 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
113 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
114 assertEquals(forwardPathIntent.egressLabel(), egressLabel);
115 assertEquals(forwardPathIntent.ingressLabel(), ingressLabel);
120 * Tests a pair of devices in an 8 hop path, forward direction.
123 public void testReversePathCompilation() {
124 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
125 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
127 MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel);
128 assertThat(intent, is(notNullValue()));
130 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
131 MplsIntentCompiler compiler = makeCompiler(hops);
132 assertThat(compiler, is(notNullValue()));
134 List<Intent> result = compiler.compile(intent, null, null);
135 assertThat(result, is(Matchers.notNullValue()));
136 assertThat(result, hasSize(1));
137 Intent reverseResultIntent = result.get(0);
138 assertThat(reverseResultIntent instanceof MplsPathIntent, is(true));
140 // if statement suppresses static analysis warnings about unchecked cast
141 if (reverseResultIntent instanceof MplsPathIntent) {
142 MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent;
143 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
144 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
145 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
146 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
147 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
148 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
149 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
150 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
151 assertEquals(reversePathIntent.egressLabel(), egressLabel);
152 assertEquals(reversePathIntent.ingressLabel(), ingressLabel);
157 * Tests compilation of the intent which designates two different ports on the same switch.
160 public void testSameSwitchDifferentPortsIntentCompilation() {
161 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
162 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
163 MplsIntent intent = MplsIntent.builder()
166 .treatment(treatment)
168 .ingressLabel(Optional.empty())
170 .egressLabel(Optional.empty())
173 String[] hops = {"1"};
174 MplsIntentCompiler sut = makeCompiler(hops);
176 List<Intent> compiled = sut.compile(intent, null, null);
178 assertThat(compiled, hasSize(1));
179 assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class)));
180 Path path = ((MplsPathIntent) compiled.get(0)).path();
182 assertThat(path.links(), hasSize(2));
183 Link firstLink = path.links().get(0);
184 assertThat(firstLink, is(createEdgeLink(src, true)));
185 Link secondLink = path.links().get(1);
186 assertThat(secondLink, is(createEdgeLink(dst, false)));