Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 24 | import org.onosproject.TestApplicationId; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.core.IdGenerator; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultLink; |
| 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; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.criteria.Criteria; |
| 38 | import org.onosproject.net.flow.criteria.Criterion; |
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.LinkCollectionIntent; |
| 43 | import org.onosproject.net.intent.MockIdGenerator; |
| 44 | |
| 45 | import java.util.Collection; |
| 46 | import java.util.Collections; |
| 47 | import java.util.List; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 48 | import java.util.Map; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | import java.util.Set; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 50 | import java.util.stream.Collectors; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 51 | |
| 52 | import static org.easymock.EasyMock.createMock; |
| 53 | import static org.easymock.EasyMock.expect; |
| 54 | import static org.easymock.EasyMock.replay; |
| 55 | import static org.hamcrest.MatcherAssert.assertThat; |
| 56 | import static org.hamcrest.Matchers.hasSize; |
| 57 | import static org.hamcrest.Matchers.is; |
| 58 | import static org.onosproject.net.Link.Type.DIRECT; |
| 59 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 60 | import static org.onosproject.net.NetTestTools.PID; |
| 61 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 62 | |
| 63 | public class LinkCollectionIntentCompilerTest { |
| 64 | |
| 65 | private final ApplicationId appId = new TestApplicationId("test"); |
| 66 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 67 | private final ConnectPoint d1p1 = connectPoint("s1", 1); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 68 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 69 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 70 | private final ConnectPoint d2p2 = connectPoint("s2", 2); |
| 71 | private final ConnectPoint d2p3 = connectPoint("s2", 3); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 72 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 73 | private final ConnectPoint d3p2 = connectPoint("s3", 9); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 74 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 75 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 76 | private final ConnectPoint d4p1 = connectPoint("s4", 1); |
| 77 | private final ConnectPoint d4p0 = connectPoint("s4", 10); |
| 78 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | |
| 80 | private final Set<Link> links = ImmutableSet.of( |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 81 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 82 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
| 83 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 84 | |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 85 | private final Set<Link> linksMultiple = ImmutableSet.of( |
| 86 | DefaultLink.builder().providerId(PID).src(d3p1).dst(d2p0).type(DIRECT).build()); |
| 87 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 88 | private final Set<Link> linksMultiple2 = ImmutableSet.of( |
| 89 | DefaultLink.builder().providerId(PID).src(d2p0).dst(d1p1).type(DIRECT).build(), |
| 90 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
| 91 | DefaultLink.builder().providerId(PID).src(d2p2).dst(d4p1).type(DIRECT).build()); |
| 92 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 93 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 94 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 95 | |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 96 | private final VlanId ingressVlan1 = VlanId.vlanId("10"); |
| 97 | private final TrafficSelector selectorVlan1 = DefaultTrafficSelector |
| 98 | .builder() |
| 99 | .matchVlanId(ingressVlan1) |
| 100 | .build(); |
| 101 | |
| 102 | private final VlanId ingressVlan2 = VlanId.vlanId("20"); |
| 103 | private final TrafficSelector selectorVlan2 = DefaultTrafficSelector |
| 104 | .builder() |
| 105 | .matchVlanId(ingressVlan2) |
| 106 | .build(); |
| 107 | |
| 108 | private final VlanId egressVlan = VlanId.vlanId("666"); |
| 109 | private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment |
| 110 | .builder() |
| 111 | .setVlanId(egressVlan) |
| 112 | .build(); |
| 113 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 114 | private final VlanId ingressVlan = VlanId.vlanId("10"); |
| 115 | private final TrafficSelector vlanSelector = DefaultTrafficSelector |
| 116 | .builder() |
| 117 | .matchVlanId(ingressVlan) |
| 118 | .build(); |
| 119 | |
| 120 | private final VlanId egressVlan1 = VlanId.vlanId("20"); |
| 121 | private final TrafficTreatment vlanTreatment1 = DefaultTrafficTreatment |
| 122 | .builder() |
| 123 | .setVlanId(egressVlan1) |
| 124 | .build(); |
| 125 | |
| 126 | private final VlanId egressVlan2 = VlanId.vlanId("666"); |
| 127 | private final TrafficTreatment vlanTreatment2 = DefaultTrafficTreatment |
| 128 | .builder() |
| 129 | .setVlanId(egressVlan2) |
| 130 | .build(); |
| 131 | |
| 132 | private final VlanId egressVlan3 = VlanId.vlanId("69"); |
| 133 | private final TrafficTreatment vlanTreatment3 = DefaultTrafficTreatment |
| 134 | .builder() |
| 135 | .setVlanId(egressVlan3) |
| 136 | .build(); |
| 137 | |
| 138 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 139 | private CoreService coreService; |
| 140 | private IntentExtensionService intentExtensionService; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 141 | private IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 142 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 143 | |
| 144 | private LinkCollectionIntent intent; |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 145 | private LinkCollectionIntent intentMultipleSelectors; |
| 146 | private LinkCollectionIntent intentMultipleTreatments; |
| 147 | private LinkCollectionIntent intentMultipleTreatments2; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 148 | |
| 149 | private LinkCollectionIntentCompiler sut; |
| 150 | |
| 151 | @Before |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 152 | public void setUp() { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 153 | sut = new LinkCollectionIntentCompiler(); |
| 154 | coreService = createMock(CoreService.class); |
| 155 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 156 | .andReturn(appId); |
| 157 | sut.coreService = coreService; |
| 158 | |
| 159 | Intent.bindIdGenerator(idGenerator); |
| 160 | |
| 161 | intent = LinkCollectionIntent.builder() |
| 162 | .appId(APP_ID) |
| 163 | .selector(selector) |
| 164 | .treatment(treatment) |
| 165 | .links(links) |
| 166 | .ingressPoints(ImmutableSet.of(d1p1)) |
| 167 | .egressPoints(ImmutableSet.of(d3p1)) |
| 168 | .build(); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 169 | intentMultipleSelectors = LinkCollectionIntent.builder() |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 170 | .appId(APP_ID) |
| 171 | .treatment(vlanTreatment) |
| 172 | .links(linksMultiple) |
| 173 | .ingressPoints(ImmutableSet.of(d3p0, d3p2)) |
| 174 | .egressPoints(ImmutableSet.of(d2p1)) |
| 175 | .ingressSelectors(this.createIngressSelectors()) |
| 176 | .build(); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 177 | intentMultipleTreatments = LinkCollectionIntent.builder() |
| 178 | .appId(APP_ID) |
| 179 | .selector(vlanSelector) |
| 180 | .links(linksMultiple) |
| 181 | .ingressPoints(ImmutableSet.of(d3p0)) |
| 182 | .egressPoints(ImmutableSet.of(d2p1, d2p2)) |
| 183 | .egressTreatments(this.createEgressTreatments()) |
| 184 | .applyTreatmentOnEgress(true) |
| 185 | .build(); |
| 186 | intentMultipleTreatments2 = LinkCollectionIntent.builder() |
| 187 | .appId(APP_ID) |
| 188 | .selector(vlanSelector) |
| 189 | .links(linksMultiple2) |
| 190 | .ingressPoints(ImmutableSet.of(d2p3)) |
| 191 | .egressPoints(ImmutableSet.of(d1p0, d3p0, d4p0)) |
| 192 | .egressTreatments(this.createEgressTreatments2()) |
| 193 | .applyTreatmentOnEgress(true) |
| 194 | .build(); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 195 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 196 | intentExtensionService = createMock(IntentExtensionService.class); |
| 197 | intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut); |
| 198 | intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 199 | |
| 200 | registrator = new IntentConfigurableRegistrator(); |
| 201 | registrator.extensionService = intentExtensionService; |
| 202 | registrator.cfgService = new ComponentConfigAdapter(); |
| 203 | registrator.activate(); |
| 204 | |
| 205 | sut.registrator = registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 206 | |
| 207 | replay(coreService, intentExtensionService); |
| 208 | } |
| 209 | |
| 210 | @After |
| 211 | public void tearDown() { |
| 212 | Intent.unbindIdGenerator(idGenerator); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testCompile() { |
| 217 | sut.activate(); |
| 218 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 219 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 220 | assertThat(compiled, hasSize(1)); |
| 221 | |
| 222 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 223 | assertThat(rules, hasSize(links.size())); |
| 224 | |
| 225 | // if not found, get() raises an exception |
| 226 | FlowRule rule1 = rules.stream() |
| 227 | .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) |
| 228 | .findFirst() |
| 229 | .get(); |
| 230 | assertThat(rule1.selector(), is( |
| 231 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build() |
| 232 | )); |
| 233 | assertThat(rule1.treatment(), is( |
| 234 | DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build() |
| 235 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 236 | assertThat(rule1.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 237 | |
| 238 | FlowRule rule2 = rules.stream() |
| 239 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 240 | .findFirst() |
| 241 | .get(); |
| 242 | assertThat(rule2.selector(), is( |
| 243 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build() |
| 244 | )); |
| 245 | assertThat(rule2.treatment(), is( |
| 246 | DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build() |
| 247 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 248 | assertThat(rule2.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 249 | |
| 250 | FlowRule rule3 = rules.stream() |
| 251 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 252 | .findFirst() |
| 253 | .get(); |
| 254 | assertThat(rule3.selector(), is( |
| 255 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build() |
| 256 | )); |
| 257 | assertThat(rule3.treatment(), is( |
| 258 | DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build() |
| 259 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 260 | assertThat(rule3.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 261 | |
| 262 | sut.deactivate(); |
| 263 | } |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 264 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 265 | @Test |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 266 | public void testCompileMultipleSelectors() { |
| 267 | sut.activate(); |
| 268 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 269 | List<Intent> compiled = sut.compile(intentMultipleSelectors, Collections.emptyList()); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 270 | assertThat(compiled, hasSize(1)); |
| 271 | |
| 272 | |
| 273 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 274 | assertThat(rules, hasSize((linksMultiple.size()) + intentMultipleSelectors.ingressPoints().size())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 275 | |
| 276 | Set<FlowRule> d3Rules = rules |
| 277 | .parallelStream() |
| 278 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 279 | .collect(Collectors.toSet()); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 280 | assertThat(d3Rules, hasSize(intentMultipleSelectors.ingressPoints().size())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 281 | |
| 282 | FlowRule rule1 = rules.stream() |
| 283 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId()) |
| 284 | && |
| 285 | rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p0.port()))) |
| 286 | .findFirst() |
| 287 | .get(); |
| 288 | assertThat(rule1.selector(), is( |
| 289 | DefaultTrafficSelector |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 290 | .builder(intentMultipleSelectors.selector()) |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 291 | .matchInPort(d3p0.port()) |
| 292 | .matchVlanId(ingressVlan1) |
| 293 | .build() |
| 294 | )); |
| 295 | assertThat(rule1.treatment(), is( |
| 296 | DefaultTrafficTreatment |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 297 | .builder(intentMultipleSelectors.treatment()) |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 298 | .setOutput(d3p1.port()) |
| 299 | .build() |
| 300 | )); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 301 | assertThat(rule1.priority(), is(intentMultipleSelectors.priority())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 302 | |
| 303 | FlowRule rule2 = rules.stream() |
| 304 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId()) |
| 305 | && |
| 306 | rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p2.port()))) |
| 307 | .findFirst() |
| 308 | .get(); |
| 309 | assertThat(rule2.selector(), is( |
| 310 | DefaultTrafficSelector |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 311 | .builder(intentMultipleSelectors.selector()) |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 312 | .matchInPort(d3p2.port()) |
| 313 | .matchVlanId(ingressVlan2) |
| 314 | .build() |
| 315 | )); |
| 316 | assertThat(rule2.treatment(), is( |
| 317 | DefaultTrafficTreatment |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 318 | .builder(intentMultipleSelectors.treatment()) |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 319 | .setOutput(d3p1.port()) |
| 320 | .build() |
| 321 | )); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 322 | assertThat(rule2.priority(), is(intentMultipleSelectors.priority())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 323 | |
| 324 | Set<FlowRule> d2Rules = rules |
| 325 | .parallelStream() |
| 326 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 327 | .collect(Collectors.toSet()); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 328 | assertThat(d2Rules, hasSize(intentMultipleSelectors.egressPoints().size())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 329 | |
| 330 | // We do not need in_port filter |
| 331 | FlowRule rule3 = rules.stream() |
| 332 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 333 | .findFirst() |
| 334 | .get(); |
| 335 | assertThat(rule3.selector(), is( |
| 336 | DefaultTrafficSelector |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 337 | .builder(intentMultipleSelectors.selector()) |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 338 | .matchInPort(d2p0.port()) |
| 339 | .matchVlanId(egressVlan) |
| 340 | .build() |
| 341 | )); |
| 342 | assertThat(rule3.treatment(), is( |
| 343 | DefaultTrafficTreatment |
| 344 | .builder() |
| 345 | .setOutput(d2p1.port()) |
| 346 | .build() |
| 347 | )); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 348 | assertThat(rule3.priority(), is(intentMultipleSelectors.priority())); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 349 | |
| 350 | sut.deactivate(); |
| 351 | } |
| 352 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 353 | @Test |
| 354 | public void testCompileMultipleTreatments() { |
| 355 | sut.activate(); |
| 356 | |
| 357 | List<Intent> compiled = sut.compile(intentMultipleTreatments, Collections.emptyList()); |
| 358 | assertThat(compiled, hasSize(1)); |
| 359 | |
| 360 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
Yi Tseng | 0e251b3 | 2016-09-20 11:08:32 -0700 | [diff] [blame^] | 361 | assertThat(rules, hasSize(2)); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 362 | |
| 363 | Set<FlowRule> d3Rules = rules |
| 364 | .parallelStream() |
| 365 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 366 | .collect(Collectors.toSet()); |
| 367 | assertThat(d3Rules, hasSize(intentMultipleTreatments.ingressPoints().size())); |
| 368 | |
| 369 | FlowRule rule1 = rules.stream() |
| 370 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 371 | .findFirst() |
| 372 | .get(); |
| 373 | assertThat(rule1.selector(), is( |
| 374 | DefaultTrafficSelector |
| 375 | .builder(intentMultipleTreatments.selector()) |
| 376 | .matchInPort(d3p0.port()) |
| 377 | .matchVlanId(ingressVlan) |
| 378 | .build() |
| 379 | )); |
| 380 | assertThat(rule1.treatment(), is( |
| 381 | DefaultTrafficTreatment |
| 382 | .builder(intentMultipleTreatments.treatment()) |
| 383 | .setOutput(d3p1.port()) |
| 384 | .build() |
| 385 | )); |
| 386 | assertThat(rule1.priority(), is(intentMultipleTreatments.priority())); |
| 387 | |
| 388 | Set<FlowRule> d2Rules = rules |
| 389 | .parallelStream() |
| 390 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 391 | .collect(Collectors.toSet()); |
| 392 | assertThat(d2Rules, hasSize(1)); |
| 393 | |
| 394 | FlowRule rule2 = rules.stream() |
| 395 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 396 | .findFirst() |
| 397 | .get(); |
| 398 | assertThat(rule2.selector(), is( |
| 399 | DefaultTrafficSelector |
| 400 | .builder(intentMultipleTreatments.selector()) |
| 401 | .matchInPort(d2p0.port()) |
| 402 | .matchVlanId(ingressVlan) |
| 403 | .build() |
| 404 | )); |
| 405 | assertThat(rule2.treatment(), is( |
| 406 | DefaultTrafficTreatment |
| 407 | .builder(intentMultipleTreatments.treatment()) |
| 408 | .setVlanId(egressVlan1) |
| 409 | .setOutput(d2p1.port()) |
| 410 | .setVlanId(egressVlan2) |
| 411 | .setOutput(d2p2.port()) |
| 412 | .build() |
| 413 | )); |
| 414 | assertThat(rule2.priority(), is(intentMultipleTreatments.priority())); |
| 415 | |
| 416 | } |
| 417 | |
| 418 | @Test |
| 419 | public void testCompileMultipleTreatments2() { |
| 420 | sut.activate(); |
| 421 | |
| 422 | List<Intent> compiled = sut.compile(intentMultipleTreatments2, Collections.emptyList()); |
| 423 | assertThat(compiled, hasSize(1)); |
| 424 | |
| 425 | |
| 426 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
Yi Tseng | 0e251b3 | 2016-09-20 11:08:32 -0700 | [diff] [blame^] | 427 | assertThat(rules, hasSize(4)); |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 428 | |
| 429 | |
| 430 | Set<FlowRule> d2Rules = rules |
| 431 | .parallelStream() |
| 432 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 433 | .collect(Collectors.toSet()); |
| 434 | assertThat(d2Rules, hasSize(1)); |
| 435 | |
| 436 | |
| 437 | FlowRule rule1 = rules.stream() |
| 438 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 439 | .findFirst() |
| 440 | .get(); |
| 441 | assertThat(rule1.selector(), is( |
| 442 | DefaultTrafficSelector |
| 443 | .builder(intentMultipleTreatments.selector()) |
| 444 | .matchInPort(d2p3.port()) |
| 445 | .matchVlanId(ingressVlan) |
| 446 | .build() |
| 447 | )); |
| 448 | assertThat(rule1.treatment(), is( |
| 449 | DefaultTrafficTreatment |
| 450 | .builder(intentMultipleTreatments.treatment()) |
| 451 | .setOutput(d2p0.port()) |
| 452 | .setOutput(d2p1.port()) |
| 453 | .setOutput(d2p2.port()) |
| 454 | .build() |
| 455 | )); |
| 456 | assertThat(rule1.priority(), is(intentMultipleTreatments.priority())); |
| 457 | |
| 458 | Set<FlowRule> d1Rules = rules |
| 459 | .parallelStream() |
| 460 | .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) |
| 461 | .collect(Collectors.toSet()); |
| 462 | assertThat(d1Rules, hasSize(1)); |
| 463 | |
| 464 | FlowRule rule2 = rules.stream() |
| 465 | .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) |
| 466 | .findFirst() |
| 467 | .get(); |
| 468 | assertThat(rule2.selector(), is( |
| 469 | DefaultTrafficSelector |
| 470 | .builder(intentMultipleTreatments2.selector()) |
| 471 | .matchInPort(d1p1.port()) |
| 472 | .matchVlanId(ingressVlan) |
| 473 | .build() |
| 474 | )); |
| 475 | assertThat(rule2.treatment(), is( |
| 476 | DefaultTrafficTreatment |
| 477 | .builder(intentMultipleTreatments2.treatment()) |
| 478 | .setVlanId(egressVlan1) |
| 479 | .setOutput(d1p0.port()) |
| 480 | .build() |
| 481 | )); |
| 482 | assertThat(rule2.priority(), is(intentMultipleTreatments.priority())); |
| 483 | |
| 484 | Set<FlowRule> d3Rules = rules |
| 485 | .parallelStream() |
| 486 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 487 | .collect(Collectors.toSet()); |
| 488 | assertThat(d3Rules, hasSize(1)); |
| 489 | |
| 490 | FlowRule rule3 = rules.stream() |
| 491 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 492 | .findFirst() |
| 493 | .get(); |
| 494 | assertThat(rule3.selector(), is( |
| 495 | DefaultTrafficSelector |
| 496 | .builder(intentMultipleTreatments2.selector()) |
| 497 | .matchInPort(d3p1.port()) |
| 498 | .matchVlanId(ingressVlan) |
| 499 | .build() |
| 500 | )); |
| 501 | assertThat(rule3.treatment(), is( |
| 502 | DefaultTrafficTreatment |
| 503 | .builder(intentMultipleTreatments2.treatment()) |
| 504 | .setVlanId(egressVlan2) |
| 505 | .setOutput(d3p0.port()) |
| 506 | .build() |
| 507 | )); |
| 508 | assertThat(rule3.priority(), is(intentMultipleTreatments.priority())); |
| 509 | |
| 510 | Set<FlowRule> d4Rules = rules |
| 511 | .parallelStream() |
| 512 | .filter(rule -> rule.deviceId().equals(d4p0.deviceId())) |
| 513 | .collect(Collectors.toSet()); |
| 514 | assertThat(d4Rules, hasSize(1)); |
| 515 | |
| 516 | FlowRule rule4 = rules.stream() |
| 517 | .filter(rule -> rule.deviceId().equals(d4p0.deviceId())) |
| 518 | .findFirst() |
| 519 | .get(); |
| 520 | assertThat(rule4.selector(), is( |
| 521 | DefaultTrafficSelector |
| 522 | .builder(intentMultipleTreatments2.selector()) |
| 523 | .matchInPort(d4p1.port()) |
| 524 | .matchVlanId(ingressVlan) |
| 525 | .build() |
| 526 | )); |
| 527 | assertThat(rule4.treatment(), is( |
| 528 | DefaultTrafficTreatment |
| 529 | .builder(intentMultipleTreatments2.treatment()) |
| 530 | .setVlanId(egressVlan3) |
| 531 | .setOutput(d4p0.port()) |
| 532 | .build() |
| 533 | )); |
| 534 | assertThat(rule4.priority(), is(intentMultipleTreatments.priority())); |
| 535 | |
| 536 | } |
| 537 | |
| 538 | public Map<ConnectPoint, TrafficTreatment> createEgressTreatments() { |
| 539 | Map<ConnectPoint, TrafficTreatment> mapToReturn = Maps.newHashMap(); |
| 540 | mapToReturn.put(d2p1, vlanTreatment1); |
| 541 | mapToReturn.put(d2p2, vlanTreatment2); |
| 542 | return mapToReturn; |
| 543 | } |
| 544 | |
| 545 | public Map<ConnectPoint, TrafficTreatment> createEgressTreatments2() { |
| 546 | Map<ConnectPoint, TrafficTreatment> mapToReturn = Maps.newHashMap(); |
| 547 | mapToReturn.put(d1p0, vlanTreatment1); |
| 548 | mapToReturn.put(d3p0, vlanTreatment2); |
| 549 | mapToReturn.put(d4p0, vlanTreatment3); |
| 550 | return mapToReturn; |
| 551 | } |
| 552 | |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame] | 553 | public Map<ConnectPoint, TrafficSelector> createIngressSelectors() { |
| 554 | Map<ConnectPoint, TrafficSelector> mapToReturn = Maps.newHashMap(); |
| 555 | mapToReturn.put(d3p0, selectorVlan1); |
| 556 | mapToReturn.put(d3p2, selectorVlan2); |
| 557 | return mapToReturn; |
| 558 | } |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 559 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 560 | } |