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.slf4j.LoggerFactory.getLogger; |
| 4 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 5 | import java.util.List; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 6 | |
| 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 12 | import org.onlab.onos.ApplicationId; |
| 13 | import org.onlab.onos.CoreService; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.DeviceId; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.Link; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 18 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 19 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 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 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 65 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 66 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 67 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 68 | link.src().deviceId(), |
| 69 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 72 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 73 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 74 | intent.egressPoint().deviceId(), |
| 75 | intent.egressPoint().port())); |
| 76 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 77 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 81 | public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 82 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 83 | |
| 84 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 85 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 86 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 87 | link.src().deviceId(), |
| 88 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 89 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 90 | |
| 91 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 92 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 93 | intent.egressPoint().deviceId(), |
| 94 | intent.egressPoint().port())); |
| 95 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 96 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 97 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Creates a FlowRuleBatchEntry based on the provided parameters. |
| 101 | * |
| 102 | * @param operation the FlowRuleOperation to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 103 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 104 | * @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 Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 109 | LinkCollectionIntent intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 110 | DeviceId deviceId, |
| 111 | PortNumber outPort) { |
| 112 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 113 | 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 Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 120 | |
| 121 | FlowRule rule = new DefaultFlowRule(deviceId, |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 122 | selector, treatment, 123, appId, 0, true); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 123 | |
| 124 | return new FlowRuleBatchEntry(operation, rule); |
| 125 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 126 | } |