blob: 007c132131f55c17ad00d37e62210e7201f6ae8a [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.compiler;
17
Michele Santuari69fc2ff2015-12-03 17:05:35 +010018import com.google.common.collect.ImmutableList;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010022import org.onlab.packet.VlanId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080023import org.onosproject.TestApplicationId;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DefaultLink;
29import org.onosproject.net.DefaultPath;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010030import org.onosproject.net.EncapsulationType;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.Link;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010037import org.onosproject.net.flow.instructions.Instructions;
38import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080039import org.onosproject.net.intent.FlowRuleIntent;
40import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.MockIdGenerator;
43import org.onosproject.net.intent.PathIntent;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010044import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045import org.onosproject.net.provider.ProviderId;
46
Michele Santuari69fc2ff2015-12-03 17:05:35 +010047import java.util.Arrays;
48import java.util.Collection;
49import java.util.Collections;
50import java.util.List;
51import java.util.Set;
52import java.util.stream.Collectors;
53
54import static org.easymock.EasyMock.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055import static org.hamcrest.MatcherAssert.assertThat;
56import static org.hamcrest.Matchers.hasSize;
57import static org.hamcrest.Matchers.is;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010058import static org.hamcrest.number.OrderingComparison.greaterThan;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080059import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
60import static org.onosproject.net.Link.Type.DIRECT;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010061import static org.onosproject.net.NetTestTools.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062
63/**
64 * Unit tests for PathIntentCompiler.
65 */
66public class PathIntentCompilerTest {
67
68 private CoreService coreService;
69 private IntentExtensionService intentExtensionService;
70 private IdGenerator idGenerator = new MockIdGenerator();
71 private PathIntentCompiler sut;
72
73 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
74 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Michele Santuari3ea53902016-02-15 11:21:56 +010075 private final VlanId ingressVlan = VlanId.vlanId(((short) 101));
76 private final TrafficSelector vlanSelector = DefaultTrafficSelector.builder()
77 .matchVlanId(ingressVlan).build();
78 private final VlanId egressVlan = VlanId.vlanId((short) 100);
79 private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment.builder()
80 .setVlanId(egressVlan).build();
81
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080082 private final ApplicationId appId = new TestApplicationId("test");
83 private final ProviderId pid = new ProviderId("of", "test");
84 private final ConnectPoint d1p1 = connectPoint("s1", 0);
85 private final ConnectPoint d2p0 = connectPoint("s2", 0);
86 private final ConnectPoint d2p1 = connectPoint("s2", 1);
87 private final ConnectPoint d3p1 = connectPoint("s3", 1);
88 private final ConnectPoint d3p0 = connectPoint("s3", 10);
89 private final ConnectPoint d1p0 = connectPoint("s1", 10);
Brian O'Connor81134662015-06-25 17:23:33 -040090 private static final int PRIORITY = 555;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080091
92 private final List<Link> links = Arrays.asList(
93 createEdgeLink(d1p0, true),
Ray Milkey2693bda2016-01-22 16:08:14 -080094 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
95 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080096 createEdgeLink(d3p0, false)
97 );
98 private final int hops = links.size() - 1;
99 private PathIntent intent;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100100 private PathIntent constraintIntent;
Michele Santuari3ea53902016-02-15 11:21:56 +0100101 private PathIntent constrainIngressEgressVlanIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800102
103 /**
104 * Configures objects used in all the test cases.
105 */
106 @Before
107 public void setUp() {
108 sut = new PathIntentCompiler();
109 coreService = createMock(CoreService.class);
110 expect(coreService.registerApplication("org.onosproject.net.intent"))
111 .andReturn(appId);
112 sut.coreService = coreService;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100113 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800114
115 Intent.bindIdGenerator(idGenerator);
116
117 intent = PathIntent.builder()
118 .appId(APP_ID)
119 .selector(selector)
120 .treatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400121 .priority(PRIORITY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800122 .path(new DefaultPath(pid, links, hops))
123 .build();
Michele Santuari3ea53902016-02-15 11:21:56 +0100124 //Intent with VLAN encap without egress VLAN
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100125 constraintIntent = PathIntent.builder()
126 .appId(APP_ID)
127 .selector(selector)
128 .treatment(treatment)
129 .priority(PRIORITY)
130 .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN)))
131 .path(new DefaultPath(pid, links, hops))
132 .build();
Michele Santuari3ea53902016-02-15 11:21:56 +0100133 //Intent with VLAN encap with ingress and egress VLAN
134 constrainIngressEgressVlanIntent = PathIntent.builder()
135 .appId(APP_ID)
136 .selector(vlanSelector)
137 .treatment(vlanTreatment)
138 .priority(PRIORITY)
139 .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN)))
140 .path(new DefaultPath(pid, links, hops))
141 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800142 intentExtensionService = createMock(IntentExtensionService.class);
143 intentExtensionService.registerCompiler(PathIntent.class, sut);
144 intentExtensionService.unregisterCompiler(PathIntent.class);
145 sut.intentManager = intentExtensionService;
146
147 replay(coreService, intentExtensionService);
148 }
149
150 /**
151 * Tears down objects used in all the test cases.
152 */
153 @After
154 public void tearDown() {
155 Intent.unbindIdGenerator(idGenerator);
156 }
157
158 /**
159 * Tests the compilation behavior of the path intent compiler.
160 */
161 @Test
162 public void testCompile() {
163 sut.activate();
164
165 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
166 assertThat(compiled, hasSize(1));
167
168 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
169
170 FlowRule rule1 = rules.stream()
171 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
172 .findFirst()
173 .get();
174 assertThat(rule1.deviceId(), is(d1p0.deviceId()));
175 assertThat(rule1.selector(),
176 is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build()));
177 assertThat(rule1.treatment(),
178 is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400179 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800180
181 FlowRule rule2 = rules.stream()
182 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
183 .findFirst()
184 .get();
185 assertThat(rule2.deviceId(), is(d2p0.deviceId()));
186 assertThat(rule2.selector(),
187 is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build()));
188 assertThat(rule2.treatment(),
189 is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400190 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800191
192 FlowRule rule3 = rules.stream()
193 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
194 .findFirst()
195 .get();
196 assertThat(rule3.deviceId(), is(d3p1.deviceId()));
197 assertThat(rule3.selector(),
198 is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build()));
199 assertThat(rule3.treatment(),
200 is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400201 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800202
203 sut.deactivate();
204 }
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100205
206 /**
207 * Tests the compilation behavior of the path intent compiler in case of
Michele Santuari3ea53902016-02-15 11:21:56 +0100208 * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}.
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100209 */
210 @Test
211 public void testEncapCompile() {
212 sut.activate();
213
214 List<Intent> compiled = sut.compile(constraintIntent, Collections.emptyList(), Collections.emptySet());
215 assertThat(compiled, hasSize(1));
216
217 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
218 assertThat(rules, hasSize(3));
219
220 FlowRule rule1 = rules.stream()
221 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
222 .findFirst()
223 .get();
224 assertThat(rule1.deviceId(), is(d1p0.deviceId()));
225 assertThat(rule1.priority(), is(intent.priority()));
226 verifyEncapSelector(rule1.selector(), d1p0, VlanId.NONE);
227 VlanId vlanToEncap = verifyEncapTreatment(rule1.treatment(), d1p1, true, false);
228
229 FlowRule rule2 = rules.stream()
230 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
231 .findFirst()
232 .get();
233 assertThat(rule2.deviceId(), is(d2p0.deviceId()));
234 assertThat(rule2.priority(), is(intent.priority()));
235 verifyEncapSelector(rule2.selector(), d2p0, vlanToEncap);
236 verifyEncapTreatment(rule2.treatment(), d2p1, false, false);
237
238 FlowRule rule3 = rules.stream()
239 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
240 .findFirst()
241 .get();
242 assertThat(rule3.deviceId(), is(d3p1.deviceId()));
243 assertThat(rule3.priority(), is(intent.priority()));
244 verifyEncapSelector(rule3.selector(), d3p1, vlanToEncap);
245 verifyEncapTreatment(rule3.treatment(), d3p0, false, true);
246
247 sut.deactivate();
248 }
249
Michele Santuari3ea53902016-02-15 11:21:56 +0100250 /**
251 * Tests the compilation behavior of the path intent compiler in case of
252 * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}.
253 * This test includes a selector to match a VLAN at the ingress and a treatment to set VLAN at the egress.
254 */
255 @Test
256 public void testEncapIngressEgressVlansCompile() {
257 sut.activate();
258
259 List<Intent> compiled = sut.compile(constrainIngressEgressVlanIntent,
260 Collections.emptyList(), Collections.emptySet());
261 assertThat(compiled, hasSize(1));
262
263 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
264 assertThat(rules, hasSize(3));
265
266 FlowRule rule1 = rules.stream()
267 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
268 .findFirst()
269 .get();
270 assertThat(rule1.deviceId(), is(d1p0.deviceId()));
271 assertThat(rule1.priority(), is(intent.priority()));
272 verifyEncapSelector(rule1.selector(), d1p0, ingressVlan);
273 VlanId vlanToEncap = verifyEncapTreatment(rule1.treatment(), d1p1, true, false);
274
275 FlowRule rule2 = rules.stream()
276 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
277 .findFirst()
278 .get();
279 assertThat(rule2.deviceId(), is(d2p0.deviceId()));
280 assertThat(rule2.priority(), is(intent.priority()));
281 verifyEncapSelector(rule2.selector(), d2p0, vlanToEncap);
282 verifyEncapTreatment(rule2.treatment(), d2p1, false, false);
283
284 FlowRule rule3 = rules.stream()
285 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
286 .findFirst()
287 .get();
288 assertThat(rule3.deviceId(), is(d3p1.deviceId()));
289 assertThat(rule3.priority(), is(intent.priority()));
290 verifyEncapSelector(rule3.selector(), d3p1, vlanToEncap);
291 Set<L2ModificationInstruction.ModVlanIdInstruction> vlanMod = rule3.treatment().allInstructions().stream()
292 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
293 .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x)
294 .collect(Collectors.toSet());
295 assertThat(rule3.treatment().allInstructions().stream()
296 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
297 .collect(Collectors.toSet()), hasSize(1));
298 assertThat(vlanMod.iterator().next().vlanId(), is(egressVlan));
299 assertThat(rule3.treatment().allInstructions().stream()
300 .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction)
301 .collect(Collectors.toSet()), hasSize(0));
302
303 sut.deactivate();
304 }
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100305
306 private VlanId verifyEncapTreatment(TrafficTreatment trafficTreatment,
307 ConnectPoint egress, boolean isIngress, boolean isEgress) {
308 Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream()
309 .filter(treat -> treat instanceof Instructions.OutputInstruction)
310 .map(treat -> (Instructions.OutputInstruction) treat)
311 .collect(Collectors.toSet());
312 assertThat(ruleOutput, hasSize(1));
313 assertThat((ruleOutput.iterator().next()).port(), is(egress.port()));
314 VlanId vlanToEncap = VlanId.NONE;
315 if (isIngress && !isEgress) {
316 Set<L2ModificationInstruction.ModVlanIdInstruction> vlanRules = trafficTreatment.allInstructions().stream()
317 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
318 .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x)
319 .collect(Collectors.toSet());
320 assertThat(vlanRules, hasSize(1));
321 L2ModificationInstruction.ModVlanIdInstruction vlanRule = vlanRules.iterator().next();
322 assertThat(vlanRule.vlanId().toShort(), greaterThan((short) 0));
323 vlanToEncap = vlanRule.vlanId();
324 } else if (!isIngress && !isEgress) {
325 assertThat(trafficTreatment.allInstructions().stream()
326 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
327 .collect(Collectors.toSet()), hasSize(0));
328 } else {
329 assertThat(trafficTreatment.allInstructions().stream()
330 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
331 .collect(Collectors.toSet()), hasSize(0));
332 assertThat(trafficTreatment.allInstructions().stream()
333 .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction)
334 .collect(Collectors.toSet()), hasSize(1));
335
336 }
337
338 return vlanToEncap;
339
340 }
341
342 private void verifyEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, VlanId vlanToMatch) {
343
344 is(DefaultTrafficSelector.builder(selector).matchInPort(ingress.port())
345 .matchVlanId(vlanToMatch).build());
346 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800347}