blob: cd7a1b66f5045fe1db717b800811fe2dfdf7ab63 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 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 */
Saurav Dase3274c82015-05-24 17:21:56 -070016package org.onosproject.driver.pipeline;
17
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;
23
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.packet.Ethernet;
26import org.onlab.packet.IPv4;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070027import org.onlab.packet.TpPort;
Saurav Dase3274c82015-05-24 17:21:56 -070028import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.DeviceId;
Saurav Das6c44a632015-05-30 22:05:22 -070033import org.onosproject.net.PortNumber;
Saurav Dase3274c82015-05-24 17:21:56 -070034import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
44import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criterion;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.PortCriterion;
51import org.onosproject.net.flow.criteria.EthTypeCriterion;
52import org.onosproject.net.flow.criteria.IPCriterion;
53import org.onosproject.net.flow.criteria.VlanIdCriterion;
54import org.onosproject.net.flow.instructions.Instruction;
55import org.onosproject.net.flow.instructions.L2ModificationInstruction;
56import org.onosproject.net.flowobjective.FilteringObjective;
57import org.onosproject.net.flowobjective.FlowObjectiveStore;
58import org.onosproject.net.flowobjective.ForwardingObjective;
59import org.onosproject.net.flowobjective.NextObjective;
60import org.onosproject.net.flowobjective.Objective;
61import org.onosproject.net.flowobjective.ObjectiveError;
62import org.onosproject.net.group.DefaultGroupBucket;
63import org.onosproject.net.group.DefaultGroupDescription;
64import org.onosproject.net.group.DefaultGroupKey;
65import org.onosproject.net.group.Group;
66import org.onosproject.net.group.GroupBucket;
67import org.onosproject.net.group.GroupBuckets;
68import org.onosproject.net.group.GroupDescription;
69import org.onosproject.net.group.GroupEvent;
70import org.onosproject.net.group.GroupKey;
71import org.onosproject.net.group.GroupListener;
72import org.onosproject.net.group.GroupService;
73import org.slf4j.Logger;
74
75import java.util.Collection;
76import java.util.Collections;
77import java.util.Set;
78import java.util.concurrent.Executors;
79import java.util.concurrent.ScheduledExecutorService;
80import java.util.concurrent.TimeUnit;
81import java.util.stream.Collectors;
82
83import static org.onlab.util.Tools.groupedThreads;
84import static org.slf4j.LoggerFactory.getLogger;
85
86/**
87 * Driver for Centec's V350 switches.
88 */
89public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
90
91 protected static final int PORT_VLAN_TABLE = 0;
92 protected static final int FILTER_TABLE = 1;
93 // TMAC is configured in MAC Table to redirect packets to ROUTE_TABLE.
94 protected static final int MAC_TABLE = 2;
95 protected static final int ROUTE_TABLE = 3;
96
97 private static final long DEFAULT_METADATA = 100;
Saurav Das6c44a632015-05-30 22:05:22 -070098 private static final long DEFAULT_METADATA_MASK = 0xffffffffffffffffL;
Saurav Dase3274c82015-05-24 17:21:56 -070099
100 // Priority used in PORT_VLAN Table, the only priority accepted is PORT_VLAN_TABLE_PRIORITY.
101 // The packet passed PORT+VLAN check will goto FILTER Table.
102 private static final int PORT_VLAN_TABLE_PRIORITY = 0xffff;
103
104 // Priority used in Filter Table.
105 private static final int FILTER_TABLE_CONTROLLER_PRIORITY = 500;
106 // TMAC priority should be lower than controller.
107 private static final int FILTER_TABLE_TMAC_PRIORITY = 200;
108 private static final int FILTER_TABLE_HIGHEST_PRIORITY = 0xffff;
109
110 // Priority used in MAC Table.
111 // We do exact matching for DMAC+metadata, so priority is ignored and required to be set to 0xffff.
112 private static final int MAC_TABLE_PRIORITY = 0xffff;
113
114 // Priority used in Route Table.
115 // We do LPM matching in Route Table, so priority is ignored and required to be set to 0xffff.
116 private static final int ROUTE_TABLE_PRIORITY = 0xffff;
117
118 private static final short BGP_PORT = 179;
119
120 private final Logger log = getLogger(getClass());
121
122 private ServiceDirectory serviceDirectory;
123 private FlowRuleService flowRuleService;
124 private CoreService coreService;
125 private GroupService groupService;
126 private FlowObjectiveStore flowObjectiveStore;
127 private DeviceId deviceId;
128 private ApplicationId appId;
129
130 private KryoNamespace appKryo = new KryoNamespace.Builder()
131 .register(GroupKey.class)
132 .register(DefaultGroupKey.class)
133 .register(CentecV350Group.class)
134 .register(byte[].class)
135 .build();
136
137 private Cache<GroupKey, NextObjective> pendingGroups;
138
139 private ScheduledExecutorService groupChecker =
140 Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner",
141 "centec-V350-%d"));
142
143 @Override
144 public void init(DeviceId deviceId, PipelinerContext context) {
145 this.serviceDirectory = context.directory();
146 this.deviceId = deviceId;
147
148 pendingGroups = CacheBuilder.newBuilder()
149 .expireAfterWrite(20, TimeUnit.SECONDS)
150 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
151 if (notification.getCause() == RemovalCause.EXPIRED) {
152 fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED);
153 }
154 }).build();
155
156 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS);
157
158 coreService = serviceDirectory.get(CoreService.class);
159 flowRuleService = serviceDirectory.get(FlowRuleService.class);
160 groupService = serviceDirectory.get(GroupService.class);
161 flowObjectiveStore = context.store();
162
163 groupService.addListener(new InnerGroupListener());
164
165 appId = coreService.registerApplication(
166 "org.onosproject.driver.CentecV350Pipeline");
167
168 initializePipeline();
169 }
170
171 @Override
172 public void filter(FilteringObjective filteringObjective) {
173 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
174 processFilter(filteringObjective,
175 filteringObjective.op() == Objective.Operation.ADD,
176 filteringObjective.appId());
177 } else {
178 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
179 }
180 }
181
182 @Override
183 public void forward(ForwardingObjective fwd) {
184 Collection<FlowRule> rules;
185 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
186
187 rules = processForward(fwd);
188 switch (fwd.op()) {
189 case ADD:
190 rules.stream()
191 .filter(rule -> rule != null)
192 .forEach(flowBuilder::add);
193 break;
194 case REMOVE:
195 rules.stream()
196 .filter(rule -> rule != null)
197 .forEach(flowBuilder::remove);
198 break;
199 default:
200 fail(fwd, ObjectiveError.UNKNOWN);
201 log.warn("Unknown forwarding type {}", fwd.op());
202 }
203
204
205 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
206 @Override
207 public void onSuccess(FlowRuleOperations ops) {
208 pass(fwd);
209 }
210
211 @Override
212 public void onError(FlowRuleOperations ops) {
213 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
214 }
215 }));
216
217 }
218
219 @Override
220 public void next(NextObjective nextObjective) {
221 switch (nextObjective.type()) {
222 case SIMPLE:
223 Collection<TrafficTreatment> treatments = nextObjective.next();
224 if (treatments.size() == 1) {
225 TrafficTreatment treatment = treatments.iterator().next();
226
227 // Since we do not support strip_vlan in PORT_VLAN table, we use mod_vlan
228 // to modify the packet to desired vlan.
229 // Note: if we use push_vlan here, the switch will add a second VLAN tag to the outgoing
230 // packet, which is not what we want.
231 TrafficTreatment.Builder treatmentWithoutPushVlan = DefaultTrafficTreatment.builder();
232 VlanId modVlanId;
233 for (Instruction ins : treatment.allInstructions()) {
234 if (ins.type() == Instruction.Type.L2MODIFICATION) {
235 L2ModificationInstruction l2ins = (L2ModificationInstruction) ins;
236 switch (l2ins.subtype()) {
237 case ETH_DST:
238 treatmentWithoutPushVlan.setEthDst(
239 ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac());
240 break;
241 case ETH_SRC:
242 treatmentWithoutPushVlan.setEthSrc(
243 ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac());
244 break;
245 case VLAN_ID:
246 modVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId();
247 treatmentWithoutPushVlan.setVlanId(modVlanId);
248 break;
249 default:
250 break;
251 }
252 } else if (ins.type() == Instruction.Type.OUTPUT) {
253 //long portNum = ((Instructions.OutputInstruction) ins).port().toLong();
254 treatmentWithoutPushVlan.add(ins);
255 } else {
256 // Ignore the vlan_pcp action since it's does matter much.
257 log.warn("Driver does not handle this type of TrafficTreatment"
258 + " instruction in nextObjectives: {}", ins.type());
259 }
260 }
261
262 GroupBucket bucket =
263 DefaultGroupBucket.createIndirectGroupBucket(treatmentWithoutPushVlan.build());
264 final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id()));
265 GroupDescription groupDescription
266 = new DefaultGroupDescription(deviceId,
267 GroupDescription.Type.INDIRECT,
268 new GroupBuckets(Collections
269 .singletonList(bucket)),
270 key,
271 null, // let group service determine group id
272 nextObjective.appId());
273 groupService.addGroup(groupDescription);
274 pendingGroups.put(key, nextObjective);
275 }
276 break;
277 case HASHED:
278 case BROADCAST:
279 case FAILOVER:
280 fail(nextObjective, ObjectiveError.UNSUPPORTED);
281 log.warn("Unsupported next objective type {}", nextObjective.type());
282 break;
283 default:
284 fail(nextObjective, ObjectiveError.UNKNOWN);
285 log.warn("Unknown next objective type {}", nextObjective.type());
286 }
287
288 }
289
290 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
291 switch (fwd.flag()) {
292 case SPECIFIC:
293 return processSpecific(fwd);
294 case VERSATILE:
295 return processVersatile(fwd);
296 default:
297 fail(fwd, ObjectiveError.UNKNOWN);
298 log.warn("Unknown forwarding flag {}", fwd.flag());
299 }
300 return Collections.emptySet();
301 }
302
303 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
304 log.warn("Driver does not support versatile forwarding objective");
305 fail(fwd, ObjectiveError.UNSUPPORTED);
306 return Collections.emptySet();
307 }
308
309 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
310 log.debug("Processing specific forwarding objective");
311 TrafficSelector selector = fwd.selector();
312 EthTypeCriterion ethType =
313 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700314 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dase3274c82015-05-24 17:21:56 -0700315 fail(fwd, ObjectiveError.UNSUPPORTED);
316 return Collections.emptySet();
317 }
318
319 // Must have metadata as key.
320 TrafficSelector filteredSelector =
321 DefaultTrafficSelector.builder()
322 .matchEthType(Ethernet.TYPE_IPV4)
323 .matchMetadata(DEFAULT_METADATA)
324 .matchIPDst(
325 ((IPCriterion)
326 selector.getCriterion(Criterion.Type.IPV4_DST)).ip())
327 .build();
328
329 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
330
331 if (fwd.nextId() != null) {
332 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
333 GroupKey key = appKryo.deserialize(next.data());
334 Group group = groupService.getGroup(deviceId, key);
335 if (group == null) {
336 log.warn("The group left!");
337 fail(fwd, ObjectiveError.GROUPMISSING);
338 return Collections.emptySet();
339 }
340 tb.group(group.id());
341 }
342
343 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
344 .fromApp(fwd.appId())
345 .withPriority(ROUTE_TABLE_PRIORITY)
346 .forDevice(deviceId)
347 .withSelector(filteredSelector)
348 .withTreatment(tb.build());
349
350 if (fwd.permanent()) {
351 ruleBuilder.makePermanent();
352 } else {
353 ruleBuilder.makeTemporary(fwd.timeout());
354 }
355
356 ruleBuilder.forTable(ROUTE_TABLE);
357
358 return Collections.singletonList(ruleBuilder.build());
359
360 }
361
362 private void processFilter(FilteringObjective filt, boolean install,
363 ApplicationId applicationId) {
364 PortCriterion p;
365 if (!filt.key().equals(Criteria.dummy()) &&
366 filt.key().type() == Criterion.Type.IN_PORT) {
367 p = (PortCriterion) filt.key();
368 } else {
369 log.warn("No key defined in filtering objective from app: {}. Not"
370 + "processing filtering objective", applicationId);
371 fail(filt, ObjectiveError.UNKNOWN);
372 return;
373 }
374
375 // Convert filtering conditions for switch-intfs into flow rules.
376 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
377
378 for (Criterion c : filt.conditions()) {
379 // Here we do a trick to install 2 flow rules to MAC_TABLE and ROUTE_TABLE.
380 if (c.type() == Criterion.Type.ETH_DST) {
381 EthCriterion e = (EthCriterion) c;
382
383 // Install TMAC flow rule.
384 log.debug("adding rule for Termination MAC in Filter Table: {}", e.mac());
385 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
386 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
387 selector.matchEthDst(e.mac());
388 // Add IPv4 matching explicitly since we will redirect it to ROUTE Table
389 // through MAC table.
390 selector.matchEthType(Ethernet.TYPE_IPV4);
391 treatment.transition(MAC_TABLE);
392 FlowRule rule = DefaultFlowRule.builder()
393 .forDevice(deviceId)
394 .withSelector(selector.build())
395 .withTreatment(treatment.build())
396 .withPriority(FILTER_TABLE_TMAC_PRIORITY)
397 .fromApp(applicationId)
398 .makePermanent()
399 .forTable(FILTER_TABLE).build();
400 ops = install ? ops.add(rule) : ops.remove(rule);
401
402 // Must install another rule to direct the IPv4 packets that hit TMAC to
403 // Route table.
404 log.debug("adding rule for Termination MAC in MAC Table: {}", e.mac());
405 selector = DefaultTrafficSelector.builder();
406 treatment = DefaultTrafficTreatment.builder();
407 selector.matchEthDst(e.mac());
408 // MAC_Table must have metadata matching configured, use the default metadata.
409 selector.matchMetadata(DEFAULT_METADATA);
410 treatment.transition(ROUTE_TABLE);
411 rule = DefaultFlowRule.builder()
412 .forDevice(deviceId)
413 .withSelector(selector.build())
414 .withTreatment(treatment.build())
415 .withPriority(MAC_TABLE_PRIORITY)
416 .fromApp(applicationId)
417 .makePermanent()
418 .forTable(MAC_TABLE).build();
419 ops = install ? ops.add(rule) : ops.remove(rule);
420 } else if (c.type() == Criterion.Type.VLAN_VID) {
421 VlanIdCriterion v = (VlanIdCriterion) c;
422 log.debug("adding rule for VLAN: {}", v.vlanId());
423 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
424 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
425 selector.matchVlanId(v.vlanId());
426 selector.matchInPort(p.port());
427 // Although the accepted packets will be sent to filter table, we must
428 // explicitly set goto_table instruction here.
Saurav Das86af8f12015-05-25 23:55:33 -0700429 treatment.writeMetadata(DEFAULT_METADATA, DEFAULT_METADATA_MASK);
430 // set default metadata written by PORT_VLAN Table.
Saurav Dase3274c82015-05-24 17:21:56 -0700431 treatment.transition(FILTER_TABLE);
432 // We do not support strip vlan here, treatment.deferred().popVlan();
Saurav Dase3274c82015-05-24 17:21:56 -0700433 // PORT_VLAN table only accept 0xffff priority since it does exact match only.
434 FlowRule rule = DefaultFlowRule.builder()
435 .forDevice(deviceId)
436 .withSelector(selector.build())
437 .withTreatment(treatment.build())
438 .withPriority(PORT_VLAN_TABLE_PRIORITY)
439 .fromApp(applicationId)
440 .makePermanent()
441 .forTable(PORT_VLAN_TABLE).build();
442 ops = install ? ops.add(rule) : ops.remove(rule);
443 } else if (c.type() == Criterion.Type.IPV4_DST) {
Saurav Das6c44a632015-05-30 22:05:22 -0700444 IPCriterion ipaddr = (IPCriterion) c;
445 log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip());
446 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
447 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
448 selector.matchEthType(Ethernet.TYPE_IPV4);
449 selector.matchIPDst(ipaddr.ip()); // router IPs to the controller
450 treatment.setOutput(PortNumber.CONTROLLER);
451 FlowRule rule = DefaultFlowRule.builder()
452 .forDevice(deviceId)
453 .withSelector(selector.build())
454 .withTreatment(treatment.build())
455 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
456 .fromApp(applicationId)
457 .makePermanent()
458 .forTable(FILTER_TABLE).build();
459 ops = install ? ops.add(rule) : ops.remove(rule);
Saurav Dase3274c82015-05-24 17:21:56 -0700460 } else {
461 log.warn("Driver does not currently process filtering condition"
462 + " of type: {}", c.type());
463 fail(filt, ObjectiveError.UNSUPPORTED);
464 }
465 }
466
467 // apply filtering flow rules
468 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
469 @Override
470 public void onSuccess(FlowRuleOperations ops) {
471 pass(filt);
472 log.info("Applied filtering rules");
473 }
474
475 @Override
476 public void onError(FlowRuleOperations ops) {
477 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
478 log.info("Failed to apply filtering rules");
479 }
480 }));
481 }
482
483 private void pass(Objective obj) {
484 if (obj.context().isPresent()) {
485 obj.context().get().onSuccess(obj);
486 }
487 }
488
489 private void fail(Objective obj, ObjectiveError error) {
490 if (obj.context().isPresent()) {
491 obj.context().get().onError(obj, error);
492 }
493 }
494
495 private void initializePipeline() {
496 // CENTEC_V350: PORT_VLAN_TABLE->FILTER_TABLE->MAC_TABLE(TMAC)->ROUTE_TABLE.
497 processPortVlanTable(true);
498 processFilterTable(true);
499 }
500
501 private void processPortVlanTable(boolean install) {
502 // By default the packet are dropped, need install port+vlan by some ways.
503
504 // XXX can we add table-miss-entry to drop? Code says drops by default
505 // XXX TTP description says default goes to table1.
506 // It also says that match is only on vlan -- not port-vlan -- which one is true?
507 }
508
509 private void processFilterTable(boolean install) {
510 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
511 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
512 .builder();
513 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
514 FlowRule rule;
515
516 // Punt ARP packets to controller by default.
517 selector.matchEthType(Ethernet.TYPE_ARP);
518 treatment.punt();
519 rule = DefaultFlowRule.builder()
520 .forDevice(deviceId)
521 .withSelector(selector.build())
522 .withTreatment(treatment.build())
523 .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY)
524 .fromApp(appId)
525 .makePermanent()
526 .forTable(FILTER_TABLE).build();
527 ops = install ? ops.add(rule) : ops.remove(rule);
528
529 // Punt BGP packets to controller directly.
530 selector = DefaultTrafficSelector.builder();
531 treatment = DefaultTrafficTreatment.builder();
532 selector.matchEthType(Ethernet.TYPE_IPV4)
533 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700534 .matchTcpSrc(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700535 treatment.punt();
536 rule = DefaultFlowRule.builder()
537 .forDevice(deviceId)
538 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
539 .withSelector(selector.build())
540 .withTreatment(treatment.build())
541 .fromApp(appId)
542 .makePermanent()
543 .forTable(FILTER_TABLE).build();
544 ops = install ? ops.add(rule) : ops.remove(rule);
545
546 selector = DefaultTrafficSelector.builder();
547 treatment = DefaultTrafficTreatment.builder();
548 selector.matchEthType(Ethernet.TYPE_IPV4)
549 .matchIPProtocol(IPv4.PROTOCOL_TCP)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700550 .matchTcpDst(TpPort.tpPort(BGP_PORT));
Saurav Dase3274c82015-05-24 17:21:56 -0700551 treatment.punt();
552 rule = DefaultFlowRule.builder()
553 .forDevice(deviceId)
554 .withPriority(FILTER_TABLE_HIGHEST_PRIORITY)
555 .withSelector(selector.build())
556 .withTreatment(treatment.build())
557 .fromApp(appId)
558 .makePermanent()
559 .forTable(FILTER_TABLE).build();
560
561 ops = install ? ops.add(rule) : ops.remove(rule);
562
563 // Packet will be discard in PORT_VLAN table, no need to install rule in
564 // filter table.
565
566 // XXX does not tell me if packets are going to be dropped by default in
567 // filter table or not? TTP says it will be dropped by default
568
569 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
570 @Override
571 public void onSuccess(FlowRuleOperations ops) {
572 log.info("Provisioned filter table");
573 }
574
575 @Override
576 public void onError(FlowRuleOperations ops) {
577 log.info("Failed to provision filter table");
578 }
579 }));
580 }
581
582 private class InnerGroupListener implements GroupListener {
583 @Override
584 public void event(GroupEvent event) {
585 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
586 GroupKey key = event.subject().appCookie();
587
588 NextObjective obj = pendingGroups.getIfPresent(key);
589 if (obj != null) {
590 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
591 pass(obj);
592 pendingGroups.invalidate(key);
593 }
594 }
595 }
596 }
597
598
599 private class GroupChecker implements Runnable {
600
601 @Override
602 public void run() {
603 Set<GroupKey> keys = pendingGroups.asMap().keySet().stream()
604 .filter(key -> groupService.getGroup(deviceId, key) != null)
605 .collect(Collectors.toSet());
606
607 keys.stream().forEach(key -> {
608 NextObjective obj = pendingGroups.getIfPresent(key);
609 if (obj == null) {
610 return;
611 }
612 pass(obj);
613 pendingGroups.invalidate(key);
614 log.info("Heard back from group service for group {}. "
615 + "Applying pending forwarding objectives", obj.id());
616 flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key));
617 });
618 }
619 }
620
621 private class CentecV350Group implements NextGroup {
622
623 private final GroupKey key;
624
625 public CentecV350Group(GroupKey key) {
626 this.key = key;
627 }
628
629 @SuppressWarnings("unused")
630 public GroupKey key() {
631 return key;
632 }
633
634 @Override
635 public byte[] data() {
636 return appKryo.serialize(key);
637 }
638
639 }
640}