blob: ec668dc493de6a2406ec56f232eae2c7f35a5c0e [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent.impl;
2
Jonathan Hart6b2ffc32014-10-18 02:09:22 -07003import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
4import static org.slf4j.LoggerFactory.getLogger;
5
Ray Milkey0742ec92014-10-13 08:39:55 -07006import java.util.List;
7import java.util.concurrent.Future;
8
9import org.apache.felix.scr.annotations.Activate;
10import org.apache.felix.scr.annotations.Component;
11import org.apache.felix.scr.annotations.Deactivate;
12import org.apache.felix.scr.annotations.Reference;
13import org.apache.felix.scr.annotations.ReferenceCardinality;
14import org.onlab.onos.ApplicationId;
15import org.onlab.onos.CoreService;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070016import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070017import org.onlab.onos.net.Link;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070018import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070019import org.onlab.onos.net.flow.CompletedBatchOperation;
20import org.onlab.onos.net.flow.DefaultFlowRule;
21import org.onlab.onos.net.flow.DefaultTrafficSelector;
22import org.onlab.onos.net.flow.FlowRule;
23import org.onlab.onos.net.flow.FlowRuleBatchEntry;
24import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
25import org.onlab.onos.net.flow.FlowRuleBatchOperation;
26import org.onlab.onos.net.flow.FlowRuleService;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.flow.TrafficTreatment;
29import org.onlab.onos.net.intent.IntentExtensionService;
30import org.onlab.onos.net.intent.IntentInstaller;
31import org.onlab.onos.net.intent.LinkCollectionIntent;
32import org.onlab.onos.net.intent.PathIntent;
33import org.slf4j.Logger;
34
35import com.google.common.collect.Lists;
36
Ray Milkey0742ec92014-10-13 08:39:55 -070037/**
38 * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent}
39 * path segment intents.
40 */
41@Component(immediate = true)
42public class LinkCollectionIntentInstaller implements IntentInstaller<LinkCollectionIntent> {
43
44 private final Logger log = getLogger(getClass());
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected IntentExtensionService intentManager;
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected FlowRuleService flowRuleService;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected CoreService coreService;
54
55 private ApplicationId appId;
56
57 @Activate
58 public void activate() {
59 appId = coreService.registerApplication("org.onlab.onos.net.intent");
60 intentManager.registerInstaller(LinkCollectionIntent.class, this);
61 }
62
63 @Deactivate
64 public void deactivate() {
65 intentManager.unregisterInstaller(PathIntent.class);
66 }
67
68 /**
69 * Apply a list of FlowRules.
70 *
71 * @param rules rules to apply
72 */
73 private Future<CompletedBatchOperation> applyBatch(List<FlowRuleBatchEntry> rules) {
74 FlowRuleBatchOperation batch = new FlowRuleBatchOperation(rules);
75 return flowRuleService.applyBatch(batch);
76 }
77
78 @Override
79 public Future<CompletedBatchOperation> install(LinkCollectionIntent intent) {
80 TrafficSelector.Builder builder =
81 DefaultTrafficSelector.builder(intent.selector());
82 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
83 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070084 rules.add(createBatchEntry(FlowRuleOperation.ADD,
85 builder.build(),
86 link.src().deviceId(),
87 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070088 }
89
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070090 rules.add(createBatchEntry(FlowRuleOperation.ADD,
91 builder.build(),
92 intent.egressPoint().deviceId(),
93 intent.egressPoint().port()));
94
Ray Milkey0742ec92014-10-13 08:39:55 -070095 return applyBatch(rules);
96 }
97
98 @Override
99 public Future<CompletedBatchOperation> uninstall(LinkCollectionIntent intent) {
100 TrafficSelector.Builder builder =
101 DefaultTrafficSelector.builder(intent.selector());
102 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
103
104 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700105 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
106 builder.build(),
107 link.src().deviceId(),
108 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -0700109 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700110
111 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
112 builder.build(),
113 intent.egressPoint().deviceId(),
114 intent.egressPoint().port()));
115
Ray Milkey0742ec92014-10-13 08:39:55 -0700116 return applyBatch(rules);
117 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700118
119 /**
120 * Creates a FlowRuleBatchEntry based on the provided parameters.
121 *
122 * @param operation the FlowRuleOperation to use
123 * @param selector the traffic selector
124 * @param deviceId the device ID for the flow rule
125 * @param outPort the output port of the flow rule
126 * @return the new flow rule batch entry
127 */
128 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
129 TrafficSelector selector,
130 DeviceId deviceId,
131 PortNumber outPort) {
132
133 TrafficTreatment treatment = builder().setOutput(outPort).build();
134
135 FlowRule rule = new DefaultFlowRule(deviceId,
136 selector, treatment, 123, appId, 600);
137
138 return new FlowRuleBatchEntry(operation, rule);
139 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700140}