Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [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 | |
| 17 | package org.onosproject.net.intent.impl.compiler; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.FilteredConnectPoint; |
| 27 | import org.onosproject.net.domain.DomainPointToPointIntent; |
| 28 | import org.onosproject.net.domain.DomainService; |
| 29 | import org.onosproject.net.flow.FlowRule; |
| 30 | import org.onosproject.net.intent.Constraint; |
| 31 | import org.onosproject.net.intent.FlowRuleIntent; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentExtensionService; |
| 34 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 35 | import org.onosproject.net.intent.constraint.DomainConstraint; |
| 36 | import org.onosproject.net.resource.MockResourceService; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
| 40 | import java.util.List; |
| 41 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 42 | import static org.easymock.EasyMock.*; |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 43 | import static org.hamcrest.MatcherAssert.assertThat; |
| 44 | import static org.hamcrest.Matchers.equalTo; |
| 45 | import static org.hamcrest.Matchers.hasSize; |
| 46 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 47 | import static org.onosproject.net.NetTestTools.link; |
| 48 | import static org.onosproject.net.domain.DomainId.LOCAL; |
| 49 | |
| 50 | /** |
| 51 | * Those tests verify the compilation process with domains included in the path. |
| 52 | */ |
| 53 | public class LinkCollectionIntentCompilerDomainP2PTest extends AbstractLinkCollectionTest { |
| 54 | |
| 55 | private static List<Constraint> domainConstraint = |
| 56 | ImmutableList.of(DomainConstraint.domain()); |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | sut = new LinkCollectionIntentCompiler(); |
| 61 | coreService = createMock(CoreService.class); |
| 62 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 63 | .andReturn(appId); |
| 64 | sut.coreService = coreService; |
| 65 | |
| 66 | // defining the domain assignments |
| 67 | domainService = createMock(DomainService.class); |
| 68 | expect(domainService.getDomain(d1Id)).andReturn(LOCAL).anyTimes(); |
| 69 | expect(domainService.getDomain(d2Id)).andReturn(domain).anyTimes(); |
| 70 | expect(domainService.getDomain(d4Id)).andReturn(domain).anyTimes(); |
| 71 | expect(domainService.getDomain(d3Id)).andReturn(LOCAL).anyTimes(); |
| 72 | sut.domainService = domainService; |
| 73 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 74 | super.setUp(); |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 75 | |
| 76 | intentExtensionService = createMock(IntentExtensionService.class); |
| 77 | intentExtensionService |
| 78 | .registerCompiler(LinkCollectionIntent.class, sut); |
| 79 | intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); |
| 80 | |
| 81 | registrator = new IntentConfigurableRegistrator(); |
| 82 | registrator.extensionService = intentExtensionService; |
| 83 | registrator.cfgService = new ComponentConfigAdapter(); |
| 84 | registrator.activate(); |
| 85 | |
| 86 | sut.registrator = registrator; |
| 87 | sut.resourceService = new MockResourceService(); |
| 88 | |
| 89 | LinkCollectionCompiler.optimizeInstructions = false; |
| 90 | LinkCollectionCompiler.copyTtl = false; |
| 91 | |
| 92 | replay(coreService, domainService, intentExtensionService); |
| 93 | } |
| 94 | |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 95 | /** |
| 96 | * We test the proper compilation of one domain device. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testCompilationSingleDeviceDomainP2P() { |
| 100 | |
| 101 | intent = LinkCollectionIntent.builder() |
| 102 | .appId(APP_ID) |
| 103 | .selector(selector) |
| 104 | .treatment(treatment) |
| 105 | .applyTreatmentOnEgress(true) |
| 106 | .links(p2pLinks) |
| 107 | .filteredIngressPoints(ImmutableSet.of( |
| 108 | new FilteredConnectPoint(d1p10) |
| 109 | )) |
| 110 | .filteredEgressPoints(ImmutableSet.of( |
| 111 | new FilteredConnectPoint(d3p10) |
| 112 | )) |
| 113 | .constraints(domainConstraint) |
| 114 | .build(); |
| 115 | |
| 116 | sut.activate(); |
| 117 | |
| 118 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
| 119 | assertThat(compiled, hasSize(2)); |
| 120 | |
| 121 | DomainPointToPointIntent domainIntent = |
| 122 | ((DomainPointToPointIntent) compiled.get(0)); |
| 123 | ConnectPoint ingress = |
| 124 | domainIntent.filteredIngressPoints().iterator().next() |
| 125 | .connectPoint(); |
| 126 | assertThat(ingress, equalTo(d2p0)); |
| 127 | ConnectPoint egress = |
| 128 | domainIntent.filteredEgressPoints().iterator().next() |
| 129 | .connectPoint(); |
| 130 | assertThat(egress, equalTo(d2p1)); |
| 131 | assertThat(domainIntent.links(), hasSize(0)); |
| 132 | |
| 133 | Collection<FlowRule> rules = |
| 134 | ((FlowRuleIntent) compiled.get(1)).flowRules(); |
| 135 | assertThat(rules, hasSize(2)); |
| 136 | |
| 137 | sut.deactivate(); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * We test the proper compilation of a domain with two devices. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testCompilationMultiHopDomainP2P() { |
| 146 | |
| 147 | intent = LinkCollectionIntent.builder() |
| 148 | .appId(APP_ID) |
| 149 | .selector(selector) |
| 150 | .treatment(treatment) |
| 151 | .applyTreatmentOnEgress(true) |
| 152 | .links(domainP2Plinks) |
| 153 | .filteredIngressPoints(ImmutableSet.of( |
| 154 | new FilteredConnectPoint(d1p10) |
| 155 | )) |
| 156 | .filteredEgressPoints(ImmutableSet.of( |
| 157 | new FilteredConnectPoint(d3p10) |
| 158 | )) |
| 159 | .constraints(domainConstraint) |
| 160 | .build(); |
| 161 | |
| 162 | sut.activate(); |
| 163 | |
| 164 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
| 165 | assertThat(compiled, hasSize(2)); |
| 166 | |
| 167 | DomainPointToPointIntent domainIntent = |
| 168 | ((DomainPointToPointIntent) compiled.get(0)); |
| 169 | ConnectPoint ingress = |
| 170 | domainIntent.filteredIngressPoints().iterator().next() |
| 171 | .connectPoint(); |
| 172 | assertThat(ingress, equalTo(d2p0)); |
| 173 | ConnectPoint egress = |
| 174 | domainIntent.filteredEgressPoints().iterator().next() |
| 175 | .connectPoint(); |
| 176 | assertThat(egress, equalTo(d4p0)); |
| 177 | assertThat(domainIntent.links(), hasSize(1)); |
| 178 | |
| 179 | Collection<FlowRule> rules = |
| 180 | ((FlowRuleIntent) compiled.get(1)).flowRules(); |
| 181 | assertThat(rules, hasSize(2)); |
| 182 | |
| 183 | sut.deactivate(); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * We test the proper compilation of a domain starting with a domain device. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testCompilationDomainStartP2P() { |
| 193 | |
| 194 | intent = LinkCollectionIntent.builder() |
| 195 | .appId(APP_ID) |
| 196 | .selector(selector) |
| 197 | .treatment(treatment) |
| 198 | .applyTreatmentOnEgress(true) |
| 199 | .links(ImmutableSet.of( |
| 200 | link(d2p0, d1p0) |
| 201 | )) |
| 202 | .filteredIngressPoints(ImmutableSet.of( |
| 203 | new FilteredConnectPoint(d2p10) |
| 204 | )) |
| 205 | .filteredEgressPoints(ImmutableSet.of( |
| 206 | new FilteredConnectPoint(d1p10) |
| 207 | )) |
| 208 | .constraints(domainConstraint) |
| 209 | .build(); |
| 210 | |
| 211 | sut.activate(); |
| 212 | |
| 213 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
| 214 | assertThat(compiled, hasSize(2)); |
| 215 | |
| 216 | DomainPointToPointIntent domainIntent = |
| 217 | ((DomainPointToPointIntent) compiled.get(0)); |
| 218 | ConnectPoint ingress = |
| 219 | domainIntent.filteredIngressPoints().iterator().next() |
| 220 | .connectPoint(); |
| 221 | assertThat(ingress, equalTo(d2p10)); |
| 222 | ConnectPoint egress = |
| 223 | domainIntent.filteredEgressPoints().iterator().next() |
| 224 | .connectPoint(); |
| 225 | assertThat(egress, equalTo(d2p0)); |
| 226 | assertThat(domainIntent.links(), hasSize(0)); |
| 227 | |
| 228 | Collection<FlowRule> rules = |
| 229 | ((FlowRuleIntent) compiled.get(1)).flowRules(); |
| 230 | assertThat(rules, hasSize(1)); |
| 231 | |
| 232 | sut.deactivate(); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * We test the proper compilation of a domain ending with a domain device. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testCompilationDomainEndP2P() { |
| 241 | |
| 242 | intent = LinkCollectionIntent.builder() |
| 243 | .appId(APP_ID) |
| 244 | .selector(selector) |
| 245 | .treatment(treatment) |
| 246 | .applyTreatmentOnEgress(true) |
| 247 | .links(ImmutableSet.of( |
| 248 | link(d1p0, d2p0) |
| 249 | )) |
| 250 | .filteredIngressPoints(ImmutableSet.of( |
| 251 | new FilteredConnectPoint(d1p10) |
| 252 | )) |
| 253 | .filteredEgressPoints(ImmutableSet.of( |
| 254 | new FilteredConnectPoint(d2p10) |
| 255 | )) |
| 256 | .constraints(domainConstraint) |
| 257 | .build(); |
| 258 | |
| 259 | sut.activate(); |
| 260 | |
| 261 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
| 262 | assertThat(compiled, hasSize(2)); |
| 263 | |
| 264 | DomainPointToPointIntent domainIntent = |
| 265 | ((DomainPointToPointIntent) compiled.get(0)); |
| 266 | ConnectPoint ingress = |
| 267 | domainIntent.filteredIngressPoints().iterator().next() |
| 268 | .connectPoint(); |
| 269 | assertThat(ingress, equalTo(d2p0)); |
| 270 | ConnectPoint egress = |
| 271 | domainIntent.filteredEgressPoints().iterator().next() |
| 272 | .connectPoint(); |
| 273 | assertThat(egress, equalTo(d2p10)); |
| 274 | assertThat(domainIntent.links(), hasSize(0)); |
| 275 | |
| 276 | Collection<FlowRule> rules = |
| 277 | ((FlowRuleIntent) compiled.get(1)).flowRules(); |
| 278 | assertThat(rules, hasSize(1)); |
| 279 | |
| 280 | sut.deactivate(); |
| 281 | |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * We test the proper compilation of a path fully inside of a domain. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testCompilationDomainFullP2P() { |
| 289 | |
| 290 | intent = LinkCollectionIntent.builder() |
| 291 | .appId(APP_ID) |
| 292 | .selector(selector) |
| 293 | .treatment(treatment) |
| 294 | .applyTreatmentOnEgress(true) |
| 295 | .links(ImmutableSet.of( |
| 296 | link(d2p0, d4p0) |
| 297 | )) |
| 298 | .filteredIngressPoints(ImmutableSet.of( |
| 299 | new FilteredConnectPoint(d2p10) |
| 300 | )) |
| 301 | .filteredEgressPoints(ImmutableSet.of( |
| 302 | new FilteredConnectPoint(d4p10) |
| 303 | )) |
| 304 | .constraints(domainConstraint) |
| 305 | .build(); |
| 306 | |
| 307 | sut.activate(); |
| 308 | |
| 309 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
| 310 | assertThat(compiled, hasSize(1)); |
| 311 | |
| 312 | DomainPointToPointIntent domainIntent = |
| 313 | ((DomainPointToPointIntent) compiled.get(0)); |
| 314 | ConnectPoint ingress = |
| 315 | domainIntent.filteredIngressPoints().iterator().next() |
| 316 | .connectPoint(); |
| 317 | assertThat(ingress, equalTo(d2p10)); |
| 318 | ConnectPoint egress = |
| 319 | domainIntent.filteredEgressPoints().iterator().next() |
| 320 | .connectPoint(); |
| 321 | assertThat(egress, equalTo(d4p10)); |
| 322 | assertThat(domainIntent.links(), hasSize(1)); |
| 323 | |
| 324 | sut.deactivate(); |
| 325 | |
| 326 | } |
| 327 | |
| 328 | } |