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 | |
| 67 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 68 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 69 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 70 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 71 | private final ConnectPoint d3p2 = connectPoint("s3", 9); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 72 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 73 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
| 74 | |
| 75 | private final Set<Link> links = ImmutableSet.of( |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 76 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 77 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
| 78 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 80 | private final Set<Link> linksMultiple = ImmutableSet.of( |
| 81 | DefaultLink.builder().providerId(PID).src(d3p1).dst(d2p0).type(DIRECT).build()); |
| 82 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 83 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 84 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 85 | |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 86 | private final VlanId ingressVlan1 = VlanId.vlanId("10"); |
| 87 | private final TrafficSelector selectorVlan1 = DefaultTrafficSelector |
| 88 | .builder() |
| 89 | .matchVlanId(ingressVlan1) |
| 90 | .build(); |
| 91 | |
| 92 | private final VlanId ingressVlan2 = VlanId.vlanId("20"); |
| 93 | private final TrafficSelector selectorVlan2 = DefaultTrafficSelector |
| 94 | .builder() |
| 95 | .matchVlanId(ingressVlan2) |
| 96 | .build(); |
| 97 | |
| 98 | private final VlanId egressVlan = VlanId.vlanId("666"); |
| 99 | private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment |
| 100 | .builder() |
| 101 | .setVlanId(egressVlan) |
| 102 | .build(); |
| 103 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 104 | private CoreService coreService; |
| 105 | private IntentExtensionService intentExtensionService; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 106 | private IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 107 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 108 | |
| 109 | private LinkCollectionIntent intent; |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 110 | private LinkCollectionIntent intentMultiple; |
| 111 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 112 | |
| 113 | private LinkCollectionIntentCompiler sut; |
| 114 | |
| 115 | @Before |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 116 | public void setUp() { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 117 | sut = new LinkCollectionIntentCompiler(); |
| 118 | coreService = createMock(CoreService.class); |
| 119 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 120 | .andReturn(appId); |
| 121 | sut.coreService = coreService; |
| 122 | |
| 123 | Intent.bindIdGenerator(idGenerator); |
| 124 | |
| 125 | intent = LinkCollectionIntent.builder() |
| 126 | .appId(APP_ID) |
| 127 | .selector(selector) |
| 128 | .treatment(treatment) |
| 129 | .links(links) |
| 130 | .ingressPoints(ImmutableSet.of(d1p1)) |
| 131 | .egressPoints(ImmutableSet.of(d3p1)) |
| 132 | .build(); |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 133 | intentMultiple = LinkCollectionIntent.builder() |
| 134 | .appId(APP_ID) |
| 135 | .treatment(vlanTreatment) |
| 136 | .links(linksMultiple) |
| 137 | .ingressPoints(ImmutableSet.of(d3p0, d3p2)) |
| 138 | .egressPoints(ImmutableSet.of(d2p1)) |
| 139 | .ingressSelectors(this.createIngressSelectors()) |
| 140 | .build(); |
| 141 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 142 | intentExtensionService = createMock(IntentExtensionService.class); |
| 143 | intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut); |
| 144 | intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 145 | |
| 146 | registrator = new IntentConfigurableRegistrator(); |
| 147 | registrator.extensionService = intentExtensionService; |
| 148 | registrator.cfgService = new ComponentConfigAdapter(); |
| 149 | registrator.activate(); |
| 150 | |
| 151 | sut.registrator = registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 152 | |
| 153 | replay(coreService, intentExtensionService); |
| 154 | } |
| 155 | |
| 156 | @After |
| 157 | public void tearDown() { |
| 158 | Intent.unbindIdGenerator(idGenerator); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testCompile() { |
| 163 | sut.activate(); |
| 164 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 165 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 166 | assertThat(compiled, hasSize(1)); |
| 167 | |
| 168 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 169 | assertThat(rules, hasSize(links.size())); |
| 170 | |
| 171 | // if not found, get() raises an exception |
| 172 | FlowRule rule1 = rules.stream() |
| 173 | .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) |
| 174 | .findFirst() |
| 175 | .get(); |
| 176 | assertThat(rule1.selector(), is( |
| 177 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build() |
| 178 | )); |
| 179 | assertThat(rule1.treatment(), is( |
| 180 | DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build() |
| 181 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 182 | assertThat(rule1.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 183 | |
| 184 | FlowRule rule2 = rules.stream() |
| 185 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 186 | .findFirst() |
| 187 | .get(); |
| 188 | assertThat(rule2.selector(), is( |
| 189 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build() |
| 190 | )); |
| 191 | assertThat(rule2.treatment(), is( |
| 192 | DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build() |
| 193 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 194 | assertThat(rule2.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 195 | |
| 196 | FlowRule rule3 = rules.stream() |
| 197 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 198 | .findFirst() |
| 199 | .get(); |
| 200 | assertThat(rule3.selector(), is( |
| 201 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build() |
| 202 | )); |
| 203 | assertThat(rule3.treatment(), is( |
| 204 | DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build() |
| 205 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 206 | assertThat(rule3.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 207 | |
| 208 | sut.deactivate(); |
| 209 | } |
Pier Ventre | 647138f | 2016-08-26 17:32:44 -0700 | [diff] [blame^] | 210 | |
| 211 | @Test |
| 212 | public void testCompileMultipleSelectors() { |
| 213 | sut.activate(); |
| 214 | |
| 215 | List<Intent> compiled = sut.compile(intentMultiple, Collections.emptyList()); |
| 216 | assertThat(compiled, hasSize(1)); |
| 217 | |
| 218 | |
| 219 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 220 | assertThat(rules, hasSize((linksMultiple.size()) + intentMultiple.ingressPoints().size())); |
| 221 | |
| 222 | Set<FlowRule> d3Rules = rules |
| 223 | .parallelStream() |
| 224 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 225 | .collect(Collectors.toSet()); |
| 226 | assertThat(d3Rules, hasSize(intentMultiple.ingressPoints().size())); |
| 227 | |
| 228 | FlowRule rule1 = rules.stream() |
| 229 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId()) |
| 230 | && |
| 231 | rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p0.port()))) |
| 232 | .findFirst() |
| 233 | .get(); |
| 234 | assertThat(rule1.selector(), is( |
| 235 | DefaultTrafficSelector |
| 236 | .builder(intentMultiple.selector()) |
| 237 | .matchInPort(d3p0.port()) |
| 238 | .matchVlanId(ingressVlan1) |
| 239 | .build() |
| 240 | )); |
| 241 | assertThat(rule1.treatment(), is( |
| 242 | DefaultTrafficTreatment |
| 243 | .builder(intentMultiple.treatment()) |
| 244 | .setOutput(d3p1.port()) |
| 245 | .build() |
| 246 | )); |
| 247 | assertThat(rule1.priority(), is(intentMultiple.priority())); |
| 248 | |
| 249 | FlowRule rule2 = rules.stream() |
| 250 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId()) |
| 251 | && |
| 252 | rule.selector().getCriterion(Criterion.Type.IN_PORT).equals(Criteria.matchInPort(d3p2.port()))) |
| 253 | .findFirst() |
| 254 | .get(); |
| 255 | assertThat(rule2.selector(), is( |
| 256 | DefaultTrafficSelector |
| 257 | .builder(intentMultiple.selector()) |
| 258 | .matchInPort(d3p2.port()) |
| 259 | .matchVlanId(ingressVlan2) |
| 260 | .build() |
| 261 | )); |
| 262 | assertThat(rule2.treatment(), is( |
| 263 | DefaultTrafficTreatment |
| 264 | .builder(intentMultiple.treatment()) |
| 265 | .setOutput(d3p1.port()) |
| 266 | .build() |
| 267 | )); |
| 268 | assertThat(rule1.priority(), is(intentMultiple.priority())); |
| 269 | |
| 270 | Set<FlowRule> d2Rules = rules |
| 271 | .parallelStream() |
| 272 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 273 | .collect(Collectors.toSet()); |
| 274 | assertThat(d2Rules, hasSize(intentMultiple.egressPoints().size())); |
| 275 | |
| 276 | // We do not need in_port filter |
| 277 | FlowRule rule3 = rules.stream() |
| 278 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 279 | .findFirst() |
| 280 | .get(); |
| 281 | assertThat(rule3.selector(), is( |
| 282 | DefaultTrafficSelector |
| 283 | .builder(intentMultiple.selector()) |
| 284 | .matchInPort(d2p0.port()) |
| 285 | .matchVlanId(egressVlan) |
| 286 | .build() |
| 287 | )); |
| 288 | assertThat(rule3.treatment(), is( |
| 289 | DefaultTrafficTreatment |
| 290 | .builder() |
| 291 | .setOutput(d2p1.port()) |
| 292 | .build() |
| 293 | )); |
| 294 | assertThat(rule3.priority(), is(intentMultiple.priority())); |
| 295 | |
| 296 | sut.deactivate(); |
| 297 | } |
| 298 | |
| 299 | public Map<ConnectPoint, TrafficSelector> createIngressSelectors() { |
| 300 | Map<ConnectPoint, TrafficSelector> mapToReturn = Maps.newHashMap(); |
| 301 | mapToReturn.put(d3p0, selectorVlan1); |
| 302 | mapToReturn.put(d3p2, selectorVlan2); |
| 303 | return mapToReturn; |
| 304 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 305 | } |