blob: 7ce1c160eb3fc8e1a0bb9b0f9b927a509f13a1b1 [file] [log] [blame]
Charles Chana7903c82018-03-15 20:14:16 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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.flowobjective.impl;
18
19import com.google.common.collect.Lists;
20import com.google.common.collect.Sets;
21import org.junit.Before;
22import org.junit.Ignore;
23import org.junit.Test;
24import org.onlab.packet.Ethernet;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.MplsLabel;
28import org.onlab.packet.VlanId;
Charles Chan45c19d72018-04-19 21:38:40 -070029import org.onosproject.cfg.ComponentConfigService;
Charles Chana7903c82018-03-15 20:14:16 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.DefaultApplicationId;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerAdapter;
Charles Chan45c19d72018-04-19 21:38:40 -070037import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.driver.DriverService;
Charles Chana7903c82018-03-15 20:14:16 -070039import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
43import org.onosproject.net.flow.criteria.Criteria;
44import org.onosproject.net.flowobjective.DefaultFilteringObjective;
45import org.onosproject.net.flowobjective.DefaultForwardingObjective;
46import org.onosproject.net.flowobjective.DefaultNextObjective;
47import org.onosproject.net.flowobjective.FilteringObjective;
48import org.onosproject.net.flowobjective.FlowObjectiveStore;
49import org.onosproject.net.flowobjective.ForwardingObjective;
50import org.onosproject.net.flowobjective.NextObjective;
51import org.onosproject.net.flowobjective.Objective;
Charles Chan45c19d72018-04-19 21:38:40 -070052import org.onosproject.net.flowobjective.ObjectiveContext;
Charles Chana7903c82018-03-15 20:14:16 -070053import org.onosproject.net.flowobjective.ObjectiveError;
54import org.onosproject.net.flowobjective.ObjectiveEvent;
55
56import static java.util.concurrent.Executors.newFixedThreadPool;
57import static org.easymock.EasyMock.createMock;
58import static org.easymock.EasyMock.expect;
59import static org.easymock.EasyMock.replay;
Charles Chan45c19d72018-04-19 21:38:40 -070060import static org.easymock.EasyMock.reset;
Charles Chana7903c82018-03-15 20:14:16 -070061import static org.easymock.EasyMock.verify;
62import static org.junit.Assert.assertTrue;
63import static org.junit.Assert.assertEquals;
64import static org.onlab.junit.TestTools.assertAfter;
65import static org.onlab.util.Tools.groupedThreads;
66
67import java.util.Collection;
68import java.util.List;
69import java.util.Random;
70
71public class InOrderFlowObjectiveManagerTest {
72 private InOrderFlowObjectiveManager mgr;
73
74 private static final int PRIORITY = 1000;
75 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "org.onosproject.test");
76 private static final DeviceId DEV1 = DeviceId.deviceId("of:1");
77 private static final PortNumber P1 = PortNumber.portNumber(1);
78 private static final PortNumber P2 = PortNumber.portNumber(2);
79 private static final PortNumber P3 = PortNumber.portNumber(3);
80 private static final PortNumber P4 = PortNumber.portNumber(4);
81 private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01");
82 private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02");
83 private static final MacAddress M3 = MacAddress.valueOf("00:00:00:00:00:03");
84 private static final VlanId V1 = VlanId.vlanId((short) 10);
85 private static final VlanId V2 = VlanId.vlanId((short) 20);
86 private static final VlanId V3 = VlanId.vlanId((short) 30);
87 private static final TrafficSelector S1 = DefaultTrafficSelector.builder()
88 .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.1/32")).build();
89 private static final TrafficSelector S2 = DefaultTrafficSelector.builder()
90 .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.2/32")).build();
91 private static final int NID1 = 1;
92 private static final int NID2 = 2;
93 private static final NextGroup NGRP1 = () -> new byte[] {0x00, 0x01};
94 private static final NextGroup NGRP2 = () -> new byte[] {0x02, 0x03};
95
96 // Delay flow objectives OFFSET + rand(0, BOUND) millis
Charles Chan45c19d72018-04-19 21:38:40 -070097 private static final int DEFAULT_OFFSET = 10; // ms
98 private static final int DEFAULT_BOUND = 40; // ms
99 private static int offset = DEFAULT_OFFSET;
100 private static int bound = DEFAULT_BOUND;
Charles Chana7903c82018-03-15 20:14:16 -0700101
102 private static final FilteringObjective FILT1 = buildFilteringObjective(P2, V3, M3, 1).add();
103 private static final FilteringObjective FILT2 = buildFilteringObjective(P2, V2, M2, 2).add();
104 private static final FilteringObjective FILT3 = buildFilteringObjective(P2, V3, M3, 3).remove();
105 private static final FilteringObjective FILT4 = buildFilteringObjective(P1, V1, M1, 4).add();
106 private static final FilteringObjective FILT5 = buildFilteringObjective(P2, V2, M2, 5).remove();
107 private static final FilteringObjective FILT6 = buildFilteringObjective(P1, V1, M1, 6).remove();
108 private static final FilteringObjective FILT7 = buildFilteringObjective(P2, V3, M3, 7).add();
109 private List<FilteringObjective> expectFiltObjs = Lists.newCopyOnWriteArrayList(
110 Lists.newArrayList(FILT1, FILT2, FILT3, FILT4, FILT5, FILT6, FILT7));
111
112 private static final NextObjective NEXT1 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).add();
113 private static final NextObjective NEXT2 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).add();
114 private static final NextObjective NEXT3 = buildNextObjective(NID1, V1, Sets.newHashSet(P1, P2)).addToExisting();
115 private static final NextObjective NEXT4 = buildNextObjective(NID2, V2, Sets.newHashSet(P3, P4)).addToExisting();
116 private static final NextObjective NEXT5 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).removeFromExisting();
117 private static final NextObjective NEXT6 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).removeFromExisting();
118 private static final NextObjective NEXT7 = buildNextObjective(NID1, V1, Sets.newHashSet()).remove();
119 private static final NextObjective NEXT8 = buildNextObjective(NID2, V2, Sets.newHashSet()).remove();
120 private List<NextObjective> expectNextObjs = Lists.newCopyOnWriteArrayList(
121 Lists.newArrayList(NEXT1, NEXT2, NEXT3, NEXT4, NEXT5, NEXT6, NEXT7, NEXT8));
122 private List<NextObjective> expectNextObjsPending = Lists.newCopyOnWriteArrayList(
123 Lists.newArrayList(NEXT5, NEXT6, NEXT1, NEXT2, NEXT3, NEXT4, NEXT7, NEXT8));
124
125 private static final ForwardingObjective FWD1 = buildFwdObjective(S1, NID1).add();
126 private static final ForwardingObjective FWD2 = buildFwdObjective(S2, NID2).add();
127 private static final ForwardingObjective FWD3 = buildFwdObjective(S1, NID2).add();
128 private static final ForwardingObjective FWD4 = buildFwdObjective(S2, NID1).add();
129 private static final ForwardingObjective FWD5 = buildFwdObjective(S1, NID2).remove();
130 private static final ForwardingObjective FWD6 = buildFwdObjective(S2, NID1).remove();
131 private List<ForwardingObjective> expectFwdObjs = Lists.newCopyOnWriteArrayList(
132 Lists.newArrayList(FWD1, FWD2, FWD3, FWD4, FWD5, FWD6));
133
Charles Chan45c19d72018-04-19 21:38:40 -0700134 private static boolean timeout = false;
135 private static final ForwardingObjective FWD11 = buildFwdObjective(S1, NID2).add(new ObjectiveContext() {
136 @Override
137 public void onError(Objective objective, ObjectiveError error) {
138 timeout = error.equals(ObjectiveError.INSTALLATIONTIMEOUT);
139 }
140 });
141 private List<ForwardingObjective> expectFwdObjsTimeout = Lists.newCopyOnWriteArrayList(
142 Lists.newArrayList(FWD11, FWD1, FWD2));
143
Charles Chana7903c82018-03-15 20:14:16 -0700144 private List<Objective> actualObjs = Lists.newCopyOnWriteArrayList();
145
146 private Pipeliner pipeliner = new PipelinerAdapter() {
147 @Override
148 public void filter(FilteringObjective filterObjective) {
149 recordObjective(filterObjective);
150 }
151
152 @Override
153 public void forward(ForwardingObjective forwardObjective) {
154 recordObjective(forwardObjective);
155 }
156
157 @Override
158 public void next(NextObjective nextObjective) {
159 recordObjective(nextObjective);
160
161 // Notify delegate when the next obj is completed
162 ObjectiveEvent.Type type;
163 if (nextObjective.op() == Objective.Operation.ADD ||
164 nextObjective.op() == Objective.Operation.ADD_TO_EXISTING) {
165 type = ObjectiveEvent.Type.ADD;
166 } else if (nextObjective.op() == Objective.Operation.REMOVE ||
167 nextObjective.op() == Objective.Operation.REMOVE_FROM_EXISTING) {
168 type = ObjectiveEvent.Type.REMOVE;
169 } else {
170 return;
171 }
172 mgr.delegate.notify(new ObjectiveEvent(type, nextObjective.id()));
173 }
174
175 /**
176 * Record the objectives.
177 * The random delay is introduced in order to mimic pipeline and flow operation behavior.
178 *
179 * @param obj Flow objective
180 */
181 private void recordObjective(Objective obj) {
182 try {
Charles Chan45c19d72018-04-19 21:38:40 -0700183 Thread.sleep(new Random().nextInt(bound) + offset);
Charles Chana7903c82018-03-15 20:14:16 -0700184 actualObjs.add(obj);
185 obj.context().ifPresent(c -> c.onSuccess(obj));
186 } catch (Exception e) {
187 obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNKNOWN));
188 }
189 }
190 };
191
192 @Before
193 public void setUp() {
194 mgr = new InOrderFlowObjectiveManager();
195 mgr.pipeliners.put(DEV1, pipeliner);
196 mgr.executorService = newFixedThreadPool(4, groupedThreads("foo", "bar"));
Charles Chan45c19d72018-04-19 21:38:40 -0700197 mgr.cfgService = createMock(ComponentConfigService.class);
198 mgr.deviceService = createMock(DeviceService.class);
199 mgr.driverService = createMock(DriverService.class);
Charles Chana7903c82018-03-15 20:14:16 -0700200 mgr.flowObjectiveStore = createMock(FlowObjectiveStore.class);
Charles Chan45c19d72018-04-19 21:38:40 -0700201 mgr.activate();
Charles Chana7903c82018-03-15 20:14:16 -0700202
Charles Chan45c19d72018-04-19 21:38:40 -0700203 reset(mgr.flowObjectiveStore);
204 timeout = false;
205 offset = DEFAULT_OFFSET;
206 bound = DEFAULT_BOUND;
Charles Chana7903c82018-03-15 20:14:16 -0700207 actualObjs.clear();
208 }
209
210 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700211 public void filter() {
Charles Chana7903c82018-03-15 20:14:16 -0700212 expectFiltObjs.forEach(filtObj -> mgr.filter(DEV1, filtObj));
213
214 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700215 int expectedTime = (bound + offset) * 7;
Charles Chana7903c82018-03-15 20:14:16 -0700216 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFiltObjs.size(), actualObjs.size()));
217
218 assertTrue(actualObjs.indexOf(FILT1) < actualObjs.indexOf(FILT2));
219 assertTrue(actualObjs.indexOf(FILT2) < actualObjs.indexOf(FILT3));
220 assertTrue(actualObjs.indexOf(FILT3) < actualObjs.indexOf(FILT5));
221 assertTrue(actualObjs.indexOf(FILT5) < actualObjs.indexOf(FILT7));
222 assertTrue(actualObjs.indexOf(FILT4) < actualObjs.indexOf(FILT6));
223 }
224
225 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700226 public void forward() {
Charles Chana7903c82018-03-15 20:14:16 -0700227 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
228 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
229 replay(mgr.flowObjectiveStore);
230
231 expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
232
233 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700234 int expectedTime = (bound + offset) * 6;
Charles Chana7903c82018-03-15 20:14:16 -0700235 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size(), actualObjs.size()));
236
237 assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3));
238 assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5));
239 assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4));
240 assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6));
241
242 verify(mgr.flowObjectiveStore);
243 }
244
245 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700246 public void forwardTimeout() {
247 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(1);
248 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(2);
249 replay(mgr.flowObjectiveStore);
250
251 // Force this objective to time out
252 offset = InOrderFlowObjectiveManager.OBJ_TIMEOUT_MS + 500;
253
254 expectFwdObjsTimeout.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
255
256 // Wait for the pipeline operation to complete
257 int expectedTime = (bound + offset) * 3;
258 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjsTimeout.size(), actualObjs.size()));
259
260 assertTrue(timeout);
261 assertTrue(actualObjs.indexOf(FWD11) < actualObjs.indexOf(FWD1));
262
263 verify(mgr.flowObjectiveStore);
264 }
265
266 @Test
267 public void forwardPending() {
Charles Chana7903c82018-03-15 20:14:16 -0700268 // Note: current logic will double check if the next obj need to be queued
269 // it does not check when resubmitting pending next back to the queue
270 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(2);
271 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(2);
272 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
273 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
274 replay(mgr.flowObjectiveStore);
275
276 expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
277
278 // Trigger the next objectives
279 mgr.next(DEV1, NEXT1);
280 mgr.next(DEV1, NEXT2);
281
282 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700283 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700284 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size() + 2, actualObjs.size()));
285
286 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(FWD1));
287 assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3));
288 assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5));
289 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(FWD2));
290 assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4));
291 assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6));
292
293 verify(mgr.flowObjectiveStore);
294 }
295
296 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700297 public void next() {
Charles Chana7903c82018-03-15 20:14:16 -0700298 // Note: ADD operation won't query this
299 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
300 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
301 replay(mgr.flowObjectiveStore);
302
303 expectNextObjs.forEach(nextObj -> mgr.next(DEV1, nextObj));
304
305 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700306 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700307 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size()));
308
309 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT3));
310 assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT5));
311 assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT7));
312 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT4));
313 assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT6));
314 assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT8));
315
316 verify(mgr.flowObjectiveStore);
317 }
318
319 // FIXME We currently do not handle the case when an app sends edit/remove of a next id before add.
320 // The edit/remove operation will be queued by pendingNext, and the add operation will be
321 // queued by the ordering queue forever due to the deadlock. This can be improved by making
322 // pendingForwards, pendingNexts and ordering queue caches.
323 @Test
324 @Ignore("Not supported")
Charles Chan45c19d72018-04-19 21:38:40 -0700325 public void nextPending() {
Charles Chana7903c82018-03-15 20:14:16 -0700326 // Note: current logic will double check if the next obj need to be queued
327 // it does not check when resubmitting pending next back to the queue
328 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(6);
329 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(6);
330 replay(mgr.flowObjectiveStore);
331
332 expectNextObjsPending.forEach(nextObj -> mgr.next(DEV1, nextObj));
333
334 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700335 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700336 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size()));
337
338 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT5));
339 assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT3));
340 assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT7));
341 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT6));
342 assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT4));
343 assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT8));
344
345 verify(mgr.flowObjectiveStore);
346 }
347
348 /**
349 * Creates filtering objective builder with a serial number encoded in MPLS label.
350 * The serial number is used to identify same objective that occurs multiple times.
351 *
352 * @param portnum Port number
353 * @param vlanId VLAN Id
354 * @param mac MAC address
355 * @param serial Serial number
356 * @return Filtering objective builder
357 */
358 private static FilteringObjective.Builder buildFilteringObjective(PortNumber portnum, VlanId vlanId,
359 MacAddress mac, int serial) {
360 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
361 fob.withKey(Criteria.matchInPort(portnum))
362 .addCondition(Criteria.matchEthDst(mac))
363 .addCondition(Criteria.matchVlanId(VlanId.NONE))
364 .addCondition(Criteria.matchMplsLabel(MplsLabel.mplsLabel(serial)))
365 .withPriority(PRIORITY);
366
367 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
368 tBuilder.pushVlan().setVlanId(vlanId);
369 fob.withMeta(tBuilder.build());
370
371 fob.permit().fromApp(APP_ID);
372 return fob;
373 }
374
375 /**
376 * Creates next objective builder.
377 *
378 * @param nextId next ID
379 * @param vlanId VLAN ID
380 * @param ports Set of ports that is in the given VLAN ID
381 *
382 * @return Next objective builder
383 */
384 private static NextObjective.Builder buildNextObjective(int nextId, VlanId vlanId, Collection<PortNumber> ports) {
385 TrafficSelector metadata =
386 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
387
388 NextObjective.Builder nextObjBuilder = DefaultNextObjective
389 .builder().withId(nextId)
390 .withType(NextObjective.Type.BROADCAST).fromApp(APP_ID)
391 .withMeta(metadata);
392
393 ports.forEach(port -> {
394 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
395 tBuilder.popVlan();
396 tBuilder.setOutput(port);
397 nextObjBuilder.addTreatment(tBuilder.build());
398 });
399
400 return nextObjBuilder;
401 }
402
403 /**
404 * Creates forwarding objective builder.
405 *
406 * @param selector Traffic selector
407 * @param nextId next ID
408 * @return Forwarding objective builder
409 */
410 private static ForwardingObjective.Builder buildFwdObjective(TrafficSelector selector, int nextId) {
411 return DefaultForwardingObjective.builder()
412 .makePermanent()
413 .withSelector(selector)
414 .nextStep(nextId)
415 .fromApp(APP_ID)
416 .withPriority(PRIORITY)
417 .withFlag(ForwardingObjective.Flag.SPECIFIC);
418 }
419}