Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame^] | 3 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 4 | |
| 5 | import java.util.Iterator; |
| 6 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 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.net.ConnectPoint; |
| 14 | import org.onlab.onos.net.Link; |
| 15 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 16 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.FlowRule; |
| 18 | import org.onlab.onos.net.flow.FlowRuleService; |
| 19 | import org.onlab.onos.net.flow.TrafficSelector; |
| 20 | import org.onlab.onos.net.flow.TrafficTreatment; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 22 | import org.onlab.onos.net.intent.IntentInstaller; |
| 23 | import org.onlab.onos.net.intent.PathIntent; |
| 24 | |
| 25 | /** |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 26 | * Installer for {@link PathIntent path connectivity intents}. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 27 | */ |
| 28 | @Component(immediate = true) |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 29 | public class PathIntentInstaller implements IntentInstaller<PathIntent> { |
| 30 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 31 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 32 | protected IntentExtensionService intentManager; |
| 33 | |
| 34 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 35 | protected FlowRuleService flowRuleService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 36 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 37 | private final ApplicationId appId = ApplicationId.getAppId(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 38 | |
| 39 | @Activate |
| 40 | public void activate() { |
| 41 | intentManager.registerInstaller(PathIntent.class, this); |
| 42 | } |
| 43 | |
| 44 | @Deactivate |
| 45 | public void deactivate() { |
| 46 | intentManager.unregisterInstaller(PathIntent.class); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void install(PathIntent intent) { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 51 | TrafficSelector.Builder builder = |
| 52 | DefaultTrafficSelector.builder(intent.getTrafficSelector()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | Iterator<Link> links = intent.getPath().links().iterator(); |
| 54 | ConnectPoint prev = links.next().dst(); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 55 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 56 | while (links.hasNext()) { |
| 57 | builder.matchInport(prev.port()); |
| 58 | Link link = links.next(); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 59 | TrafficTreatment treatment = builder() |
| 60 | .setOutput(link.src().port()).build(); |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 61 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame^] | 62 | builder.build(), treatment, |
| 63 | 123, appId, 600); |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 64 | flowRuleService.applyFlowRules(rule); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 65 | prev = link.dst(); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void uninstall(PathIntent intent) { |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame^] | 72 | TrafficSelector.Builder builder = |
| 73 | DefaultTrafficSelector.builder(intent.getTrafficSelector()); |
| 74 | Iterator<Link> links = intent.getPath().links().iterator(); |
| 75 | ConnectPoint prev = links.next().dst(); |
| 76 | |
| 77 | while (links.hasNext()) { |
| 78 | builder.matchInport(prev.port()); |
| 79 | Link link = links.next(); |
| 80 | TrafficTreatment treatment = builder() |
| 81 | .setOutput(link.src().port()).build(); |
| 82 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
| 83 | builder.build(), treatment, |
| 84 | 123, appId, 600); |
| 85 | flowRuleService.removeFlowRules(rule); |
| 86 | prev = link.dst(); |
| 87 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 88 | } |
| 89 | } |