blob: 76b26f466093550782e55aaa3b472ae323df8dad [file] [log] [blame]
Sho SHIMIZU6c28f832015-02-20 16:12:19 -08001package org.onosproject.net.intent.impl.compiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +01002
3import java.util.List;
4import java.util.Optional;
5
6import org.hamcrest.Matchers;
7import org.junit.Test;
8
9import static org.junit.Assert.assertEquals;
10
11
12import org.onlab.packet.MplsLabel;
13import org.onosproject.TestApplicationId;
14import org.onosproject.core.ApplicationId;
15import org.onosproject.net.ConnectPoint;
16import org.onosproject.net.Link;
17import org.onosproject.net.Path;
18import org.onosproject.net.flow.TrafficSelector;
19import org.onosproject.net.flow.TrafficTreatment;
20import org.onosproject.net.intent.AbstractIntentTest;
21import org.onosproject.net.intent.Intent;
22import org.onosproject.net.intent.IntentTestsMocks;
23import org.onosproject.net.intent.MplsIntent;
24import org.onosproject.net.intent.MplsPathIntent;
25
26import static org.hamcrest.CoreMatchers.instanceOf;
27import static org.hamcrest.CoreMatchers.notNullValue;
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.hasSize;
30import static org.hamcrest.Matchers.is;
31import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
32import static org.onosproject.net.DeviceId.deviceId;
33import static org.onosproject.net.NetTestTools.APP_ID;
34import static org.onosproject.net.NetTestTools.connectPoint;
35import static org.onosproject.net.PortNumber.portNumber;
36import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
37
38/**
39 * Unit tests for the HostToHost intent compiler.
40 */
41public class MplsIntentCompilerTest extends AbstractIntentTest {
42
43 private static final ApplicationId APPID = new TestApplicationId("foo");
44
45 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
46 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
47
48 /**
49 * Creates a PointToPoint intent based on ingress and egress device Ids.
50 *
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
54 */
55 private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel,
56 String egressIdString, Optional<MplsLabel> egressLabel) {
57
Ray Milkeyebc5d222015-03-18 15:45:36 -070058 return MplsIntent.builder()
59 .appId(APPID)
60 .selector(selector)
61 .treatment(treatment)
62 .ingressPoint(connectPoint(ingressIdString, 1))
63 .ingressLabel(ingressLabel)
64 .egressPoint(connectPoint(egressIdString, 1))
65 .egressLabel(egressLabel).build();
Michele Santuari4b6019e2014-12-19 11:31:45 +010066 }
67 /**
68 * Creates a compiler for HostToHost intents.
69 *
70 * @param hops string array describing the path hops to use when compiling
71 * @return HostToHost intent compiler
72 */
73 private MplsIntentCompiler makeCompiler(String[] hops) {
74 MplsIntentCompiler compiler =
75 new MplsIntentCompiler();
76 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
77 return compiler;
78 }
79
80
81 /**
82 * Tests a pair of devices in an 8 hop path, forward direction.
83 */
84 @Test
85 public void testForwardPathCompilation() {
Ray Milkey167ae0b2015-02-26 07:33:25 -080086 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
87 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
Michele Santuari4b6019e2014-12-19 11:31:45 +010088
89 MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel);
90 assertThat(intent, is(notNullValue()));
91
92 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
93 MplsIntentCompiler compiler = makeCompiler(hops);
94 assertThat(compiler, is(notNullValue()));
95
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));
101
Ray Milkeya046a872015-02-26 12:07:50 -0800102 // if statement suppresses static analysis warnings about unchecked cast
103 if (forwardResultIntent instanceof MplsPathIntent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100104 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);
116 }
117 }
118
119 /**
120 * Tests a pair of devices in an 8 hop path, forward direction.
121 */
122 @Test
123 public void testReversePathCompilation() {
Ray Milkey167ae0b2015-02-26 07:33:25 -0800124 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
125 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100126
127 MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel);
128 assertThat(intent, is(notNullValue()));
129
130 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
131 MplsIntentCompiler compiler = makeCompiler(hops);
132 assertThat(compiler, is(notNullValue()));
133
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));
139
Ray Milkeya046a872015-02-26 12:07:50 -0800140 // if statement suppresses static analysis warnings about unchecked cast
141 if (reverseResultIntent instanceof MplsPathIntent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100142 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);
153 }
154 }
155
156 /**
157 * Tests compilation of the intent which designates two different ports on the same switch.
158 */
159 @Test
160 public void testSameSwitchDifferentPortsIntentCompilation() {
161 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
162 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700163 MplsIntent intent = MplsIntent.builder()
164 .appId(APP_ID)
165 .selector(selector)
166 .treatment(treatment)
167 .ingressPoint(src)
168 .ingressLabel(Optional.empty())
169 .egressPoint(dst)
170 .egressLabel(Optional.empty())
171 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100172
173 String[] hops = {"1"};
174 MplsIntentCompiler sut = makeCompiler(hops);
175
176 List<Intent> compiled = sut.compile(intent, null, null);
177
178 assertThat(compiled, hasSize(1));
179 assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class)));
180 Path path = ((MplsPathIntent) compiled.get(0)).path();
181
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)));
187 }
188}