Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 23 | import org.onosproject.TestApplicationId; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultLink; |
| 29 | import org.onosproject.net.DefaultPath; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 30 | import org.onosproject.net.EncapsulationType; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 37 | import org.onosproject.net.flow.instructions.Instructions; |
| 38 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.MockIdGenerator; |
| 43 | import org.onosproject.net.intent.PathIntent; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 44 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 45 | import org.onosproject.net.provider.ProviderId; |
| 46 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 47 | import java.util.Arrays; |
| 48 | import java.util.Collection; |
| 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.*; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 55 | import static org.hamcrest.MatcherAssert.assertThat; |
| 56 | import static org.hamcrest.Matchers.hasSize; |
| 57 | import static org.hamcrest.Matchers.is; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 58 | import static org.hamcrest.number.OrderingComparison.greaterThan; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 59 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 60 | import static org.onosproject.net.Link.Type.DIRECT; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 61 | import static org.onosproject.net.NetTestTools.*; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Unit tests for PathIntentCompiler. |
| 65 | */ |
| 66 | public 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 Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 75 | 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 SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 82 | 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 90 | private static final int PRIORITY = 555; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 91 | |
| 92 | private final List<Link> links = Arrays.asList( |
| 93 | createEdgeLink(d1p0, true), |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 94 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 95 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 96 | createEdgeLink(d3p0, false) |
| 97 | ); |
| 98 | private final int hops = links.size() - 1; |
| 99 | private PathIntent intent; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 100 | private PathIntent constraintIntent; |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 101 | private PathIntent constrainIngressEgressVlanIntent; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 102 | |
| 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 Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 113 | sut.resourceService = new MockResourceService(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 114 | |
| 115 | Intent.bindIdGenerator(idGenerator); |
| 116 | |
| 117 | intent = PathIntent.builder() |
| 118 | .appId(APP_ID) |
| 119 | .selector(selector) |
| 120 | .treatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 121 | .priority(PRIORITY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 122 | .path(new DefaultPath(pid, links, hops)) |
| 123 | .build(); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 124 | //Intent with VLAN encap without egress VLAN |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 125 | 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 Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 133 | //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 SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 142 | 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 179 | assertThat(rule1.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 180 | |
| 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 190 | assertThat(rule2.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 191 | |
| 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 201 | assertThat(rule3.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 202 | |
| 203 | sut.deactivate(); |
| 204 | } |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Tests the compilation behavior of the path intent compiler in case of |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 208 | * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}. |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 209 | */ |
| 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 Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 250 | /** |
| 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 Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 305 | |
| 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 SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 347 | } |