blob: 2deb83787858d9bf54935e6d60d0e3c2aad87a99 [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;
Ray Milkey0742ec92014-10-13 08:39:55 -07007
8import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Reference;
12import org.apache.felix.scr.annotations.ReferenceCardinality;
13import org.onlab.onos.ApplicationId;
14import org.onlab.onos.CoreService;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070015import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070016import org.onlab.onos.net.Link;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070017import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070018import org.onlab.onos.net.flow.DefaultFlowRule;
19import org.onlab.onos.net.flow.DefaultTrafficSelector;
20import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.FlowRuleBatchEntry;
22import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
23import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Ray Milkey0742ec92014-10-13 08:39:55 -070024import org.onlab.onos.net.flow.TrafficSelector;
25import org.onlab.onos.net.flow.TrafficTreatment;
26import org.onlab.onos.net.intent.IntentExtensionService;
27import org.onlab.onos.net.intent.IntentInstaller;
28import org.onlab.onos.net.intent.LinkCollectionIntent;
29import org.onlab.onos.net.intent.PathIntent;
30import org.slf4j.Logger;
31
32import com.google.common.collect.Lists;
33
Ray Milkey0742ec92014-10-13 08:39:55 -070034/**
35 * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent}
36 * path segment intents.
37 */
38@Component(immediate = true)
39public class LinkCollectionIntentInstaller implements IntentInstaller<LinkCollectionIntent> {
40
41 private final Logger log = getLogger(getClass());
42
43 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 protected IntentExtensionService intentManager;
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070047 protected CoreService coreService;
48
49 private ApplicationId appId;
50
51 @Activate
52 public void activate() {
53 appId = coreService.registerApplication("org.onlab.onos.net.intent");
54 intentManager.registerInstaller(LinkCollectionIntent.class, this);
55 }
56
57 @Deactivate
58 public void deactivate() {
59 intentManager.unregisterInstaller(PathIntent.class);
60 }
61
Ray Milkey0742ec92014-10-13 08:39:55 -070062 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070063 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Ray Milkey0742ec92014-10-13 08:39:55 -070064 TrafficSelector.Builder builder =
65 DefaultTrafficSelector.builder(intent.selector());
66 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
67 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070068 rules.add(createBatchEntry(FlowRuleOperation.ADD,
69 builder.build(),
70 link.src().deviceId(),
71 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070072 }
73
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070074 rules.add(createBatchEntry(FlowRuleOperation.ADD,
75 builder.build(),
76 intent.egressPoint().deviceId(),
77 intent.egressPoint().port()));
78
Brian O'Connorf2dbde52014-10-10 16:20:24 -070079 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -070080 }
81
82 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070083 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Ray Milkey0742ec92014-10-13 08:39:55 -070084 TrafficSelector.Builder builder =
85 DefaultTrafficSelector.builder(intent.selector());
86 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
87
88 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070089 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
90 builder.build(),
91 link.src().deviceId(),
92 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070093 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070094
95 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
96 builder.build(),
97 intent.egressPoint().deviceId(),
98 intent.egressPoint().port()));
99
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700100 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700101 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700102
103 /**
104 * Creates a FlowRuleBatchEntry based on the provided parameters.
105 *
106 * @param operation the FlowRuleOperation to use
107 * @param selector the traffic selector
108 * @param deviceId the device ID for the flow rule
109 * @param outPort the output port of the flow rule
110 * @return the new flow rule batch entry
111 */
112 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
113 TrafficSelector selector,
114 DeviceId deviceId,
115 PortNumber outPort) {
116
117 TrafficTreatment treatment = builder().setOutput(outPort).build();
118
119 FlowRule rule = new DefaultFlowRule(deviceId,
120 selector, treatment, 123, appId, 600);
121
122 return new FlowRuleBatchEntry(operation, rule);
123 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700124}