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