blob: 32c9f950d7117b2c4dedd5732e609eedfad95160 [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;
Charles Chan1491b9b2018-11-27 21:33:33 +080069import java.util.Objects;
Charles Chana7903c82018-03-15 20:14:16 -070070import java.util.Random;
Charles Chan1491b9b2018-11-27 21:33:33 +080071import java.util.concurrent.atomic.AtomicInteger;
Charles Chana7903c82018-03-15 20:14:16 -070072
73public class InOrderFlowObjectiveManagerTest {
74 private InOrderFlowObjectiveManager mgr;
75
76 private static final int PRIORITY = 1000;
77 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "org.onosproject.test");
78 private static final DeviceId DEV1 = DeviceId.deviceId("of:1");
79 private static final PortNumber P1 = PortNumber.portNumber(1);
80 private static final PortNumber P2 = PortNumber.portNumber(2);
81 private static final PortNumber P3 = PortNumber.portNumber(3);
82 private static final PortNumber P4 = PortNumber.portNumber(4);
83 private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01");
84 private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02");
85 private static final MacAddress M3 = MacAddress.valueOf("00:00:00:00:00:03");
86 private static final VlanId V1 = VlanId.vlanId((short) 10);
87 private static final VlanId V2 = VlanId.vlanId((short) 20);
88 private static final VlanId V3 = VlanId.vlanId((short) 30);
89 private static final TrafficSelector S1 = DefaultTrafficSelector.builder()
90 .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.1/32")).build();
91 private static final TrafficSelector S2 = DefaultTrafficSelector.builder()
92 .matchEthType(Ethernet.TYPE_IPV4).matchIPDst(IpPrefix.valueOf("10.0.0.2/32")).build();
93 private static final int NID1 = 1;
94 private static final int NID2 = 2;
95 private static final NextGroup NGRP1 = () -> new byte[] {0x00, 0x01};
96 private static final NextGroup NGRP2 = () -> new byte[] {0x02, 0x03};
97
98 // Delay flow objectives OFFSET + rand(0, BOUND) millis
Charles Chan45c19d72018-04-19 21:38:40 -070099 private static final int DEFAULT_OFFSET = 10; // ms
100 private static final int DEFAULT_BOUND = 40; // ms
Charles Chanb4f4fdb2018-12-21 13:55:29 -0800101 private static final int TIMEOUT_THRESH = 500; // ms
Charles Chan45c19d72018-04-19 21:38:40 -0700102 private static int offset = DEFAULT_OFFSET;
103 private static int bound = DEFAULT_BOUND;
Charles Chana7903c82018-03-15 20:14:16 -0700104
105 private static final FilteringObjective FILT1 = buildFilteringObjective(P2, V3, M3, 1).add();
106 private static final FilteringObjective FILT2 = buildFilteringObjective(P2, V2, M2, 2).add();
107 private static final FilteringObjective FILT3 = buildFilteringObjective(P2, V3, M3, 3).remove();
108 private static final FilteringObjective FILT4 = buildFilteringObjective(P1, V1, M1, 4).add();
109 private static final FilteringObjective FILT5 = buildFilteringObjective(P2, V2, M2, 5).remove();
110 private static final FilteringObjective FILT6 = buildFilteringObjective(P1, V1, M1, 6).remove();
111 private static final FilteringObjective FILT7 = buildFilteringObjective(P2, V3, M3, 7).add();
112 private List<FilteringObjective> expectFiltObjs = Lists.newCopyOnWriteArrayList(
113 Lists.newArrayList(FILT1, FILT2, FILT3, FILT4, FILT5, FILT6, FILT7));
114
115 private static final NextObjective NEXT1 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).add();
116 private static final NextObjective NEXT2 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).add();
117 private static final NextObjective NEXT3 = buildNextObjective(NID1, V1, Sets.newHashSet(P1, P2)).addToExisting();
118 private static final NextObjective NEXT4 = buildNextObjective(NID2, V2, Sets.newHashSet(P3, P4)).addToExisting();
119 private static final NextObjective NEXT5 = buildNextObjective(NID1, V1, Sets.newHashSet(P1)).removeFromExisting();
120 private static final NextObjective NEXT6 = buildNextObjective(NID2, V2, Sets.newHashSet(P3)).removeFromExisting();
121 private static final NextObjective NEXT7 = buildNextObjective(NID1, V1, Sets.newHashSet()).remove();
122 private static final NextObjective NEXT8 = buildNextObjective(NID2, V2, Sets.newHashSet()).remove();
123 private List<NextObjective> expectNextObjs = Lists.newCopyOnWriteArrayList(
124 Lists.newArrayList(NEXT1, NEXT2, NEXT3, NEXT4, NEXT5, NEXT6, NEXT7, NEXT8));
125 private List<NextObjective> expectNextObjsPending = Lists.newCopyOnWriteArrayList(
126 Lists.newArrayList(NEXT5, NEXT6, NEXT1, NEXT2, NEXT3, NEXT4, NEXT7, NEXT8));
127
128 private static final ForwardingObjective FWD1 = buildFwdObjective(S1, NID1).add();
129 private static final ForwardingObjective FWD2 = buildFwdObjective(S2, NID2).add();
130 private static final ForwardingObjective FWD3 = buildFwdObjective(S1, NID2).add();
131 private static final ForwardingObjective FWD4 = buildFwdObjective(S2, NID1).add();
132 private static final ForwardingObjective FWD5 = buildFwdObjective(S1, NID2).remove();
133 private static final ForwardingObjective FWD6 = buildFwdObjective(S2, NID1).remove();
134 private List<ForwardingObjective> expectFwdObjs = Lists.newCopyOnWriteArrayList(
135 Lists.newArrayList(FWD1, FWD2, FWD3, FWD4, FWD5, FWD6));
136
137 private List<Objective> actualObjs = Lists.newCopyOnWriteArrayList();
138
139 private Pipeliner pipeliner = new PipelinerAdapter() {
140 @Override
141 public void filter(FilteringObjective filterObjective) {
142 recordObjective(filterObjective);
143 }
144
145 @Override
146 public void forward(ForwardingObjective forwardObjective) {
147 recordObjective(forwardObjective);
148 }
149
150 @Override
151 public void next(NextObjective nextObjective) {
152 recordObjective(nextObjective);
153
154 // Notify delegate when the next obj is completed
155 ObjectiveEvent.Type type;
156 if (nextObjective.op() == Objective.Operation.ADD ||
157 nextObjective.op() == Objective.Operation.ADD_TO_EXISTING) {
158 type = ObjectiveEvent.Type.ADD;
159 } else if (nextObjective.op() == Objective.Operation.REMOVE ||
160 nextObjective.op() == Objective.Operation.REMOVE_FROM_EXISTING) {
161 type = ObjectiveEvent.Type.REMOVE;
162 } else {
163 return;
164 }
165 mgr.delegate.notify(new ObjectiveEvent(type, nextObjective.id()));
166 }
167
168 /**
169 * Record the objectives.
170 * The random delay is introduced in order to mimic pipeline and flow operation behavior.
171 *
172 * @param obj Flow objective
173 */
174 private void recordObjective(Objective obj) {
175 try {
Charles Chan45c19d72018-04-19 21:38:40 -0700176 Thread.sleep(new Random().nextInt(bound) + offset);
Ray Milkey95c9e0f2018-05-30 14:16:37 -0700177 if (!actualObjs.contains(obj)) {
178 actualObjs.add(obj);
179 }
Charles Chana7903c82018-03-15 20:14:16 -0700180 obj.context().ifPresent(c -> c.onSuccess(obj));
181 } catch (Exception e) {
182 obj.context().ifPresent(c -> c.onError(obj, ObjectiveError.UNKNOWN));
183 }
184 }
185 };
186
187 @Before
188 public void setUp() {
Charles Chanb4f4fdb2018-12-21 13:55:29 -0800189 internalSetup(InOrderFlowObjectiveManager.DEFAULT_OBJ_TIMEOUT);
190 }
191
192 private void internalSetup(int objTimeoutMs) {
Charles Chana7903c82018-03-15 20:14:16 -0700193 mgr = new InOrderFlowObjectiveManager();
Charles Chanb4f4fdb2018-12-21 13:55:29 -0800194 mgr.objTimeoutMs = objTimeoutMs;
Charles Chana7903c82018-03-15 20:14:16 -0700195 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);
Charles Chan45c19d72018-04-19 21:38:40 -0700204 offset = DEFAULT_OFFSET;
205 bound = DEFAULT_BOUND;
Charles Chana7903c82018-03-15 20:14:16 -0700206 actualObjs.clear();
207 }
208
209 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700210 public void filter() {
Charles Chana7903c82018-03-15 20:14:16 -0700211 expectFiltObjs.forEach(filtObj -> mgr.filter(DEV1, filtObj));
212
213 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700214 int expectedTime = (bound + offset) * 7;
Charles Chana7903c82018-03-15 20:14:16 -0700215 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFiltObjs.size(), actualObjs.size()));
216
217 assertTrue(actualObjs.indexOf(FILT1) < actualObjs.indexOf(FILT2));
218 assertTrue(actualObjs.indexOf(FILT2) < actualObjs.indexOf(FILT3));
219 assertTrue(actualObjs.indexOf(FILT3) < actualObjs.indexOf(FILT5));
220 assertTrue(actualObjs.indexOf(FILT5) < actualObjs.indexOf(FILT7));
221 assertTrue(actualObjs.indexOf(FILT4) < actualObjs.indexOf(FILT6));
222 }
223
224 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700225 public void forward() {
Charles Chana7903c82018-03-15 20:14:16 -0700226 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
227 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
228 replay(mgr.flowObjectiveStore);
229
230 expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
231
232 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700233 int expectedTime = (bound + offset) * 6;
Charles Chana7903c82018-03-15 20:14:16 -0700234 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size(), actualObjs.size()));
235
236 assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3));
237 assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5));
238 assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4));
239 assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6));
240
241 verify(mgr.flowObjectiveStore);
242 }
243
244 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700245 public void forwardTimeout() {
Charles Chan1491b9b2018-11-27 21:33:33 +0800246 final AtomicInteger counter = new AtomicInteger(0);
247 ForwardingObjective fwdTimeout = buildFwdObjective(S1, NID2).add(new ObjectiveContext() {
248 @Override
249 public void onError(Objective objective, ObjectiveError error) {
250 if (Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) {
251 counter.incrementAndGet();
252 }
253 }
254 });
255 List<ForwardingObjective> expectFwdObjsTimeout = Lists.newCopyOnWriteArrayList(
256 Lists.newArrayList(fwdTimeout, FWD1, FWD2));
257
258 // Reduce timeout so the unit test doesn't have to wait many seconds
Charles Chanb4f4fdb2018-12-21 13:55:29 -0800259 internalSetup(TIMEOUT_THRESH);
Charles Chan1491b9b2018-11-27 21:33:33 +0800260
Charles Chanc51852e2019-01-07 19:59:10 -0800261 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(1);
Charles Chan45c19d72018-04-19 21:38:40 -0700262 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(2);
263 replay(mgr.flowObjectiveStore);
264
265 // Force this objective to time out
Charles Chanc51852e2019-01-07 19:59:10 -0800266 offset = mgr.objTimeoutMs * 3;
Charles Chan45c19d72018-04-19 21:38:40 -0700267
268 expectFwdObjsTimeout.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
269
270 // Wait for the pipeline operation to complete
271 int expectedTime = (bound + offset) * 3;
272 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjsTimeout.size(), actualObjs.size()));
273
Charles Chanb4f4fdb2018-12-21 13:55:29 -0800274 assertAfter(expectedTime, expectedTime * 5, () -> assertTrue(counter.get() != 0));
Charles Chan1491b9b2018-11-27 21:33:33 +0800275 assertTrue(actualObjs.indexOf(fwdTimeout) < actualObjs.indexOf(FWD1));
Charles Chan45c19d72018-04-19 21:38:40 -0700276
277 verify(mgr.flowObjectiveStore);
278 }
279
280 @Test
281 public void forwardPending() {
Charles Chana7903c82018-03-15 20:14:16 -0700282 // Note: current logic will double check if the next obj need to be queued
283 // it does not check when resubmitting pending next back to the queue
284 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(2);
285 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(2);
286 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
287 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
288 replay(mgr.flowObjectiveStore);
289
290 expectFwdObjs.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
291
292 // Trigger the next objectives
293 mgr.next(DEV1, NEXT1);
294 mgr.next(DEV1, NEXT2);
295
296 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700297 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700298 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjs.size() + 2, actualObjs.size()));
299
300 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(FWD1));
301 assertTrue(actualObjs.indexOf(FWD1) < actualObjs.indexOf(FWD3));
302 assertTrue(actualObjs.indexOf(FWD3) < actualObjs.indexOf(FWD5));
303 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(FWD2));
304 assertTrue(actualObjs.indexOf(FWD2) < actualObjs.indexOf(FWD4));
305 assertTrue(actualObjs.indexOf(FWD4) < actualObjs.indexOf(FWD6));
306
307 verify(mgr.flowObjectiveStore);
308 }
309
310 @Test
Charles Chan45c19d72018-04-19 21:38:40 -0700311 public void next() {
Charles Chana7903c82018-03-15 20:14:16 -0700312 // Note: ADD operation won't query this
313 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(3);
314 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(3);
315 replay(mgr.flowObjectiveStore);
316
317 expectNextObjs.forEach(nextObj -> mgr.next(DEV1, nextObj));
318
319 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700320 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700321 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size()));
322
323 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT3));
324 assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT5));
325 assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT7));
326 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT4));
327 assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT6));
328 assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT8));
329
330 verify(mgr.flowObjectiveStore);
331 }
332
333 // FIXME We currently do not handle the case when an app sends edit/remove of a next id before add.
334 // The edit/remove operation will be queued by pendingNext, and the add operation will be
335 // queued by the ordering queue forever due to the deadlock. This can be improved by making
336 // pendingForwards, pendingNexts and ordering queue caches.
337 @Test
338 @Ignore("Not supported")
Charles Chan45c19d72018-04-19 21:38:40 -0700339 public void nextPending() {
Charles Chana7903c82018-03-15 20:14:16 -0700340 // Note: current logic will double check if the next obj need to be queued
341 // it does not check when resubmitting pending next back to the queue
342 expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(null).times(6);
343 expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(null).times(6);
344 replay(mgr.flowObjectiveStore);
345
346 expectNextObjsPending.forEach(nextObj -> mgr.next(DEV1, nextObj));
347
348 // Wait for the pipeline operation to complete
Charles Chan45c19d72018-04-19 21:38:40 -0700349 int expectedTime = (bound + offset) * 8;
Charles Chana7903c82018-03-15 20:14:16 -0700350 assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectNextObjs.size(), actualObjs.size()));
351
352 assertTrue(actualObjs.indexOf(NEXT1) < actualObjs.indexOf(NEXT5));
353 assertTrue(actualObjs.indexOf(NEXT5) < actualObjs.indexOf(NEXT3));
354 assertTrue(actualObjs.indexOf(NEXT3) < actualObjs.indexOf(NEXT7));
355 assertTrue(actualObjs.indexOf(NEXT2) < actualObjs.indexOf(NEXT6));
356 assertTrue(actualObjs.indexOf(NEXT6) < actualObjs.indexOf(NEXT4));
357 assertTrue(actualObjs.indexOf(NEXT4) < actualObjs.indexOf(NEXT8));
358
359 verify(mgr.flowObjectiveStore);
360 }
361
362 /**
363 * Creates filtering objective builder with a serial number encoded in MPLS label.
364 * The serial number is used to identify same objective that occurs multiple times.
365 *
366 * @param portnum Port number
367 * @param vlanId VLAN Id
368 * @param mac MAC address
369 * @param serial Serial number
370 * @return Filtering objective builder
371 */
372 private static FilteringObjective.Builder buildFilteringObjective(PortNumber portnum, VlanId vlanId,
373 MacAddress mac, int serial) {
374 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
375 fob.withKey(Criteria.matchInPort(portnum))
376 .addCondition(Criteria.matchEthDst(mac))
377 .addCondition(Criteria.matchVlanId(VlanId.NONE))
378 .addCondition(Criteria.matchMplsLabel(MplsLabel.mplsLabel(serial)))
379 .withPriority(PRIORITY);
380
381 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
382 tBuilder.pushVlan().setVlanId(vlanId);
383 fob.withMeta(tBuilder.build());
384
385 fob.permit().fromApp(APP_ID);
386 return fob;
387 }
388
389 /**
390 * Creates next objective builder.
391 *
392 * @param nextId next ID
393 * @param vlanId VLAN ID
394 * @param ports Set of ports that is in the given VLAN ID
395 *
396 * @return Next objective builder
397 */
398 private static NextObjective.Builder buildNextObjective(int nextId, VlanId vlanId, Collection<PortNumber> ports) {
399 TrafficSelector metadata =
400 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
401
402 NextObjective.Builder nextObjBuilder = DefaultNextObjective
403 .builder().withId(nextId)
404 .withType(NextObjective.Type.BROADCAST).fromApp(APP_ID)
405 .withMeta(metadata);
406
407 ports.forEach(port -> {
408 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
409 tBuilder.popVlan();
410 tBuilder.setOutput(port);
411 nextObjBuilder.addTreatment(tBuilder.build());
412 });
413
414 return nextObjBuilder;
415 }
416
417 /**
418 * Creates forwarding objective builder.
419 *
420 * @param selector Traffic selector
421 * @param nextId next ID
422 * @return Forwarding objective builder
423 */
424 private static ForwardingObjective.Builder buildFwdObjective(TrafficSelector selector, int nextId) {
425 return DefaultForwardingObjective.builder()
426 .makePermanent()
427 .withSelector(selector)
428 .nextStep(nextId)
429 .fromApp(APP_ID)
430 .withPriority(PRIORITY)
431 .withFlag(ForwardingObjective.Flag.SPECIFIC);
432 }
433}