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; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 20 | import org.onlab.onos.net.topology.LinkWeight; |
| 21 | //import org.onlab.onos.net.topology.LinkWeight; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.topology.PathService; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 23 | import org.onlab.onos.net.topology.Topology; |
| 24 | import org.onlab.onos.net.topology.TopologyEdge; |
| 25 | //import org.onlab.onos.net.topology.Topology; |
| 26 | //import org.onlab.onos.net.topology.TopologyEdge; |
| 27 | import org.onlab.onos.net.topology.TopologyService; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 28 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.Arrays; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 31 | import java.util.Iterator; |
| 32 | //import java.util.Iterator; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Set; |
| 35 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 36 | /** |
| 37 | * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}. |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | public class PointToPointIntentCompiler |
| 41 | implements IntentCompiler<PointToPointIntent> { |
| 42 | |
| 43 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected IntentExtensionService intentManager; |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected PathService pathService; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected HostService hostService; |
| 52 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected TopologyService topologyService; |
| 55 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 56 | @Activate |
| 57 | public void activate() { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 58 | intentManager.registerCompiler(PointToPointIntent.class, this); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | public void deactivate() { |
| 63 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public List<Intent> compile(PointToPointIntent intent) { |
| 68 | Path path = getPath(intent.ingressPoint(), intent.egressPoint()); |
| 69 | |
| 70 | List<Link> links = new ArrayList<>(); |
| 71 | links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true)); |
| 72 | links.addAll(path.links()); |
| 73 | links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false)); |
| 74 | |
| 75 | return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 76 | path.annotations()), |
| 77 | intent)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates a path intent from the specified path and original |
| 82 | * connectivity intent. |
| 83 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 84 | * @param path path to create an intent for |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 85 | * @param intent original intent |
| 86 | */ |
| 87 | private Intent createPathIntent(Path path, |
| 88 | PointToPointIntent intent) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 89 | return new PathIntent(intent.appId(), |
| 90 | intent.selector(), intent.treatment(), path); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Computes a path between two ConnectPoints. |
| 95 | * |
| 96 | * @param one start of the path |
| 97 | * @param two end of the path |
| 98 | * @return Path between the two |
| 99 | * @throws PathNotFoundException if a path cannot be found |
| 100 | */ |
| 101 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 102 | // Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 103 | Topology topology = topologyService.currentTopology(); |
| 104 | LinkWeight weight = new LinkWeight() { |
| 105 | @Override |
| 106 | public double weight(TopologyEdge edge) { |
| 107 | Link.Type lt = edge.link().type(); |
| 108 | if (lt == Link.Type.OPTICAL) { |
| 109 | return 1000.0; |
| 110 | } else { |
| 111 | return 1.0; |
| 112 | } |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | Set<Path> paths = topologyService.getPaths(topology, |
| 117 | one.deviceId(), |
| 118 | two.deviceId(), |
| 119 | weight); |
| 120 | |
| 121 | ArrayList<Path> localPaths = new ArrayList<>(); |
| 122 | Iterator<Path> itr = paths.iterator(); |
| 123 | while (itr.hasNext()) { |
| 124 | Path path = itr.next(); |
| 125 | if (path.cost() >= 1000) { |
| 126 | continue; |
| 127 | } |
| 128 | localPaths.add(path); |
| 129 | } |
| 130 | |
| 131 | if (localPaths.isEmpty()) { |
| 132 | throw new PathNotFoundException("No packet path from " + one + " to " + two); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 133 | } |
| 134 | // TODO: let's be more intelligent about this eventually |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 135 | return localPaths.iterator().next(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 136 | } |
| 137 | } |