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 | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.MplsLabel; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 25 | import org.onosproject.TestApplicationId; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigAdapter; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.IdGenerator; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DefaultLink; |
| 32 | import org.onosproject.net.DefaultPath; |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 34 | import org.onosproject.net.EncapsulationType; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 41 | import org.onosproject.net.flow.instructions.Instructions; |
| 42 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.FlowRuleIntent; |
| 44 | import org.onosproject.net.intent.Intent; |
| 45 | import org.onosproject.net.intent.IntentExtensionService; |
| 46 | import org.onosproject.net.intent.MockIdGenerator; |
| 47 | import org.onosproject.net.intent.PathIntent; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 48 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | import org.onosproject.net.provider.ProviderId; |
| 50 | |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 51 | import java.util.Arrays; |
| 52 | import java.util.Collection; |
| 53 | import java.util.Collections; |
| 54 | import java.util.List; |
| 55 | import java.util.Set; |
| 56 | import java.util.stream.Collectors; |
| 57 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 58 | import static org.easymock.EasyMock.createMock; |
| 59 | import static org.easymock.EasyMock.expect; |
| 60 | import static org.easymock.EasyMock.replay; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 61 | import static org.hamcrest.MatcherAssert.assertThat; |
| 62 | import static org.hamcrest.Matchers.hasSize; |
| 63 | import static org.hamcrest.Matchers.is; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 64 | import static org.hamcrest.number.OrderingComparison.greaterThan; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 65 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 66 | import static org.onosproject.net.Link.Type.DIRECT; |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 67 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 68 | import static org.onosproject.net.NetTestTools.PID; |
| 69 | import static org.onosproject.net.NetTestTools.connectPoint; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Unit tests for PathIntentCompiler. |
| 73 | */ |
| 74 | public class PathIntentCompilerTest { |
| 75 | |
| 76 | private CoreService coreService; |
| 77 | private IntentExtensionService intentExtensionService; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 78 | private IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 80 | private PathIntentCompiler sut; |
| 81 | |
| 82 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 83 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 84 | private final VlanId ingressVlan = VlanId.vlanId(((short) 101)); |
| 85 | private final TrafficSelector vlanSelector = DefaultTrafficSelector.builder() |
| 86 | .matchVlanId(ingressVlan).build(); |
| 87 | private final VlanId egressVlan = VlanId.vlanId((short) 100); |
| 88 | private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment.builder() |
| 89 | .setVlanId(egressVlan).build(); |
| 90 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 91 | private final ApplicationId appId = new TestApplicationId("test"); |
| 92 | private final ProviderId pid = new ProviderId("of", "test"); |
| 93 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 94 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 95 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 96 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
| 97 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 98 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 99 | private static final int PRIORITY = 555; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 100 | |
| 101 | private final List<Link> links = Arrays.asList( |
| 102 | createEdgeLink(d1p0, true), |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 103 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 104 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 105 | createEdgeLink(d3p0, false) |
| 106 | ); |
| 107 | private final int hops = links.size() - 1; |
| 108 | private PathIntent intent; |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 109 | private PathIntent constraintVlanIntent; |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 110 | private PathIntent constrainIngressEgressVlanIntent; |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 111 | private PathIntent constraintMplsIntent; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Configures objects used in all the test cases. |
| 115 | */ |
| 116 | @Before |
| 117 | public void setUp() { |
| 118 | sut = new PathIntentCompiler(); |
| 119 | coreService = createMock(CoreService.class); |
| 120 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 121 | .andReturn(appId); |
| 122 | sut.coreService = coreService; |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 123 | sut.resourceService = new MockResourceService(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 124 | |
| 125 | Intent.bindIdGenerator(idGenerator); |
| 126 | |
| 127 | intent = PathIntent.builder() |
| 128 | .appId(APP_ID) |
| 129 | .selector(selector) |
| 130 | .treatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 131 | .priority(PRIORITY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 132 | .path(new DefaultPath(pid, links, hops)) |
| 133 | .build(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 134 | |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 135 | //Intent with VLAN encap without egress VLAN |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 136 | constraintVlanIntent = PathIntent.builder() |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 137 | .appId(APP_ID) |
| 138 | .selector(selector) |
| 139 | .treatment(treatment) |
| 140 | .priority(PRIORITY) |
| 141 | .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN))) |
| 142 | .path(new DefaultPath(pid, links, hops)) |
| 143 | .build(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 144 | |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 145 | //Intent with VLAN encap with ingress and egress VLAN |
| 146 | constrainIngressEgressVlanIntent = PathIntent.builder() |
| 147 | .appId(APP_ID) |
| 148 | .selector(vlanSelector) |
| 149 | .treatment(vlanTreatment) |
| 150 | .priority(PRIORITY) |
| 151 | .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN))) |
| 152 | .path(new DefaultPath(pid, links, hops)) |
| 153 | .build(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 154 | |
| 155 | constraintMplsIntent = PathIntent.builder() |
| 156 | .appId(APP_ID) |
| 157 | .selector(selector) |
| 158 | .treatment(treatment) |
| 159 | .priority(PRIORITY) |
| 160 | .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.MPLS))) |
| 161 | .path(new DefaultPath(pid, links, hops)) |
| 162 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 163 | intentExtensionService = createMock(IntentExtensionService.class); |
| 164 | intentExtensionService.registerCompiler(PathIntent.class, sut); |
| 165 | intentExtensionService.unregisterCompiler(PathIntent.class); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 166 | |
| 167 | registrator = new IntentConfigurableRegistrator(); |
| 168 | registrator.extensionService = intentExtensionService; |
| 169 | registrator.cfgService = new ComponentConfigAdapter(); |
| 170 | registrator.activate(); |
| 171 | |
| 172 | sut.registrator = registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 173 | |
| 174 | replay(coreService, intentExtensionService); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Tears down objects used in all the test cases. |
| 179 | */ |
| 180 | @After |
| 181 | public void tearDown() { |
| 182 | Intent.unbindIdGenerator(idGenerator); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Tests the compilation behavior of the path intent compiler. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testCompile() { |
| 190 | sut.activate(); |
| 191 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 192 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 193 | assertThat(compiled, hasSize(1)); |
| 194 | |
| 195 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 196 | |
| 197 | FlowRule rule1 = rules.stream() |
| 198 | .filter(x -> x.deviceId().equals(d1p0.deviceId())) |
| 199 | .findFirst() |
| 200 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 201 | verifyIdAndPriority(rule1, d1p0.deviceId()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 202 | assertThat(rule1.selector(), |
| 203 | is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build())); |
| 204 | assertThat(rule1.treatment(), |
| 205 | is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build())); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 206 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 207 | |
| 208 | FlowRule rule2 = rules.stream() |
| 209 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 210 | .findFirst() |
| 211 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 212 | verifyIdAndPriority(rule2, d2p0.deviceId()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 213 | assertThat(rule2.selector(), |
| 214 | is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build())); |
| 215 | assertThat(rule2.treatment(), |
| 216 | is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build())); |
| 217 | |
| 218 | FlowRule rule3 = rules.stream() |
| 219 | .filter(x -> x.deviceId().equals(d3p0.deviceId())) |
| 220 | .findFirst() |
| 221 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 222 | verifyIdAndPriority(rule3, d3p1.deviceId()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 223 | assertThat(rule3.selector(), |
| 224 | is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build())); |
| 225 | assertThat(rule3.treatment(), |
| 226 | is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 227 | |
| 228 | sut.deactivate(); |
| 229 | } |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Tests the compilation behavior of the path intent compiler in case of |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 233 | * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}. |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 234 | */ |
| 235 | @Test |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 236 | public void testVlanEncapCompile() { |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 237 | sut.activate(); |
| 238 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 239 | List<Intent> compiled = sut.compile(constraintVlanIntent, Collections.emptyList()); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 240 | assertThat(compiled, hasSize(1)); |
| 241 | |
| 242 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 243 | assertThat(rules, hasSize(3)); |
| 244 | |
| 245 | FlowRule rule1 = rules.stream() |
| 246 | .filter(x -> x.deviceId().equals(d1p0.deviceId())) |
| 247 | .findFirst() |
| 248 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 249 | verifyIdAndPriority(rule1, d1p0.deviceId()); |
| 250 | assertThat(rule1.selector(), is(DefaultTrafficSelector.builder(selector) |
| 251 | .matchInPort(d1p0.port()).build())); |
| 252 | VlanId vlanToEncap = verifyVlanEncapTreatment(rule1.treatment(), d1p1, true, false); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 253 | |
| 254 | FlowRule rule2 = rules.stream() |
| 255 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 256 | .findFirst() |
| 257 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 258 | verifyIdAndPriority(rule2, d2p0.deviceId()); |
| 259 | verifyVlanEncapSelector(rule2.selector(), d2p0, vlanToEncap); |
| 260 | verifyVlanEncapTreatment(rule2.treatment(), d2p1, false, false); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 261 | |
| 262 | FlowRule rule3 = rules.stream() |
| 263 | .filter(x -> x.deviceId().equals(d3p0.deviceId())) |
| 264 | .findFirst() |
| 265 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 266 | verifyIdAndPriority(rule3, d3p1.deviceId()); |
| 267 | verifyVlanEncapSelector(rule3.selector(), d3p1, vlanToEncap); |
| 268 | verifyVlanEncapTreatment(rule3.treatment(), d3p0, false, true); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 269 | |
| 270 | sut.deactivate(); |
| 271 | } |
| 272 | |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 273 | /** |
| 274 | * Tests the compilation behavior of the path intent compiler in case of |
| 275 | * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}. |
| 276 | * This test includes a selector to match a VLAN at the ingress and a treatment to set VLAN at the egress. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testEncapIngressEgressVlansCompile() { |
| 280 | sut.activate(); |
| 281 | |
| 282 | List<Intent> compiled = sut.compile(constrainIngressEgressVlanIntent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 283 | Collections.emptyList()); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 284 | assertThat(compiled, hasSize(1)); |
| 285 | |
| 286 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 287 | assertThat(rules, hasSize(3)); |
| 288 | |
| 289 | FlowRule rule1 = rules.stream() |
| 290 | .filter(x -> x.deviceId().equals(d1p0.deviceId())) |
| 291 | .findFirst() |
| 292 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 293 | verifyIdAndPriority(rule1, d1p0.deviceId()); |
| 294 | verifyVlanEncapSelector(rule1.selector(), d1p0, ingressVlan); |
| 295 | VlanId vlanToEncap = verifyVlanEncapTreatment(rule1.treatment(), d1p1, true, false); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 296 | |
| 297 | FlowRule rule2 = rules.stream() |
| 298 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 299 | .findFirst() |
| 300 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 301 | verifyIdAndPriority(rule2, d2p0.deviceId()); |
| 302 | verifyVlanEncapSelector(rule2.selector(), d2p0, vlanToEncap); |
| 303 | verifyVlanEncapTreatment(rule2.treatment(), d2p1, false, false); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 304 | |
| 305 | FlowRule rule3 = rules.stream() |
| 306 | .filter(x -> x.deviceId().equals(d3p0.deviceId())) |
| 307 | .findFirst() |
| 308 | .get(); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 309 | verifyIdAndPriority(rule3, d3p1.deviceId()); |
| 310 | verifyVlanEncapSelector(rule3.selector(), d3p1, vlanToEncap); |
Michele Santuari | 3ea5390 | 2016-02-15 11:21:56 +0100 | [diff] [blame] | 311 | Set<L2ModificationInstruction.ModVlanIdInstruction> vlanMod = rule3.treatment().allInstructions().stream() |
| 312 | .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction) |
| 313 | .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x) |
| 314 | .collect(Collectors.toSet()); |
| 315 | assertThat(rule3.treatment().allInstructions().stream() |
| 316 | .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction) |
| 317 | .collect(Collectors.toSet()), hasSize(1)); |
| 318 | assertThat(vlanMod.iterator().next().vlanId(), is(egressVlan)); |
| 319 | assertThat(rule3.treatment().allInstructions().stream() |
| 320 | .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction) |
| 321 | .collect(Collectors.toSet()), hasSize(0)); |
| 322 | |
| 323 | sut.deactivate(); |
| 324 | } |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 325 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 326 | private VlanId verifyVlanEncapTreatment(TrafficTreatment trafficTreatment, |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 327 | ConnectPoint egress, boolean isIngress, boolean isEgress) { |
| 328 | Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream() |
| 329 | .filter(treat -> treat instanceof Instructions.OutputInstruction) |
| 330 | .map(treat -> (Instructions.OutputInstruction) treat) |
| 331 | .collect(Collectors.toSet()); |
| 332 | assertThat(ruleOutput, hasSize(1)); |
| 333 | assertThat((ruleOutput.iterator().next()).port(), is(egress.port())); |
| 334 | VlanId vlanToEncap = VlanId.NONE; |
| 335 | if (isIngress && !isEgress) { |
| 336 | Set<L2ModificationInstruction.ModVlanIdInstruction> vlanRules = trafficTreatment.allInstructions().stream() |
| 337 | .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction) |
| 338 | .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x) |
| 339 | .collect(Collectors.toSet()); |
| 340 | assertThat(vlanRules, hasSize(1)); |
| 341 | L2ModificationInstruction.ModVlanIdInstruction vlanRule = vlanRules.iterator().next(); |
| 342 | assertThat(vlanRule.vlanId().toShort(), greaterThan((short) 0)); |
| 343 | vlanToEncap = vlanRule.vlanId(); |
| 344 | } else if (!isIngress && !isEgress) { |
| 345 | assertThat(trafficTreatment.allInstructions().stream() |
| 346 | .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction) |
| 347 | .collect(Collectors.toSet()), hasSize(0)); |
| 348 | } else { |
| 349 | assertThat(trafficTreatment.allInstructions().stream() |
| 350 | .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction) |
| 351 | .collect(Collectors.toSet()), hasSize(0)); |
| 352 | assertThat(trafficTreatment.allInstructions().stream() |
| 353 | .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction) |
| 354 | .collect(Collectors.toSet()), hasSize(1)); |
| 355 | |
| 356 | } |
| 357 | |
| 358 | return vlanToEncap; |
| 359 | |
| 360 | } |
| 361 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 362 | private void verifyVlanEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, VlanId vlanToMatch) { |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 363 | |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 364 | assertThat(trafficSelector, is(DefaultTrafficSelector.builder().matchInPort(ingress.port()) |
| 365 | .matchVlanId(vlanToMatch).build())); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Tests the compilation behavior of the path intent compiler in case of |
| 370 | * encasulation costraint {@link EncapsulationConstraint}. |
| 371 | */ |
| 372 | @Test |
| 373 | public void testMplsEncapCompile() { |
| 374 | sut.activate(); |
| 375 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 376 | List<Intent> compiled = sut.compile(constraintMplsIntent, Collections.emptyList()); |
Michele Santuari | 6096acd | 2016-02-09 17:00:37 +0100 | [diff] [blame] | 377 | assertThat(compiled, hasSize(1)); |
| 378 | |
| 379 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 380 | assertThat(rules, hasSize(3)); |
| 381 | |
| 382 | FlowRule rule1 = rules.stream() |
| 383 | .filter(x -> x.deviceId().equals(d1p0.deviceId())) |
| 384 | .findFirst() |
| 385 | .get(); |
| 386 | verifyIdAndPriority(rule1, d1p0.deviceId()); |
| 387 | assertThat(rule1.selector(), is(DefaultTrafficSelector.builder(selector) |
| 388 | .matchInPort(d1p0.port()).build())); |
| 389 | MplsLabel mplsLabelToEncap = verifyMplsEncapTreatment(rule1.treatment(), d1p1, true, false); |
| 390 | |
| 391 | FlowRule rule2 = rules.stream() |
| 392 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 393 | .findFirst() |
| 394 | .get(); |
| 395 | verifyIdAndPriority(rule2, d2p0.deviceId()); |
| 396 | verifyMplsEncapSelector(rule2.selector(), d2p0, mplsLabelToEncap); |
| 397 | verifyMplsEncapTreatment(rule2.treatment(), d2p1, false, false); |
| 398 | |
| 399 | FlowRule rule3 = rules.stream() |
| 400 | .filter(x -> x.deviceId().equals(d3p0.deviceId())) |
| 401 | .findFirst() |
| 402 | .get(); |
| 403 | verifyIdAndPriority(rule3, d3p1.deviceId()); |
| 404 | verifyMplsEncapSelector(rule3.selector(), d3p1, mplsLabelToEncap); |
| 405 | verifyMplsEncapTreatment(rule3.treatment(), d3p0, false, true); |
| 406 | |
| 407 | sut.deactivate(); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | private MplsLabel verifyMplsEncapTreatment(TrafficTreatment trafficTreatment, |
| 412 | ConnectPoint egress, boolean isIngress, boolean isEgress) { |
| 413 | Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream() |
| 414 | .filter(treat -> treat instanceof Instructions.OutputInstruction) |
| 415 | .map(treat -> (Instructions.OutputInstruction) treat) |
| 416 | .collect(Collectors.toSet()); |
| 417 | assertThat(ruleOutput, hasSize(1)); |
| 418 | assertThat((ruleOutput.iterator().next()).port(), is(egress.port())); |
| 419 | MplsLabel mplsToEncap = MplsLabel.mplsLabel(0); |
| 420 | if (isIngress && !isEgress) { |
| 421 | Set<L2ModificationInstruction.ModMplsLabelInstruction> mplsRules = |
| 422 | trafficTreatment.allInstructions().stream() |
| 423 | .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction) |
| 424 | .map(x -> (L2ModificationInstruction.ModMplsLabelInstruction) x) |
| 425 | .collect(Collectors.toSet()); |
| 426 | assertThat(mplsRules, hasSize(1)); |
| 427 | L2ModificationInstruction.ModMplsLabelInstruction mplsRule = mplsRules.iterator().next(); |
| 428 | assertThat(mplsRule.mplsLabel().toInt(), greaterThan(0)); |
| 429 | mplsToEncap = mplsRule.mplsLabel(); |
| 430 | } else if (!isIngress && !isEgress) { |
| 431 | assertThat(trafficTreatment.allInstructions().stream() |
| 432 | .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction) |
| 433 | .collect(Collectors.toSet()), hasSize(0)); |
| 434 | } else { |
| 435 | assertThat(trafficTreatment.allInstructions().stream() |
| 436 | .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction) |
| 437 | .collect(Collectors.toSet()), hasSize(0)); |
| 438 | assertThat(trafficTreatment.allInstructions().stream() |
| 439 | .filter(treat -> treat instanceof L2ModificationInstruction.PushHeaderInstructions) |
| 440 | .collect(Collectors.toSet()), hasSize(1)); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | return mplsToEncap; |
| 445 | |
| 446 | } |
| 447 | |
| 448 | private void verifyMplsEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, MplsLabel mplsLabel) { |
| 449 | |
| 450 | assertThat(trafficSelector, is(DefaultTrafficSelector.builder() |
| 451 | .matchInPort(ingress.port()).matchEthType(Ethernet.MPLS_UNICAST) |
| 452 | .matchMplsLabel(mplsLabel).build())); |
| 453 | } |
| 454 | |
| 455 | private void verifyIdAndPriority(FlowRule rule, DeviceId deviceId) { |
| 456 | assertThat(rule.deviceId(), is(deviceId)); |
| 457 | assertThat(rule.priority(), is(PRIORITY)); |
Michele Santuari | 69fc2ff | 2015-12-03 17:05:35 +0100 | [diff] [blame] | 458 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 459 | } |