Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 3 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 6 | import java.util.List; |
| 7 | import java.util.concurrent.Future; |
| 8 | |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.onlab.onos.ApplicationId; |
| 15 | import org.onlab.onos.CoreService; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 16 | import org.onlab.onos.net.DeviceId; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.Link; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 18 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
| 20 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 21 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 22 | import org.onlab.onos.net.flow.FlowRule; |
| 23 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 24 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 25 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 26 | import org.onlab.onos.net.flow.FlowRuleService; |
| 27 | import org.onlab.onos.net.flow.TrafficSelector; |
| 28 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 29 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 30 | import org.onlab.onos.net.intent.IntentInstaller; |
| 31 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 32 | import org.onlab.onos.net.intent.PathIntent; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import com.google.common.collect.Lists; |
| 36 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent} |
| 39 | * path segment intents. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public 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 Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 84 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
| 85 | builder.build(), |
| 86 | link.src().deviceId(), |
| 87 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 90 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
| 91 | builder.build(), |
| 92 | intent.egressPoint().deviceId(), |
| 93 | intent.egressPoint().port())); |
| 94 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 95 | 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 Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 105 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
| 106 | builder.build(), |
| 107 | link.src().deviceId(), |
| 108 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 109 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 110 | |
| 111 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
| 112 | builder.build(), |
| 113 | intent.egressPoint().deviceId(), |
| 114 | intent.egressPoint().port())); |
| 115 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 116 | return applyBatch(rules); |
| 117 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame^] | 118 | |
| 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 140 | } |