blob: 8e89d149bd7346c0b32a9c69d48ea40c1547062e [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import com.google.common.collect.ImmutableSet;
Pier Ventre647138f2016-08-26 17:32:44 -070019import com.google.common.collect.Maps;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Pier Ventre647138f2016-08-26 17:32:44 -070023import org.onlab.packet.VlanId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080024import org.onosproject.TestApplicationId;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080025import org.onosproject.cfg.ComponentConfigAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.core.IdGenerator;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DefaultLink;
31import org.onosproject.net.Link;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Pier Ventre647138f2016-08-26 17:32:44 -070037import org.onosproject.net.flow.criteria.Criteria;
38import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080039import org.onosproject.net.intent.FlowRuleIntent;
40import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.LinkCollectionIntent;
43import org.onosproject.net.intent.MockIdGenerator;
44
45import java.util.Collection;
46import java.util.Collections;
47import java.util.List;
Pier Ventre647138f2016-08-26 17:32:44 -070048import java.util.Map;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049import java.util.Set;
Pier Ventre647138f2016-08-26 17:32:44 -070050import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051
52import static org.easymock.EasyMock.createMock;
53import static org.easymock.EasyMock.expect;
54import static org.easymock.EasyMock.replay;
55import static org.hamcrest.MatcherAssert.assertThat;
56import static org.hamcrest.Matchers.hasSize;
57import static org.hamcrest.Matchers.is;
58import static org.onosproject.net.Link.Type.DIRECT;
59import static org.onosproject.net.NetTestTools.APP_ID;
60import static org.onosproject.net.NetTestTools.PID;
61import static org.onosproject.net.NetTestTools.connectPoint;
62
63public 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 Ventre647138f2016-08-26 17:32:44 -070071 private final ConnectPoint d3p2 = connectPoint("s3", 9);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072 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 Milkey2693bda2016-01-22 16:08:14 -080076 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 SHIMIZUee2aa652015-02-25 18:56:43 -080079
Pier Ventre647138f2016-08-26 17:32:44 -070080 private final Set<Link> linksMultiple = ImmutableSet.of(
81 DefaultLink.builder().providerId(PID).src(d3p1).dst(d2p0).type(DIRECT).build());
82
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080083 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
84 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
85
Pier Ventre647138f2016-08-26 17:32:44 -070086 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 SHIMIZUee2aa652015-02-25 18:56:43 -0800104 private CoreService coreService;
105 private IntentExtensionService intentExtensionService;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800106 private IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800107 private IdGenerator idGenerator = new MockIdGenerator();
108
109 private LinkCollectionIntent intent;
Pier Ventre647138f2016-08-26 17:32:44 -0700110 private LinkCollectionIntent intentMultiple;
111
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800112
113 private LinkCollectionIntentCompiler sut;
114
115 @Before
Brian O'Connor81134662015-06-25 17:23:33 -0400116 public void setUp() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800117 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 Ventre647138f2016-08-26 17:32:44 -0700133 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 SHIMIZUee2aa652015-02-25 18:56:43 -0800142 intentExtensionService = createMock(IntentExtensionService.class);
143 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
144 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800145
146 registrator = new IntentConfigurableRegistrator();
147 registrator.extensionService = intentExtensionService;
148 registrator.cfgService = new ComponentConfigAdapter();
149 registrator.activate();
150
151 sut.registrator = registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800152
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 SHIMIZUec07ffd2016-02-22 20:45:21 -0800165 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800166 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'Connor81134662015-06-25 17:23:33 -0400182 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800183
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'Connor81134662015-06-25 17:23:33 -0400194 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800195
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'Connor81134662015-06-25 17:23:33 -0400206 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800207
208 sut.deactivate();
209 }
Pier Ventre647138f2016-08-26 17:32:44 -0700210
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 SHIMIZUee2aa652015-02-25 18:56:43 -0800305}