blob: 8083c2b9c02cbd046c3523ffea7d76de00612b2c [file] [log] [blame]
Michele Santuari9a8d16d2016-03-24 10:37:58 -07001/*
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.drivers.corsa;
18
19import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalCause;
22import com.google.common.cache.RemovalNotification;
Michele Santuarid2c8b152016-03-30 17:57:56 -070023import com.google.common.collect.ImmutableSet;
Michele Santuari9a8d16d2016-03-24 10:37:58 -070024import org.onlab.osgi.ServiceDirectory;
25import org.onlab.packet.Ethernet;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.behaviour.NextGroup;
31import org.onosproject.net.behaviour.Pipeliner;
32import org.onosproject.net.behaviour.PipelinerContext;
33import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.driver.AbstractHandlerBehaviour;
35import org.onosproject.net.flow.DefaultFlowRule;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.FlowRuleOperations;
40import org.onosproject.net.flow.FlowRuleOperationsContext;
41import org.onosproject.net.flow.FlowRuleService;
42import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
44import org.onosproject.net.flow.criteria.Criteria;
45import org.onosproject.net.flow.criteria.Criterion;
46import org.onosproject.net.flow.criteria.EthCriterion;
47import org.onosproject.net.flow.criteria.EthTypeCriterion;
48import org.onosproject.net.flow.criteria.IPCriterion;
49import org.onosproject.net.flow.criteria.PortCriterion;
50import org.onosproject.net.flow.criteria.VlanIdCriterion;
51import org.onosproject.net.flowobjective.FilteringObjective;
52import org.onosproject.net.flowobjective.FlowObjectiveStore;
53import org.onosproject.net.flowobjective.ForwardingObjective;
54import org.onosproject.net.flowobjective.NextObjective;
55import org.onosproject.net.flowobjective.Objective;
56import org.onosproject.net.flowobjective.ObjectiveError;
57import org.onosproject.net.group.DefaultGroupBucket;
58import org.onosproject.net.group.DefaultGroupDescription;
59import org.onosproject.net.group.DefaultGroupKey;
60import org.onosproject.net.group.Group;
61import org.onosproject.net.group.GroupBucket;
62import org.onosproject.net.group.GroupBuckets;
63import org.onosproject.net.group.GroupDescription;
64import org.onosproject.net.group.GroupEvent;
65import org.onosproject.net.group.GroupKey;
66import org.onosproject.net.group.GroupListener;
67import org.onosproject.net.group.GroupService;
68import org.onosproject.net.meter.MeterService;
69import org.slf4j.Logger;
70
71import java.util.Collection;
72import java.util.Collections;
73import java.util.List;
74import java.util.Objects;
75import java.util.Set;
76import java.util.concurrent.Executors;
77import java.util.concurrent.ScheduledExecutorService;
78import java.util.concurrent.TimeUnit;
79import java.util.stream.Collectors;
80
81import static org.onlab.util.Tools.groupedThreads;
82import static org.onosproject.net.flow.FlowRule.Builder;
83import static org.slf4j.LoggerFactory.getLogger;
84
85/**
86 * Abstraction of the Corsa pipeline handler.
87 */
88public abstract class AbstractCorsaPipeline extends AbstractHandlerBehaviour implements Pipeliner {
89
90
91 private final Logger log = getLogger(getClass());
92
93 private ServiceDirectory serviceDirectory;
94 protected FlowRuleService flowRuleService;
95 private CoreService coreService;
Pier Ventredb673552016-07-20 15:37:19 +020096 protected GroupService groupService;
Michele Santuari9a8d16d2016-03-24 10:37:58 -070097 protected MeterService meterService;
Pier Ventredb673552016-07-20 15:37:19 +020098 protected FlowObjectiveStore flowObjectiveStore;
Michele Santuari9a8d16d2016-03-24 10:37:58 -070099 protected DeviceId deviceId;
100 protected ApplicationId appId;
101 protected DeviceService deviceService;
102
Pier Ventredb673552016-07-20 15:37:19 +0200103 protected KryoNamespace appKryo = new KryoNamespace.Builder()
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700104 .register(GroupKey.class)
105 .register(DefaultGroupKey.class)
106 .register(CorsaGroup.class)
107 .register(byte[].class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700108 .build("AbstractCorsaPipeline");
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700109
110 private Cache<GroupKey, NextObjective> pendingGroups;
Pier Ventredb673552016-07-20 15:37:19 +0200111 protected Cache<Integer, NextObjective> pendingNext;
112
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700113
114 private ScheduledExecutorService groupChecker =
115 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner",
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700116 "ovs-corsa-%d", log));
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700117
118 protected static final int CONTROLLER_PRIORITY = 255;
119 protected static final int DROP_PRIORITY = 0;
120 protected static final int HIGHEST_PRIORITY = 0xffff;
121 protected static final String APPID = "org.onosproject.drivers.corsa.CorsaPipeline";
122
123 @Override
124 public void init(DeviceId deviceId, PipelinerContext context) {
125 this.serviceDirectory = context.directory();
126 this.deviceId = deviceId;
127
128 pendingGroups = CacheBuilder.newBuilder()
129 .expireAfterWrite(20, TimeUnit.SECONDS)
130 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
131 if (notification.getCause() == RemovalCause.EXPIRED) {
132 fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED);
133 }
134 }).build();
135
Pier Ventredb673552016-07-20 15:37:19 +0200136 pendingNext = CacheBuilder.newBuilder()
137 .expireAfterWrite(20, TimeUnit.SECONDS)
138 .removalListener((RemovalNotification<Integer, NextObjective> notification) -> {
139 if (notification.getCause() == RemovalCause.EXPIRED) {
140 notification.getValue().context()
141 .ifPresent(c -> c.onError(notification.getValue(),
142 ObjectiveError.FLOWINSTALLATIONFAILED));
143 }
144 }).build();
145
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700146 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
147
148 coreService = serviceDirectory.get(CoreService.class);
149 flowRuleService = serviceDirectory.get(FlowRuleService.class);
150 groupService = serviceDirectory.get(GroupService.class);
151 meterService = serviceDirectory.get(MeterService.class);
152 deviceService = serviceDirectory.get(DeviceService.class);
153 flowObjectiveStore = context.store();
154
155 groupService.addListener(new InnerGroupListener());
156
157 appId = coreService.registerApplication(APPID);
158
159 initializePipeline();
160 }
161
162 protected abstract void initializePipeline();
163
164 protected void pass(Objective obj) {
165 obj.context().ifPresent(context -> context.onSuccess(obj));
166 }
167
168 protected void fail(Objective obj, ObjectiveError error) {
169 obj.context().ifPresent(context -> context.onError(obj, error));
170 }
171
172 private class GroupChecker implements Runnable {
173
174 @Override
175 public void run() {
176 Set<GroupKey> keys = pendingGroups.asMap().keySet().stream()
177 .filter(key -> groupService.getGroup(deviceId, key) != null)
178 .collect(Collectors.toSet());
179
180 keys.stream().forEach(key -> {
181 NextObjective obj = pendingGroups.getIfPresent(key);
182 if (obj == null) {
183 return;
184 }
185 pass(obj);
186 pendingGroups.invalidate(key);
187 log.info("Heard back from group service for group {}. "
188 + "Applying pending forwarding objectives", obj.id());
189 flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key));
190 });
191 }
192 }
193
194 private class CorsaGroup implements NextGroup {
195
196 private final GroupKey key;
197
198 public CorsaGroup(GroupKey key) {
199 this.key = key;
200 }
201
202 public GroupKey key() {
203 return key;
204 }
205
206 @Override
207 public byte[] data() {
208 return appKryo.serialize(key);
209 }
210
211 }
212
213 @Override
214 public List<String> getNextMappings(NextGroup nextGroup) {
215 //TODO: to be implemented
216 return Collections.emptyList();
217 }
218
219 private class InnerGroupListener implements GroupListener {
220 @Override
221 public void event(GroupEvent event) {
222 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
223 GroupKey key = event.subject().appCookie();
224
225 NextObjective obj = pendingGroups.getIfPresent(key);
226 if (obj != null) {
227 flowObjectiveStore.putNextGroup(obj.id(), new CorsaGroup(key));
228 pass(obj);
229 pendingGroups.invalidate(key);
230 }
231 }
232 }
233 }
234
235
236 @Override
237 public void filter(FilteringObjective filteringObjective) {
238 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
239 processFilter(filteringObjective,
240 filteringObjective.op() == Objective.Operation.ADD,
241 filteringObjective.appId());
242 } else {
243 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
244 }
245 }
246
247 private void processFilter(FilteringObjective filt, boolean install,
248 ApplicationId applicationId) {
249 // This driver only processes filtering criteria defined with switch
250 // ports as the key
251 PortCriterion port;
252 if (!filt.key().equals(Criteria.dummy()) &&
253 filt.key().type() == Criterion.Type.IN_PORT) {
254 port = (PortCriterion) filt.key();
255 } else {
256 log.warn("No key defined in filtering objective from app: {}. Not"
257 + "processing filtering objective", applicationId);
258 fail(filt, ObjectiveError.UNKNOWN);
259 return;
260 }
261 // convert filtering conditions for switch-intfs into flowrules
262 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
263 for (Criterion c : filt.conditions()) {
264 if (c.type() == Criterion.Type.ETH_DST) {
265 EthCriterion eth = (EthCriterion) c;
266 FlowRule.Builder rule = processEthFiler(filt, eth, port);
267 rule.forDevice(deviceId)
268 .fromApp(applicationId);
269 ops = install ? ops.add(rule.build()) : ops.remove(rule.build());
270
271 } else if (c.type() == Criterion.Type.VLAN_VID) {
272 VlanIdCriterion vlan = (VlanIdCriterion) c;
273 FlowRule.Builder rule = processVlanFiler(filt, vlan, port);
274 rule.forDevice(deviceId)
275 .fromApp(applicationId);
276 ops = install ? ops.add(rule.build()) : ops.remove(rule.build());
277
278 } else if (c.type() == Criterion.Type.IPV4_DST) {
279 IPCriterion ip = (IPCriterion) c;
280 FlowRule.Builder rule = processIpFilter(filt, ip, port);
281 rule.forDevice(deviceId)
282 .fromApp(applicationId);
283 ops = install ? ops.add(rule.build()) : ops.remove(rule.build());
284
285 } else {
286 log.warn("Driver does not currently process filtering condition"
287 + " of type: {}", c.type());
288 fail(filt, ObjectiveError.UNSUPPORTED);
289 }
290 }
291 // apply filtering flow rules
292 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
293 @Override
294 public void onSuccess(FlowRuleOperations ops) {
295 pass(filt);
296 log.info("Applied filtering rules");
297 }
298
299 @Override
300 public void onError(FlowRuleOperations ops) {
301 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
302 log.info("Failed to apply filtering rules");
303 }
304 }));
305 }
306
307 protected abstract Builder processEthFiler(FilteringObjective filt,
308 EthCriterion eth, PortCriterion port);
309
310 protected abstract Builder processVlanFiler(FilteringObjective filt,
311 VlanIdCriterion vlan, PortCriterion port);
312
313 protected abstract Builder processIpFilter(FilteringObjective filt,
314 IPCriterion ip, PortCriterion port);
315
316
317 @Override
318 public void forward(ForwardingObjective fwd) {
Pier Ventredb673552016-07-20 15:37:19 +0200319
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700320 Collection<FlowRule> rules;
321 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
322
323 rules = processForward(fwd);
324 switch (fwd.op()) {
325 case ADD:
326 rules.stream()
327 .filter(Objects::nonNull)
328 .forEach(flowBuilder::add);
329 break;
330 case REMOVE:
331 rules.stream()
332 .filter(Objects::nonNull)
333 .forEach(flowBuilder::remove);
334 break;
335 default:
336 fail(fwd, ObjectiveError.UNKNOWN);
337 log.warn("Unknown forwarding type {}", fwd.op());
338 }
339
340 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
341 @Override
342 public void onSuccess(FlowRuleOperations ops) {
343 pass(fwd);
344 }
345
346 @Override
347 public void onError(FlowRuleOperations ops) {
348 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
349 }
350 }));
351
352 }
353
354 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
355 switch (fwd.flag()) {
356 case SPECIFIC:
357 return processSpecific(fwd);
358 case VERSATILE:
359 return processVersatile(fwd);
360 default:
361 fail(fwd, ObjectiveError.UNKNOWN);
362 log.warn("Unknown forwarding flag {}", fwd.flag());
363 }
Michele Santuarid2c8b152016-03-30 17:57:56 -0700364 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700365 }
366
367 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
368 log.debug("Processing specific forwarding objective");
369 TrafficSelector selector = fwd.selector();
Pier Ventredb673552016-07-20 15:37:19 +0200370 EthTypeCriterion ethTypeCriterion =
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700371 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Pier Ventredb673552016-07-20 15:37:19 +0200372 VlanIdCriterion vlanIdCriterion =
373 (VlanIdCriterion) selector.getCriterion(Criterion.Type.VLAN_VID);
374 if (ethTypeCriterion != null) {
375 short et = ethTypeCriterion.ethType().toShort();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700376 if (et == Ethernet.TYPE_IPV4) {
377 return processSpecificRoute(fwd);
378 } else if (et == Ethernet.TYPE_VLAN) {
379 /* The ForwardingObjective must specify VLAN ethtype in order to use the Transit Circuit */
380 return processSpecificSwitch(fwd);
381 }
Pier Ventredb673552016-07-20 15:37:19 +0200382 } else if (vlanIdCriterion != null) {
383 return processSpecificSwitch(fwd);
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700384 }
385
386 fail(fwd, ObjectiveError.UNSUPPORTED);
Michele Santuarid2c8b152016-03-30 17:57:56 -0700387 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700388 }
389
390 protected Collection<FlowRule> processSpecificSwitch(ForwardingObjective fwd) {
391 /* Not supported by until CorsaPipelineV3 */
392 log.warn("Vlan switching not supported in ovs-corsa driver");
393 fail(fwd, ObjectiveError.UNSUPPORTED);
Michele Santuarid2c8b152016-03-30 17:57:56 -0700394 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700395 }
396
397 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
398 log.debug("Processing vesatile forwarding objective");
399 TrafficSelector selector = fwd.selector();
400
401 EthTypeCriterion ethType =
402 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
403 if (ethType == null) {
404 log.error("Versatile forwarding objective must include ethType");
405 fail(fwd, ObjectiveError.UNKNOWN);
Michele Santuarid2c8b152016-03-30 17:57:56 -0700406 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700407 }
408 Builder rule = DefaultFlowRule.builder()
409 .forDevice(deviceId)
410 .withSelector(fwd.selector())
411 .withTreatment(fwd.treatment())
412 .withPriority(fwd.priority())
413 .fromApp(fwd.appId())
414 .makePermanent();
415 if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) {
416 return processArpTraffic(fwd, rule);
417 } else if (ethType.ethType().toShort() == Ethernet.TYPE_LLDP ||
418 ethType.ethType().toShort() == Ethernet.TYPE_BSN) {
419 return processLinkDiscovery(fwd, rule);
420 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
421 return processIpTraffic(fwd, rule);
422 }
423 log.warn("Driver does not support given versatile forwarding objective");
424 fail(fwd, ObjectiveError.UNSUPPORTED);
Michele Santuarid2c8b152016-03-30 17:57:56 -0700425 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700426 }
427
428 protected abstract Collection<FlowRule> processArpTraffic(ForwardingObjective fwd, Builder rule);
429
430 protected abstract Collection<FlowRule> processLinkDiscovery(ForwardingObjective fwd, Builder rule);
431
432 protected abstract Collection<FlowRule> processIpTraffic(ForwardingObjective fwd, Builder rule);
433
434 private Collection<FlowRule> processSpecificRoute(ForwardingObjective fwd) {
435 TrafficSelector filteredSelector =
436 DefaultTrafficSelector.builder()
437 .matchEthType(Ethernet.TYPE_IPV4)
438 .matchIPDst(
439 ((IPCriterion) fwd.selector().getCriterion(Criterion.Type.IPV4_DST)).ip())
440 .build();
441
442 TrafficTreatment.Builder tb = processSpecificRoutingTreatment();
443
444 if (fwd.nextId() != null) {
445 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
446 GroupKey key = appKryo.deserialize(next.data());
447 Group group = groupService.getGroup(deviceId, key);
448 if (group == null) {
449 log.warn("The group left!");
450 fail(fwd, ObjectiveError.GROUPMISSING);
Michele Santuarid2c8b152016-03-30 17:57:56 -0700451 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700452 }
453 tb.group(group.id());
Michele Santuarid2c8b152016-03-30 17:57:56 -0700454 } else {
455 log.error("Missing NextObjective ID for ForwardingObjective {}", fwd.id());
456 fail(fwd, ObjectiveError.BADPARAMS);
457 return ImmutableSet.of();
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700458 }
459 Builder ruleBuilder = DefaultFlowRule.builder()
460 .fromApp(fwd.appId())
461 .withPriority(fwd.priority())
462 .forDevice(deviceId)
463 .withSelector(filteredSelector)
464 .withTreatment(tb.build());
465
466 ruleBuilder = processSpecificRoutingRule(ruleBuilder);
467
468 if (fwd.permanent()) {
469 ruleBuilder.makePermanent();
470 } else {
471 ruleBuilder.makeTemporary(fwd.timeout());
472 }
473 return Collections.singletonList(ruleBuilder.build());
474 }
475
476 //Hook for modifying Route traffic treatment
477 protected TrafficTreatment.Builder processSpecificRoutingTreatment() {
478 return DefaultTrafficTreatment.builder();
479 }
480
481 //Hook for modifying Route flow rule
482 protected abstract Builder processSpecificRoutingRule(Builder rb);
483
Pier Ventredb673552016-07-20 15:37:19 +0200484 protected enum CorsaTrafficTreatmentType {
485 /**
486 * If the treatment has to be handled as group.
487 */
488 GROUP,
489 /**
490 * If the treatment has to be handled as simple set of actions.
491 */
492 ACTIONS
493 }
494
495 /**
496 * Helper class to encapsulate both traffic treatment and
497 * type of treatment.
498 */
499 protected class CorsaTrafficTreatment {
500
501 private CorsaTrafficTreatmentType type;
502 private TrafficTreatment trafficTreatment;
503
504 public CorsaTrafficTreatment(CorsaTrafficTreatmentType treatmentType, TrafficTreatment trafficTreatment) {
505 this.type = treatmentType;
506 this.trafficTreatment = trafficTreatment;
507 }
508
509 public CorsaTrafficTreatmentType type() {
510 return type;
511 }
512
513 public TrafficTreatment treatment() {
514 return trafficTreatment;
515 }
516
517 }
518
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700519 @Override
520 public void next(NextObjective nextObjective) {
521 switch (nextObjective.type()) {
522 case SIMPLE:
523 Collection<TrafficTreatment> treatments = nextObjective.next();
524 if (treatments.size() == 1) {
525 TrafficTreatment treatment = treatments.iterator().next();
Pier Ventredb673552016-07-20 15:37:19 +0200526 CorsaTrafficTreatment corsaTreatment = processNextTreatment(treatment);
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700527 final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
Pier Ventredb673552016-07-20 15:37:19 +0200528 if (corsaTreatment.type() == CorsaTrafficTreatmentType.GROUP) {
529 GroupBucket bucket = DefaultGroupBucket.createIndirectGroupBucket(corsaTreatment.treatment());
530 GroupBuckets buckets = new GroupBuckets(Collections.singletonList(bucket));
531 // group id == null, let group service determine group id
532 GroupDescription groupDescription = new DefaultGroupDescription(deviceId,
533 GroupDescription.Type.INDIRECT,
534 buckets,
535 key,
536 null,
537 nextObjective.appId());
538 groupService.addGroup(groupDescription);
539 pendingGroups.put(key, nextObjective);
540 } else if (corsaTreatment.type() == CorsaTrafficTreatmentType.ACTIONS) {
541 pendingNext.put(nextObjective.id(), nextObjective);
542 flowObjectiveStore.putNextGroup(nextObjective.id(), new CorsaGroup(key));
543 nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective));
544 }
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700545 }
546 break;
547 case HASHED:
548 case BROADCAST:
549 case FAILOVER:
550 fail(nextObjective, ObjectiveError.UNSUPPORTED);
551 log.warn("Unsupported next objective type {}", nextObjective.type());
552 break;
553 default:
554 fail(nextObjective, ObjectiveError.UNKNOWN);
555 log.warn("Unknown next objective type {}", nextObjective.type());
556 }
557
558 }
559
560 //Hook for altering the NextObjective treatment
Pier Ventredb673552016-07-20 15:37:19 +0200561 protected CorsaTrafficTreatment processNextTreatment(TrafficTreatment treatment) {
562 return new CorsaTrafficTreatment(CorsaTrafficTreatmentType.GROUP, treatment);
Michele Santuari9a8d16d2016-03-24 10:37:58 -0700563 }
564
565 //Init helper: Table Miss = Drop
566 protected void processTableMissDrop(boolean install, int table, String description) {
567 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
568
569 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
570 treatment.drop();
571
572 FlowRule rule = DefaultFlowRule.builder()
573 .forDevice(deviceId)
574 .withSelector(selector.build())
575 .withTreatment(treatment.build())
576 .withPriority(DROP_PRIORITY)
577 .fromApp(appId)
578 .makePermanent()
579 .forTable(table).build();
580
581 processFlowRule(install, rule, description);
582 }
583
584 //Init helper: Table Miss = GoTo
585 protected void processTableMissGoTo(boolean install, int table, int goTo, String description) {
586 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
587
588 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
589 treatment.transition(goTo);
590
591 FlowRule rule = DefaultFlowRule.builder()
592 .forDevice(deviceId)
593 .withSelector(selector.build())
594 .withTreatment(treatment.build())
595 .withPriority(DROP_PRIORITY)
596 .fromApp(appId)
597 .makePermanent()
598 .forTable(table).build();
599
600 processFlowRule(install, rule, description);
601 }
602
603 //Init helper: Apply flow rule
604 protected void processFlowRule(boolean install, FlowRule rule, String description) {
605 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
606 ops = install ? ops.add(rule) : ops.remove(rule);
607
608 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
609 @Override
610 public void onSuccess(FlowRuleOperations ops) {
611 log.info(description + " success: " + ops.toString() + ", " + rule.toString());
612 }
613
614 @Override
615 public void onError(FlowRuleOperations ops) {
616 log.info(description + " error: " + ops.toString() + ", " + rule.toString());
617 }
618 }));
619 }
620}