Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.List; |
| 6 | import java.util.Set; |
| 7 | |
| 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.net.ConnectPoint; |
| 14 | import org.onlab.onos.net.Link; |
| 15 | import org.onlab.onos.net.Path; |
| 16 | import org.onlab.onos.net.intent.IdGenerator; |
| 17 | import org.onlab.onos.net.intent.Intent; |
| 18 | import org.onlab.onos.net.intent.IntentCompiler; |
| 19 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 20 | import org.onlab.onos.net.intent.IntentId; |
| 21 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 22 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 23 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 24 | import org.onlab.onos.net.topology.PathService; |
| 25 | |
| 26 | /** |
| 27 | * An intent compiler for |
| 28 | * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}. |
| 29 | */ |
| 30 | @Component(immediate = true) |
| 31 | public class MultiPointToSinglePointIntentCompiler |
| 32 | implements IntentCompiler<MultiPointToSinglePointIntent> { |
| 33 | |
| 34 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 35 | protected IntentExtensionService intentManager; |
| 36 | |
| 37 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 38 | protected PathService pathService; |
| 39 | |
| 40 | private IdGenerator<IntentId> intentIdGenerator; |
| 41 | |
| 42 | @Activate |
| 43 | public void activate() { |
| 44 | IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator(); |
| 45 | intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator); |
| 46 | intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this); |
| 47 | } |
| 48 | |
| 49 | @Deactivate |
| 50 | public void deactivate() { |
| 51 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public List<Intent> compile(MultiPointToSinglePointIntent intent) { |
| 56 | Set<Link> links = new HashSet<>(); |
| 57 | |
| 58 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 59 | Path path = getPath(ingressPoint, intent.egressPoint()); |
| 60 | links.addAll(path.links()); |
| 61 | } |
| 62 | |
| 63 | Intent result = new LinkCollectionIntent(intentIdGenerator.getNewId(), |
| 64 | intent.selector(), intent.treatment(), |
| 65 | links); |
| 66 | return Arrays.asList(result); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Computes a path between two ConnectPoints. |
| 71 | * |
| 72 | * @param one start of the path |
| 73 | * @param two end of the path |
| 74 | * @return Path between the two |
| 75 | * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found |
| 76 | */ |
| 77 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
| 78 | Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 79 | if (paths.isEmpty()) { |
| 80 | throw new PathNotFoundException("No path from " + one + " to " + two); |
| 81 | } |
| 82 | // TODO: let's be more intelligent about this eventually |
| 83 | return paths.iterator().next(); |
| 84 | } |
| 85 | } |