Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -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.DefaultEdgeLink; |
| 10 | import org.onlab.onos.net.DefaultPath; |
| 11 | import org.onlab.onos.net.Link; |
| 12 | import org.onlab.onos.net.Path; |
| 13 | import org.onlab.onos.net.host.HostService; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -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; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.intent.PathIntent; |
| 18 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 19 | import org.onlab.onos.net.provider.ProviderId; |
| 20 | import org.onlab.onos.net.topology.PathService; |
| 21 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 22 | import java.util.ArrayList; |
| 23 | import java.util.Arrays; |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| 26 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 27 | /** |
| 28 | * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}. |
| 29 | */ |
| 30 | @Component(immediate = true) |
| 31 | public class PointToPointIntentCompiler |
| 32 | implements IntentCompiler<PointToPointIntent> { |
| 33 | |
| 34 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 35 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 36 | protected IntentExtensionService intentManager; |
| 37 | |
| 38 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 39 | protected PathService pathService; |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected HostService hostService; |
| 43 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 44 | @Activate |
| 45 | public void activate() { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 46 | intentManager.registerCompiler(PointToPointIntent.class, this); |
| 47 | } |
| 48 | |
| 49 | @Deactivate |
| 50 | public void deactivate() { |
| 51 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public List<Intent> compile(PointToPointIntent intent) { |
| 56 | Path path = getPath(intent.ingressPoint(), intent.egressPoint()); |
| 57 | |
| 58 | List<Link> links = new ArrayList<>(); |
| 59 | links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true)); |
| 60 | links.addAll(path.links()); |
| 61 | links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false)); |
| 62 | |
| 63 | return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 64 | path.annotations()), |
| 65 | intent)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a path intent from the specified path and original |
| 70 | * connectivity intent. |
| 71 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 72 | * @param path path to create an intent for |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 73 | * @param intent original intent |
| 74 | */ |
| 75 | private Intent createPathIntent(Path path, |
| 76 | PointToPointIntent intent) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 77 | return new PathIntent(intent.appId(), |
| 78 | intent.selector(), intent.treatment(), path); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Computes a path between two ConnectPoints. |
| 83 | * |
| 84 | * @param one start of the path |
| 85 | * @param two end of the path |
| 86 | * @return Path between the two |
| 87 | * @throws PathNotFoundException if a path cannot be found |
| 88 | */ |
| 89 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
| 90 | Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 91 | if (paths.isEmpty()) { |
| 92 | throw new PathNotFoundException("No path from " + one + " to " + two); |
| 93 | } |
| 94 | // TODO: let's be more intelligent about this eventually |
| 95 | return paths.iterator().next(); |
| 96 | } |
| 97 | } |