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; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 7 | |
| 8 | import org.apache.felix.scr.annotations.Activate; |
| 9 | import org.apache.felix.scr.annotations.Component; |
| 10 | import org.apache.felix.scr.annotations.Deactivate; |
| 11 | import org.apache.felix.scr.annotations.Reference; |
| 12 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 13 | import org.onlab.onos.ApplicationId; |
| 14 | import org.onlab.onos.CoreService; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.DeviceId; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.Link; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 19 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 20 | import org.onlab.onos.net.flow.FlowRule; |
| 21 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 22 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 23 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.TrafficSelector; |
| 25 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 26 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 27 | import org.onlab.onos.net.intent.IntentInstaller; |
| 28 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 29 | import org.onlab.onos.net.intent.PathIntent; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import com.google.common.collect.Lists; |
| 33 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent} |
| 36 | * path segment intents. |
| 37 | */ |
| 38 | @Component(immediate = true) |
| 39 | public 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 47 | 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 62 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame^] | 63 | public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 64 | TrafficSelector.Builder builder = |
| 65 | DefaultTrafficSelector.builder(intent.selector()); |
| 66 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 67 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 68 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
| 69 | builder.build(), |
| 70 | link.src().deviceId(), |
| 71 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 74 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
| 75 | builder.build(), |
| 76 | intent.egressPoint().deviceId(), |
| 77 | intent.egressPoint().port())); |
| 78 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame^] | 79 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame^] | 83 | public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 84 | TrafficSelector.Builder builder = |
| 85 | DefaultTrafficSelector.builder(intent.selector()); |
| 86 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 87 | |
| 88 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 89 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
| 90 | builder.build(), |
| 91 | link.src().deviceId(), |
| 92 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 93 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 94 | |
| 95 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
| 96 | builder.build(), |
| 97 | intent.egressPoint().deviceId(), |
| 98 | intent.egressPoint().port())); |
| 99 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame^] | 100 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 101 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 102 | |
| 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 124 | } |