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