blob: 5424558ded48b695ad59257ca80543faa781e572 [file] [log] [blame]
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -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 */
16package org.onosproject.driver.pipeline;
17
18import static org.onlab.util.Tools.groupedThreads;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import com.google.common.cache.Cache;
22import com.google.common.cache.CacheBuilder;
23import com.google.common.cache.RemovalCause;
24import com.google.common.cache.RemovalNotification;
25
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.packet.Ethernet;
28import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
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.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.Criteria;
48import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
51import org.onosproject.net.flow.criteria.IPCriterion;
52import org.onosproject.net.flow.criteria.MplsCriterion;
53import org.onosproject.net.flow.criteria.PortCriterion;
54import org.onosproject.net.flow.criteria.VlanIdCriterion;
55import org.onosproject.net.flow.instructions.Instruction;
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.ArrayList;
76import java.util.Collection;
77import java.util.Collections;
78import java.util.List;
79import java.util.Set;
80import java.util.concurrent.Executors;
81import java.util.concurrent.ScheduledExecutorService;
82import java.util.concurrent.TimeUnit;
83import java.util.stream.Collectors;
84
85/**
86 * Driver for SPRING-OPEN pipeline.
87 */
88public class SpringOpenTTP extends AbstractHandlerBehaviour
89 implements Pipeliner {
90
91 // Default table ID - compatible with CpqD switch
92 private static final int TABLE_VLAN = 0;
93 private static final int TABLE_TMAC = 1;
94 private static final int TABLE_IPV4_UNICAST = 2;
95 private static final int TABLE_MPLS = 3;
96 private static final int TABLE_ACL = 5;
97
98 /**
99 * Set the default values. These variables will get overwritten based on the
100 * switch vendor type
101 */
102 protected int vlanTableId = TABLE_VLAN;
103 protected int tmacTableId = TABLE_TMAC;
104 protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST;
105 protected int mplsTableId = TABLE_MPLS;
106 protected int aclTableId = TABLE_ACL;
107
108 protected final Logger log = getLogger(getClass());
109
110 private ServiceDirectory serviceDirectory;
111 private FlowRuleService flowRuleService;
112 private CoreService coreService;
113 protected GroupService groupService;
114 protected FlowObjectiveStore flowObjectiveStore;
115 protected DeviceId deviceId;
116 private ApplicationId appId;
117
118 private Cache<GroupKey, NextObjective> pendingGroups;
119
120 private ScheduledExecutorService groupChecker = Executors
121 .newScheduledThreadPool(2,
122 groupedThreads("onos/pipeliner",
123 "spring-open-%d"));
124 protected KryoNamespace appKryo = new KryoNamespace.Builder()
125 .register(GroupKey.class).register(DefaultGroupKey.class)
126 .register(SegmentRoutingGroup.class).register(byte[].class).build();
127
128 @Override
129 public void init(DeviceId deviceId, PipelinerContext context) {
130 this.serviceDirectory = context.directory();
131 this.deviceId = deviceId;
132
133 pendingGroups = CacheBuilder
134 .newBuilder()
135 .expireAfterWrite(20, TimeUnit.SECONDS)
136 .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> {
137 if (notification.getCause() == RemovalCause.EXPIRED) {
138 fail(notification.getValue(),
139 ObjectiveError.GROUPINSTALLATIONFAILED);
140 }
141 }).build();
142
143 groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500,
144 TimeUnit.MILLISECONDS);
145
146 coreService = serviceDirectory.get(CoreService.class);
147 flowRuleService = serviceDirectory.get(FlowRuleService.class);
148 groupService = serviceDirectory.get(GroupService.class);
149 flowObjectiveStore = context.store();
150
151 groupService.addListener(new InnerGroupListener());
152
153 appId = coreService
154 .registerApplication("org.onosproject.driver.SpringOpenTTP");
155
156 setTableMissEntries();
157 log.info("Spring Open TTP driver initialized");
158 }
159
160 @Override
161 public void filter(FilteringObjective filteringObjective) {
162 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
163 log.debug("processing PERMIT filter objective");
164 processFilter(filteringObjective,
165 filteringObjective.op() == Objective.Operation.ADD,
166 filteringObjective.appId());
167 } else {
168 log.debug("filter objective other than PERMIT not supported");
169 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
170 }
171 }
172
173 @Override
174 public void forward(ForwardingObjective fwd) {
175 Collection<FlowRule> rules;
176 FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder();
177
178 rules = processForward(fwd);
179 switch (fwd.op()) {
180 case ADD:
181 rules.stream().filter(rule -> rule != null)
182 .forEach(flowBuilder::add);
183 break;
184 case REMOVE:
185 rules.stream().filter(rule -> rule != null)
186 .forEach(flowBuilder::remove);
187 break;
188 default:
189 fail(fwd, ObjectiveError.UNKNOWN);
190 log.warn("Unknown forwarding type {}", fwd.op());
191 }
192
193 flowRuleService.apply(flowBuilder
194 .build(new FlowRuleOperationsContext() {
195 @Override
196 public void onSuccess(FlowRuleOperations ops) {
197 pass(fwd);
198 }
199
200 @Override
201 public void onError(FlowRuleOperations ops) {
202 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
203 }
204 }));
205
206 }
207
208 @Override
209 public void next(NextObjective nextObjective) {
sangho834e4b02015-05-01 09:38:25 -0700210
211 if (nextObjective.op() == Objective.Operation.REMOVE) {
212 if (nextObjective.next() == null) {
213 removeGroup(nextObjective);
214 } else {
215 removeBucketFromGroup(nextObjective);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700216 }
sangho834e4b02015-05-01 09:38:25 -0700217 } else if (nextObjective.op() == Objective.Operation.ADD) {
218 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
219 if (nextGroup != null) {
220 addBucketToGroup(nextObjective);
221 } else {
222 addGroup(nextObjective);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700223 }
sangho834e4b02015-05-01 09:38:25 -0700224 } else {
225 log.warn("Unsupported operation {}", nextObjective.op());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700226 }
227
228 }
229
sangho834e4b02015-05-01 09:38:25 -0700230 private void removeGroup(NextObjective nextObjective) {
231 final GroupKey key = new DefaultGroupKey(
232 appKryo.serialize(nextObjective.id()));
233 groupService.removeGroup(deviceId, key, appId);
234 }
235
236 private void addGroup(NextObjective nextObjective) {
237 switch (nextObjective.type()) {
238 case SIMPLE:
239 log.debug("processing SIMPLE next objective");
240 Collection<TrafficTreatment> treatments = nextObjective.next();
241 if (treatments.size() == 1) {
242 TrafficTreatment treatment = treatments.iterator().next();
243 GroupBucket bucket = DefaultGroupBucket
244 .createIndirectGroupBucket(treatment);
245 final GroupKey key = new DefaultGroupKey(
246 appKryo.serialize(nextObjective
247 .id()));
248 GroupDescription groupDescription = new DefaultGroupDescription(
249 deviceId,
250 GroupDescription.Type.INDIRECT,
251 new GroupBuckets(
252 Collections.singletonList(bucket)),
253 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700254 null,
sangho834e4b02015-05-01 09:38:25 -0700255 nextObjective.appId());
256 groupService.addGroup(groupDescription);
257 pendingGroups.put(key, nextObjective);
258 }
259 break;
260 case HASHED:
261 log.debug("processing HASHED next objective");
262 List<GroupBucket> buckets = nextObjective
263 .next()
264 .stream()
265 .map((treatment) -> DefaultGroupBucket
266 .createSelectGroupBucket(treatment))
267 .collect(Collectors.toList());
268 if (!buckets.isEmpty()) {
269 final GroupKey key = new DefaultGroupKey(
270 appKryo.serialize(nextObjective
271 .id()));
272 GroupDescription groupDescription = new DefaultGroupDescription(
273 deviceId,
274 GroupDescription.Type.SELECT,
275 new GroupBuckets(buckets),
276 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700277 null,
sangho834e4b02015-05-01 09:38:25 -0700278 nextObjective.appId());
279 groupService.addGroup(groupDescription);
280 pendingGroups.put(key, nextObjective);
281 }
282 break;
283 case BROADCAST:
284 case FAILOVER:
285 log.debug("BROADCAST and FAILOVER next objectives not supported");
286 fail(nextObjective, ObjectiveError.UNSUPPORTED);
287 log.warn("Unsupported next objective type {}", nextObjective.type());
288 break;
289 default:
290 fail(nextObjective, ObjectiveError.UNKNOWN);
291 log.warn("Unknown next objective type {}", nextObjective.type());
292 }
293 }
294
295 private void addBucketToGroup(NextObjective nextObjective) {
296 Collection<TrafficTreatment> treatments = nextObjective.next();
297 TrafficTreatment treatment = treatments.iterator().next();
298 final GroupKey key = new DefaultGroupKey(
299 appKryo.serialize(nextObjective
300 .id()));
301 Group group = groupService.getGroup(deviceId, key);
302 if (group == null) {
303 log.warn("Group is not found in {} for {}", deviceId, key);
304 return;
305 }
306 GroupBucket bucket;
307 if (group.type() == GroupDescription.Type.INDIRECT) {
308 bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment);
309 } else if (group.type() == GroupDescription.Type.SELECT) {
310 bucket = DefaultGroupBucket.createSelectGroupBucket(treatment);
311 } else {
312 log.warn("Unsupported Group type {}", group.type());
313 return;
314 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700315 GroupBuckets bucketsToAdd = new GroupBuckets(Collections.singletonList(bucket));
sangho834e4b02015-05-01 09:38:25 -0700316 groupService.addBucketsToGroup(deviceId, key, bucketsToAdd, key, appId);
317 }
318
319 private void removeBucketFromGroup(NextObjective nextObjective) {
320 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
321 if (nextGroup != null) {
322 Collection<TrafficTreatment> treatments = nextObjective.next();
323 TrafficTreatment treatment = treatments.iterator().next();
324 final GroupKey key = new DefaultGroupKey(
325 appKryo.serialize(nextObjective
326 .id()));
327 Group group = groupService.getGroup(deviceId, key);
328 if (group == null) {
329 log.warn("Group is not found in {} for {}", deviceId, key);
330 return;
331 }
332 GroupBucket bucket;
333 if (group.type() == GroupDescription.Type.INDIRECT) {
334 bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment);
335 } else if (group.type() == GroupDescription.Type.SELECT) {
336 bucket = DefaultGroupBucket.createSelectGroupBucket(treatment);
337 } else {
338 log.warn("Unsupported Group type {}", group.type());
339 return;
340 }
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700341 GroupBuckets removeBuckets = new GroupBuckets(Collections.singletonList(bucket));
sangho834e4b02015-05-01 09:38:25 -0700342 groupService.removeBucketsFromGroup(deviceId, key, removeBuckets, key, appId);
343 }
344 }
345
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700346 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
347 switch (fwd.flag()) {
348 case SPECIFIC:
349 return processSpecific(fwd);
350 case VERSATILE:
351 return processVersatile(fwd);
352 default:
353 fail(fwd, ObjectiveError.UNKNOWN);
354 log.warn("Unknown forwarding flag {}", fwd.flag());
355 }
356 return Collections.emptySet();
357 }
358
359 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
360 fail(fwd, ObjectiveError.UNSUPPORTED);
361 return Collections.emptySet();
362 }
363
364 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
365 log.debug("Processing specific");
366 TrafficSelector selector = fwd.selector();
367 EthTypeCriterion ethType = (EthTypeCriterion) selector
368 .getCriterion(Criterion.Type.ETH_TYPE);
369 if ((ethType == null) ||
370 ((((short) ethType.ethType()) != Ethernet.TYPE_IPV4) &&
371 (((short) ethType.ethType()) != Ethernet.MPLS_UNICAST))) {
372 log.debug("processSpecific: Unsupported "
373 + "forwarding objective criteraia");
374 fail(fwd, ObjectiveError.UNSUPPORTED);
375 return Collections.emptySet();
376 }
377
378 TrafficSelector.Builder filteredSelectorBuilder =
379 DefaultTrafficSelector.builder();
380 int forTableId = -1;
381 if (((short) ethType.ethType()) == Ethernet.TYPE_IPV4) {
382 filteredSelectorBuilder = filteredSelectorBuilder
383 .matchEthType(Ethernet.TYPE_IPV4)
384 .matchIPDst(((IPCriterion) selector
385 .getCriterion(Criterion.Type.IPV4_DST))
386 .ip());
387 forTableId = ipv4UnicastTableId;
388 log.debug("processing IPv4 specific forwarding objective");
389 } else {
390 filteredSelectorBuilder = filteredSelectorBuilder
391 .matchEthType(Ethernet.MPLS_UNICAST)
392 .matchMplsLabel(((MplsCriterion)
393 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
394 //TODO: Add Match for BoS
395 //if (selector.getCriterion(Criterion.Type.MPLS_BOS) != null) {
396 //}
397 forTableId = mplsTableId;
398 log.debug("processing MPLS specific forwarding objective");
399 }
400
401 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
402 .builder();
403 if (fwd.treatment() != null) {
404 for (Instruction i : fwd.treatment().allInstructions()) {
405 treatmentBuilder.add(i);
406 }
407 }
408
409 //TODO: Analyze the forwarding objective here to make
410 //device specific decision such as no ECMP groups in Dell
411 //switches.
412 if (fwd.nextId() != null) {
413 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
414
415 if (next != null) {
416 GroupKey key = appKryo.deserialize(next.data());
417
418 Group group = groupService.getGroup(deviceId, key);
419
420 if (group == null) {
421 log.warn("The group left!");
422 fail(fwd, ObjectiveError.GROUPMISSING);
423 return Collections.emptySet();
424 }
425 treatmentBuilder.group(group.id());
426 log.debug("Adding OUTGROUP action");
427 }
428 }
429
430 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
431 TrafficTreatment treatment = treatmentBuilder.transition(aclTableId)
432 .build();
433
434 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
435 .fromApp(fwd.appId()).withPriority(fwd.priority())
436 .forDevice(deviceId).withSelector(filteredSelector)
437 .withTreatment(treatment);
438
439 if (fwd.permanent()) {
440 ruleBuilder.makePermanent();
441 } else {
442 ruleBuilder.makeTemporary(fwd.timeout());
443 }
444
445 ruleBuilder.forTable(forTableId);
446 return Collections.singletonList(ruleBuilder.build());
447
448 }
449
450 protected List<FlowRule> processEthDstFilter(Criterion c,
451 FilteringObjective filt,
452 ApplicationId applicationId) {
453 List<FlowRule> rules = new ArrayList<FlowRule>();
454 EthCriterion e = (EthCriterion) c;
455 TrafficSelector.Builder selectorIp = DefaultTrafficSelector
456 .builder();
457 TrafficTreatment.Builder treatmentIp = DefaultTrafficTreatment
458 .builder();
459 selectorIp.matchEthDst(e.mac());
460 selectorIp.matchEthType(Ethernet.TYPE_IPV4);
461 treatmentIp.transition(ipv4UnicastTableId);
462 FlowRule ruleIp = DefaultFlowRule.builder().forDevice(deviceId)
463 .withSelector(selectorIp.build())
464 .withTreatment(treatmentIp.build())
465 .withPriority(filt.priority()).fromApp(applicationId)
466 .makePermanent().forTable(tmacTableId).build();
467 log.debug("adding IP ETH rule for MAC: {}", e.mac());
468 rules.add(ruleIp);
469
470 TrafficSelector.Builder selectorMpls = DefaultTrafficSelector
471 .builder();
472 TrafficTreatment.Builder treatmentMpls = DefaultTrafficTreatment
473 .builder();
474 selectorMpls.matchEthDst(e.mac());
475 selectorMpls.matchEthType(Ethernet.MPLS_UNICAST);
476 treatmentMpls.transition(mplsTableId);
477 FlowRule ruleMpls = DefaultFlowRule.builder()
478 .forDevice(deviceId).withSelector(selectorMpls.build())
479 .withTreatment(treatmentMpls.build())
480 .withPriority(filt.priority()).fromApp(applicationId)
481 .makePermanent().forTable(tmacTableId).build();
482 log.debug("adding MPLS ETH rule for MAC: {}", e.mac());
483 rules.add(ruleMpls);
484
485 return rules;
486 }
487
488 private void processFilter(FilteringObjective filt, boolean install,
489 ApplicationId applicationId) {
490 // This driver only processes filtering criteria defined with switch
491 // ports as the key
492 PortCriterion p;
493 if (!filt.key().equals(Criteria.dummy())
494 && filt.key().type() == Criterion.Type.IN_PORT) {
495 p = (PortCriterion) filt.key();
496 } else {
497 log.warn("No key defined in filtering objective from app: {}. Not"
498 + "processing filtering objective", applicationId);
499 fail(filt, ObjectiveError.UNKNOWN);
500 return;
501 }
502 // convert filtering conditions for switch-intfs into flowrules
503 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
504 for (Criterion c : filt.conditions()) {
505 if (c.type() == Criterion.Type.ETH_DST) {
506 for (FlowRule rule : processEthDstFilter(c,
507 filt,
508 applicationId)) {
509 ops = install ? ops.add(rule) : ops.remove(rule);
510 }
511 } else if (c.type() == Criterion.Type.VLAN_VID) {
512 VlanIdCriterion v = (VlanIdCriterion) c;
513 log.debug("adding rule for VLAN: {}", v.vlanId());
514 TrafficSelector.Builder selector = DefaultTrafficSelector
515 .builder();
516 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
517 .builder();
518 if (v.vlanId() != VlanId.NONE) {
519 selector.matchVlanId(v.vlanId());
520 selector.matchInPort(p.port());
521 treatment.deferred().popVlan();
522 }
523 treatment.transition(tmacTableId);
524 FlowRule rule = DefaultFlowRule.builder().forDevice(deviceId)
525 .withSelector(selector.build())
526 .withTreatment(treatment.build())
527 .withPriority(filt.priority()).fromApp(applicationId)
528 .makePermanent().forTable(vlanTableId).build();
529 ops = install ? ops.add(rule) : ops.remove(rule);
530 } else if (c.type() == Criterion.Type.IPV4_DST) {
531 IPCriterion ip = (IPCriterion) c;
532 log.debug("adding rule for IP: {}", ip.ip());
533 TrafficSelector.Builder selector = DefaultTrafficSelector
534 .builder();
535 TrafficTreatment.Builder treatment = DefaultTrafficTreatment
536 .builder();
537 selector.matchEthType(Ethernet.TYPE_IPV4);
538 selector.matchIPDst(ip.ip());
539 treatment.transition(aclTableId);
540 FlowRule rule = DefaultFlowRule.builder().forDevice(deviceId)
541 .withSelector(selector.build())
542 .withTreatment(treatment.build())
543 .withPriority(filt.priority()).fromApp(applicationId)
544 .makePermanent().forTable(ipv4UnicastTableId).build();
545 ops = install ? ops.add(rule) : ops.remove(rule);
546 } else {
547 log.warn("Driver does not currently process filtering condition"
548 + " of type: {}", c.type());
549 fail(filt, ObjectiveError.UNSUPPORTED);
550 }
551 }
552 // apply filtering flow rules
553 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
554 @Override
555 public void onSuccess(FlowRuleOperations ops) {
556 pass(filt);
557 log.info("Provisioned tables for segment router");
558 }
559
560 @Override
561 public void onError(FlowRuleOperations ops) {
562 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
563 log.info("Failed to provision tables for segment router");
564 }
565 }));
566 }
567
568 protected void setTableMissEntries() {
569 // set all table-miss-entries
570 populateTableMissEntry(vlanTableId, true, false, false, -1);
571 populateTableMissEntry(tmacTableId, true, false, false, -1);
572 populateTableMissEntry(ipv4UnicastTableId, false, true, true,
573 aclTableId);
574 populateTableMissEntry(mplsTableId, false, true, true, aclTableId);
575 populateTableMissEntry(aclTableId, false, false, false, -1);
576 }
577
578 protected void populateTableMissEntry(int tableToAdd,
579 boolean toControllerNow,
580 boolean toControllerWrite,
581 boolean toTable, int tableToSend) {
582 TrafficSelector selector = DefaultTrafficSelector.builder().build();
583 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
584
585 if (toControllerNow) {
586 tBuilder.setOutput(PortNumber.CONTROLLER);
587 }
588
589 if (toControllerWrite) {
590 tBuilder.deferred().setOutput(PortNumber.CONTROLLER);
591 }
592
593 if (toTable) {
594 tBuilder.transition(tableToSend);
595 }
596
597 FlowRule flow = DefaultFlowRule.builder().forDevice(deviceId)
598 .withSelector(selector).withTreatment(tBuilder.build())
599 .withPriority(0).fromApp(appId).makePermanent()
600 .forTable(tableToAdd).build();
601
602 flowRuleService.applyFlowRules(flow);
603 }
604
605 private void pass(Objective obj) {
606 if (obj.context().isPresent()) {
607 obj.context().get().onSuccess(obj);
608 }
609 }
610
611 protected void fail(Objective obj, ObjectiveError error) {
612 if (obj.context().isPresent()) {
613 obj.context().get().onError(obj, error);
614 }
615 }
616
617 private class InnerGroupListener implements GroupListener {
618 @Override
619 public void event(GroupEvent event) {
620 if (event.type() == GroupEvent.Type.GROUP_ADDED) {
621 GroupKey key = event.subject().appCookie();
622
623 NextObjective obj = pendingGroups.getIfPresent(key);
624 if (obj != null) {
625 flowObjectiveStore
626 .putNextGroup(obj.id(),
627 new SegmentRoutingGroup(key));
628 pass(obj);
629 pendingGroups.invalidate(key);
630 }
631 }
632 }
633 }
634
635 private class GroupChecker implements Runnable {
636
637 @Override
638 public void run() {
639 Set<GroupKey> keys = pendingGroups
640 .asMap()
641 .keySet()
642 .stream()
643 .filter(key -> groupService.getGroup(deviceId, key) != null)
644 .collect(Collectors.toSet());
645
646 keys.stream()
647 .forEach(key -> {
648 NextObjective obj = pendingGroups
649 .getIfPresent(key);
650 if (obj == null) {
651 return;
652 }
653 pass(obj);
654 pendingGroups.invalidate(key);
655 flowObjectiveStore.putNextGroup(obj.id(),
656 new SegmentRoutingGroup(
657 key));
658 });
659 }
660 }
661
662 private class SegmentRoutingGroup implements NextGroup {
663
664 private final GroupKey key;
665
666 public SegmentRoutingGroup(GroupKey key) {
667 this.key = key;
668 }
669
670 public GroupKey key() {
671 return key;
672 }
673
674 @Override
675 public byte[] data() {
676 return appKryo.serialize(key);
677 }
678
679 }
680}