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