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