Michele Santuari
Committed by Gerrit Code Review

Support encapsulation in PathIntent

- related to ONOS-3467
- unit test work
- depends on ONOS-3507 and also on the advertisement of VLAN resource
  for different devices.

Change-Id: Ia852c751135b5ca4a16901c6f3a85ceea11514a3
...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl.compiler; ...@@ -17,6 +17,7 @@ package org.onosproject.net.intent.impl.compiler;
17 17
18 import com.google.common.collect.ImmutableList; 18 import com.google.common.collect.ImmutableList;
19 import org.onlab.packet.MplsLabel; 19 import org.onlab.packet.MplsLabel;
20 +import org.onlab.packet.VlanId;
20 import org.onosproject.net.newresource.ResourceAllocation; 21 import org.onosproject.net.newresource.ResourceAllocation;
21 import org.onosproject.net.newresource.ResourceConsumer; 22 import org.onosproject.net.newresource.ResourceConsumer;
22 import org.onosproject.net.newresource.ResourceListener; 23 import org.onosproject.net.newresource.ResourceListener;
...@@ -25,6 +26,7 @@ import org.onosproject.net.newresource.ResourceService; ...@@ -25,6 +26,7 @@ import org.onosproject.net.newresource.ResourceService;
25 26
26 import java.util.Collection; 27 import java.util.Collection;
27 import java.util.HashMap; 28 import java.util.HashMap;
29 +import java.util.HashSet;
28 import java.util.List; 30 import java.util.List;
29 import java.util.Map; 31 import java.util.Map;
30 import java.util.Optional; 32 import java.util.Optional;
...@@ -91,8 +93,11 @@ class MockResourceService implements ResourceService { ...@@ -91,8 +93,11 @@ class MockResourceService implements ResourceService {
91 93
92 @Override 94 @Override
93 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) { 95 public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
94 - ResourcePath resource = parent.child(MplsLabel.mplsLabel(10)); 96 +
95 - return ImmutableList.of(resource); 97 + Collection<ResourcePath> resources = new HashSet<ResourcePath>();
98 + resources.add(parent.child(VlanId.vlanId((short) 10)));
99 + resources.add(parent.child(MplsLabel.mplsLabel(10)));
100 + return ImmutableList.copyOf(resources);
96 } 101 }
97 102
98 @Override 103 @Override
......
...@@ -15,14 +15,11 @@ ...@@ -15,14 +15,11 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl.compiler; 16 package org.onosproject.net.intent.impl.compiler;
17 17
18 -import java.util.Arrays; 18 +import com.google.common.collect.ImmutableList;
19 -import java.util.Collection;
20 -import java.util.Collections;
21 -import java.util.List;
22 -
23 import org.junit.After; 19 import org.junit.After;
24 import org.junit.Before; 20 import org.junit.Before;
25 import org.junit.Test; 21 import org.junit.Test;
22 +import org.onlab.packet.VlanId;
26 import org.onosproject.TestApplicationId; 23 import org.onosproject.TestApplicationId;
27 import org.onosproject.core.ApplicationId; 24 import org.onosproject.core.ApplicationId;
28 import org.onosproject.core.CoreService; 25 import org.onosproject.core.CoreService;
...@@ -30,30 +27,38 @@ import org.onosproject.core.IdGenerator; ...@@ -30,30 +27,38 @@ import org.onosproject.core.IdGenerator;
30 import org.onosproject.net.ConnectPoint; 27 import org.onosproject.net.ConnectPoint;
31 import org.onosproject.net.DefaultLink; 28 import org.onosproject.net.DefaultLink;
32 import org.onosproject.net.DefaultPath; 29 import org.onosproject.net.DefaultPath;
30 +import org.onosproject.net.EncapsulationType;
33 import org.onosproject.net.Link; 31 import org.onosproject.net.Link;
34 import org.onosproject.net.flow.DefaultTrafficSelector; 32 import org.onosproject.net.flow.DefaultTrafficSelector;
35 import org.onosproject.net.flow.DefaultTrafficTreatment; 33 import org.onosproject.net.flow.DefaultTrafficTreatment;
36 import org.onosproject.net.flow.FlowRule; 34 import org.onosproject.net.flow.FlowRule;
37 import org.onosproject.net.flow.TrafficSelector; 35 import org.onosproject.net.flow.TrafficSelector;
38 import org.onosproject.net.flow.TrafficTreatment; 36 import org.onosproject.net.flow.TrafficTreatment;
37 +import org.onosproject.net.flow.instructions.Instructions;
38 +import org.onosproject.net.flow.instructions.L2ModificationInstruction;
39 import org.onosproject.net.intent.FlowRuleIntent; 39 import org.onosproject.net.intent.FlowRuleIntent;
40 import org.onosproject.net.intent.Intent; 40 import org.onosproject.net.intent.Intent;
41 import org.onosproject.net.intent.IntentExtensionService; 41 import org.onosproject.net.intent.IntentExtensionService;
42 import org.onosproject.net.intent.MockIdGenerator; 42 import org.onosproject.net.intent.MockIdGenerator;
43 import org.onosproject.net.intent.PathIntent; 43 import org.onosproject.net.intent.PathIntent;
44 +import org.onosproject.net.intent.constraint.EncapsulationConstraint;
44 import org.onosproject.net.provider.ProviderId; 45 import org.onosproject.net.provider.ProviderId;
45 46
46 -import static org.easymock.EasyMock.createMock; 47 +import java.util.Arrays;
47 -import static org.easymock.EasyMock.expect; 48 +import java.util.Collection;
48 -import static org.easymock.EasyMock.replay; 49 +import java.util.Collections;
50 +import java.util.List;
51 +import java.util.Set;
52 +import java.util.stream.Collectors;
53 +
54 +import static org.easymock.EasyMock.*;
49 import static org.hamcrest.MatcherAssert.assertThat; 55 import static org.hamcrest.MatcherAssert.assertThat;
50 import static org.hamcrest.Matchers.hasSize; 56 import static org.hamcrest.Matchers.hasSize;
51 import static org.hamcrest.Matchers.is; 57 import static org.hamcrest.Matchers.is;
58 +import static org.hamcrest.number.OrderingComparison.greaterThan;
52 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; 59 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
53 import static org.onosproject.net.Link.Type.DIRECT; 60 import static org.onosproject.net.Link.Type.DIRECT;
54 -import static org.onosproject.net.NetTestTools.APP_ID; 61 +import static org.onosproject.net.NetTestTools.*;
55 -import static org.onosproject.net.NetTestTools.PID;
56 -import static org.onosproject.net.NetTestTools.connectPoint;
57 62
58 /** 63 /**
59 * Unit tests for PathIntentCompiler. 64 * Unit tests for PathIntentCompiler.
...@@ -85,6 +90,7 @@ public class PathIntentCompilerTest { ...@@ -85,6 +90,7 @@ public class PathIntentCompilerTest {
85 ); 90 );
86 private final int hops = links.size() - 1; 91 private final int hops = links.size() - 1;
87 private PathIntent intent; 92 private PathIntent intent;
93 + private PathIntent constraintIntent;
88 94
89 /** 95 /**
90 * Configures objects used in all the test cases. 96 * Configures objects used in all the test cases.
...@@ -96,6 +102,7 @@ public class PathIntentCompilerTest { ...@@ -96,6 +102,7 @@ public class PathIntentCompilerTest {
96 expect(coreService.registerApplication("org.onosproject.net.intent")) 102 expect(coreService.registerApplication("org.onosproject.net.intent"))
97 .andReturn(appId); 103 .andReturn(appId);
98 sut.coreService = coreService; 104 sut.coreService = coreService;
105 + sut.resourceService = new MockResourceService();
99 106
100 Intent.bindIdGenerator(idGenerator); 107 Intent.bindIdGenerator(idGenerator);
101 108
...@@ -106,6 +113,14 @@ public class PathIntentCompilerTest { ...@@ -106,6 +113,14 @@ public class PathIntentCompilerTest {
106 .priority(PRIORITY) 113 .priority(PRIORITY)
107 .path(new DefaultPath(pid, links, hops)) 114 .path(new DefaultPath(pid, links, hops))
108 .build(); 115 .build();
116 + constraintIntent = PathIntent.builder()
117 + .appId(APP_ID)
118 + .selector(selector)
119 + .treatment(treatment)
120 + .priority(PRIORITY)
121 + .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN)))
122 + .path(new DefaultPath(pid, links, hops))
123 + .build();
109 intentExtensionService = createMock(IntentExtensionService.class); 124 intentExtensionService = createMock(IntentExtensionService.class);
110 intentExtensionService.registerCompiler(PathIntent.class, sut); 125 intentExtensionService.registerCompiler(PathIntent.class, sut);
111 intentExtensionService.unregisterCompiler(PathIntent.class); 126 intentExtensionService.unregisterCompiler(PathIntent.class);
...@@ -169,4 +184,91 @@ public class PathIntentCompilerTest { ...@@ -169,4 +184,91 @@ public class PathIntentCompilerTest {
169 184
170 sut.deactivate(); 185 sut.deactivate();
171 } 186 }
187 +
188 + /**
189 + * Tests the compilation behavior of the path intent compiler in case of
190 + * encasulation costraint {@link EncapsulationConstraint}.
191 + */
192 + @Test
193 + public void testEncapCompile() {
194 + sut.activate();
195 +
196 + List<Intent> compiled = sut.compile(constraintIntent, Collections.emptyList(), Collections.emptySet());
197 + assertThat(compiled, hasSize(1));
198 +
199 + Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
200 + assertThat(rules, hasSize(3));
201 +
202 + FlowRule rule1 = rules.stream()
203 + .filter(x -> x.deviceId().equals(d1p0.deviceId()))
204 + .findFirst()
205 + .get();
206 + assertThat(rule1.deviceId(), is(d1p0.deviceId()));
207 + assertThat(rule1.priority(), is(intent.priority()));
208 + verifyEncapSelector(rule1.selector(), d1p0, VlanId.NONE);
209 + VlanId vlanToEncap = verifyEncapTreatment(rule1.treatment(), d1p1, true, false);
210 +
211 + FlowRule rule2 = rules.stream()
212 + .filter(x -> x.deviceId().equals(d2p0.deviceId()))
213 + .findFirst()
214 + .get();
215 + assertThat(rule2.deviceId(), is(d2p0.deviceId()));
216 + assertThat(rule2.priority(), is(intent.priority()));
217 + verifyEncapSelector(rule2.selector(), d2p0, vlanToEncap);
218 + verifyEncapTreatment(rule2.treatment(), d2p1, false, false);
219 +
220 + FlowRule rule3 = rules.stream()
221 + .filter(x -> x.deviceId().equals(d3p0.deviceId()))
222 + .findFirst()
223 + .get();
224 + assertThat(rule3.deviceId(), is(d3p1.deviceId()));
225 + assertThat(rule3.priority(), is(intent.priority()));
226 + verifyEncapSelector(rule3.selector(), d3p1, vlanToEncap);
227 + verifyEncapTreatment(rule3.treatment(), d3p0, false, true);
228 +
229 + sut.deactivate();
230 + }
231 +
232 +
233 + private VlanId verifyEncapTreatment(TrafficTreatment trafficTreatment,
234 + ConnectPoint egress, boolean isIngress, boolean isEgress) {
235 + Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream()
236 + .filter(treat -> treat instanceof Instructions.OutputInstruction)
237 + .map(treat -> (Instructions.OutputInstruction) treat)
238 + .collect(Collectors.toSet());
239 + assertThat(ruleOutput, hasSize(1));
240 + assertThat((ruleOutput.iterator().next()).port(), is(egress.port()));
241 + VlanId vlanToEncap = VlanId.NONE;
242 + if (isIngress && !isEgress) {
243 + Set<L2ModificationInstruction.ModVlanIdInstruction> vlanRules = trafficTreatment.allInstructions().stream()
244 + .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
245 + .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x)
246 + .collect(Collectors.toSet());
247 + assertThat(vlanRules, hasSize(1));
248 + L2ModificationInstruction.ModVlanIdInstruction vlanRule = vlanRules.iterator().next();
249 + assertThat(vlanRule.vlanId().toShort(), greaterThan((short) 0));
250 + vlanToEncap = vlanRule.vlanId();
251 + } else if (!isIngress && !isEgress) {
252 + assertThat(trafficTreatment.allInstructions().stream()
253 + .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
254 + .collect(Collectors.toSet()), hasSize(0));
255 + } else {
256 + assertThat(trafficTreatment.allInstructions().stream()
257 + .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
258 + .collect(Collectors.toSet()), hasSize(0));
259 + assertThat(trafficTreatment.allInstructions().stream()
260 + .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction)
261 + .collect(Collectors.toSet()), hasSize(1));
262 +
263 + }
264 +
265 + return vlanToEncap;
266 +
267 + }
268 +
269 + private void verifyEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, VlanId vlanToMatch) {
270 +
271 + is(DefaultTrafficSelector.builder(selector).matchInPort(ingress.port())
272 + .matchVlanId(vlanToMatch).build());
273 + }
172 } 274 }
......