blob: 93c6345eb097bcf9775d8d1002e1cbb924b0804b [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.slf4j.LoggerFactory.getLogger;
4
Ray Milkey0742ec92014-10-13 08:39:55 -07005import java.util.List;
Ray Milkey0742ec92014-10-13 08:39:55 -07006
7import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Reference;
11import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070012import org.onlab.onos.core.ApplicationId;
13import org.onlab.onos.core.CoreService;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070014import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070015import org.onlab.onos.net.Link;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070016import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070017import org.onlab.onos.net.flow.DefaultFlowRule;
18import org.onlab.onos.net.flow.DefaultTrafficSelector;
Jonathan Hart6e88c682014-10-21 17:05:25 -070019import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Ray Milkey0742ec92014-10-13 08:39:55 -070020import 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 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
65 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070066 rules.add(createBatchEntry(FlowRuleOperation.ADD,
Jonathan Hart6e88c682014-10-21 17:05:25 -070067 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070068 link.src().deviceId(),
69 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070070 }
71
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070072 rules.add(createBatchEntry(FlowRuleOperation.ADD,
Jonathan Hart6e88c682014-10-21 17:05:25 -070073 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070074 intent.egressPoint().deviceId(),
75 intent.egressPoint().port()));
76
Brian O'Connorf2dbde52014-10-10 16:20:24 -070077 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -070078 }
79
80 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070081 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Ray Milkey0742ec92014-10-13 08:39:55 -070082 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
83
84 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070085 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
Jonathan Hart6e88c682014-10-21 17:05:25 -070086 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070087 link.src().deviceId(),
88 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070089 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070090
91 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
Jonathan Hart6e88c682014-10-21 17:05:25 -070092 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070093 intent.egressPoint().deviceId(),
94 intent.egressPoint().port()));
95
Brian O'Connorf2dbde52014-10-10 16:20:24 -070096 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -070097 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070098
99 /**
100 * Creates a FlowRuleBatchEntry based on the provided parameters.
101 *
102 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700103 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700104 * @param deviceId the device ID for the flow rule
105 * @param outPort the output port of the flow rule
106 * @return the new flow rule batch entry
107 */
108 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Jonathan Hart6e88c682014-10-21 17:05:25 -0700109 LinkCollectionIntent intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700110 DeviceId deviceId,
111 PortNumber outPort) {
112
Jonathan Hart6e88c682014-10-21 17:05:25 -0700113 TrafficTreatment.Builder treatmentBuilder =
114 DefaultTrafficTreatment.builder(intent.treatment());
115
116 TrafficTreatment treatment = treatmentBuilder.setOutput(outPort).build();
117
118 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector())
119 .build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700120
121 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700122 selector, treatment, 123, appId, 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700123
124 return new FlowRuleBatchEntry(operation, rule);
125 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700126}