blob: 51e0d2e77e3e83cb24ba676490e9b895fdc992c5 [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent.impl;
2
3import java.util.List;
4import java.util.concurrent.Future;
5
6import org.apache.felix.scr.annotations.Activate;
7import org.apache.felix.scr.annotations.Component;
8import org.apache.felix.scr.annotations.Deactivate;
9import org.apache.felix.scr.annotations.Reference;
10import org.apache.felix.scr.annotations.ReferenceCardinality;
11import org.onlab.onos.ApplicationId;
12import org.onlab.onos.CoreService;
13import org.onlab.onos.net.Link;
14import org.onlab.onos.net.flow.CompletedBatchOperation;
15import org.onlab.onos.net.flow.DefaultFlowRule;
16import org.onlab.onos.net.flow.DefaultTrafficSelector;
17import org.onlab.onos.net.flow.FlowRule;
18import org.onlab.onos.net.flow.FlowRuleBatchEntry;
19import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
20import org.onlab.onos.net.flow.FlowRuleBatchOperation;
21import org.onlab.onos.net.flow.FlowRuleService;
22import org.onlab.onos.net.flow.TrafficSelector;
23import org.onlab.onos.net.flow.TrafficTreatment;
24import org.onlab.onos.net.intent.IntentExtensionService;
25import org.onlab.onos.net.intent.IntentInstaller;
26import org.onlab.onos.net.intent.LinkCollectionIntent;
27import org.onlab.onos.net.intent.PathIntent;
28import org.slf4j.Logger;
29
30import com.google.common.collect.Lists;
31
32import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent}
37 * path segment intents.
38 */
39@Component(immediate = true)
40public class LinkCollectionIntentInstaller implements IntentInstaller<LinkCollectionIntent> {
41
42 private final Logger log = getLogger(getClass());
43
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected IntentExtensionService intentManager;
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected FlowRuleService flowRuleService;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected CoreService coreService;
52
53 private ApplicationId appId;
54
55 @Activate
56 public void activate() {
57 appId = coreService.registerApplication("org.onlab.onos.net.intent");
58 intentManager.registerInstaller(LinkCollectionIntent.class, this);
59 }
60
61 @Deactivate
62 public void deactivate() {
63 intentManager.unregisterInstaller(PathIntent.class);
64 }
65
66 /**
67 * Apply a list of FlowRules.
68 *
69 * @param rules rules to apply
70 */
71 private Future<CompletedBatchOperation> applyBatch(List<FlowRuleBatchEntry> rules) {
72 FlowRuleBatchOperation batch = new FlowRuleBatchOperation(rules);
73 return flowRuleService.applyBatch(batch);
74 }
75
76 @Override
77 public Future<CompletedBatchOperation> install(LinkCollectionIntent intent) {
78 TrafficSelector.Builder builder =
79 DefaultTrafficSelector.builder(intent.selector());
80 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
81 for (Link link : intent.links()) {
82 TrafficTreatment treatment = builder()
83 .setOutput(link.src().port()).build();
84
85 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
86 builder.build(), treatment,
87 123, appId, 600);
88 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule));
89 }
90
91 return applyBatch(rules);
92 }
93
94 @Override
95 public Future<CompletedBatchOperation> uninstall(LinkCollectionIntent intent) {
96 TrafficSelector.Builder builder =
97 DefaultTrafficSelector.builder(intent.selector());
98 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
99
100 for (Link link : intent.links()) {
101 TrafficTreatment treatment = builder()
102 .setOutput(link.src().port()).build();
103 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
104 builder.build(), treatment,
105 123, appId, 600);
106 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule));
107 }
108 return applyBatch(rules);
109 }
110}