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 | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.Host; |
| 9 | import org.onlab.onos.net.HostId; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.Path; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.flow.TrafficSelector; |
| 12 | import org.onlab.onos.net.host.HostService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.intent.HostToHostIntent; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.intent.Intent; |
| 15 | import org.onlab.onos.net.intent.IntentCompiler; |
| 16 | import org.onlab.onos.net.intent.IntentExtensionService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.intent.PathIntent; |
| 18 | import org.onlab.onos.net.topology.PathService; |
| 19 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 20 | import java.util.Arrays; |
| 21 | import java.util.List; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder; |
| 25 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 26 | /** |
| 27 | * A intent compiler for {@link HostToHostIntent}. |
| 28 | */ |
| 29 | @Component(immediate = true) |
| 30 | public class HostToHostIntentCompiler |
| 31 | implements IntentCompiler<HostToHostIntent> { |
| 32 | |
| 33 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 34 | protected IntentExtensionService intentManager; |
| 35 | |
| 36 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 37 | protected PathService pathService; |
| 38 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 39 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 40 | protected HostService hostService; |
| 41 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 42 | @Activate |
| 43 | public void activate() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 44 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 45 | } |
| 46 | |
| 47 | @Deactivate |
| 48 | public void deactivate() { |
| 49 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public List<Intent> compile(HostToHostIntent intent) { |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 54 | Path pathOne = getPath(intent.one(), intent.two()); |
| 55 | Path pathTwo = getPath(intent.two(), intent.one()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 56 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 57 | Host one = hostService.getHost(intent.one()); |
| 58 | Host two = hostService.getHost(intent.two()); |
| 59 | |
| 60 | return Arrays.asList(createPathIntent(pathOne, one, two, intent), |
| 61 | createPathIntent(pathTwo, two, one, intent)); |
| 62 | } |
| 63 | |
| 64 | // Creates a path intent from the specified path and original connectivity intent. |
| 65 | private Intent createPathIntent(Path path, Host src, Host dst, |
| 66 | HostToHostIntent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 67 | TrafficSelector selector = builder(intent.selector()) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 68 | .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 69 | return new PathIntent(intent.appId(), selector, intent.treatment(), |
| 70 | path); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private Path getPath(HostId one, HostId two) { |
| 74 | Set<Path> paths = pathService.getPaths(one, two); |
| 75 | if (paths.isEmpty()) { |
| 76 | throw new PathNotFoundException("No path from host " + one + " to " + two); |
| 77 | } |
| 78 | // TODO: let's be more intelligent about this eventually |
| 79 | return paths.iterator().next(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 80 | } |
| 81 | } |