Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.onos.net.ConnectPoint; |
| 28 | import org.onlab.onos.net.DefaultEdgeLink; |
| 29 | import org.onlab.onos.net.DefaultPath; |
| 30 | import org.onlab.onos.net.Link; |
| 31 | import org.onlab.onos.net.Path; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.intent.Intent; |
| 33 | import org.onlab.onos.net.intent.IntentCompiler; |
| 34 | import org.onlab.onos.net.intent.IntentExtensionService; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.intent.PathIntent; |
| 36 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 37 | import org.onlab.onos.net.provider.ProviderId; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 38 | import org.onlab.onos.net.resource.LinkResourceRequest; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.topology.LinkWeight; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.topology.Topology; |
| 41 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.topology.TopologyService; |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 43 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 44 | import static java.util.Arrays.asList; |
| 45 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 46 | /** |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 47 | * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}. |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | public class PointToPointIntentCompiler |
| 51 | implements IntentCompiler<PointToPointIntent> { |
| 52 | |
| 53 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected IntentExtensionService intentManager; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 58 | protected TopologyService topologyService; |
| 59 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 60 | @Activate |
| 61 | public void activate() { |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 62 | intentManager.registerCompiler(PointToPointIntent.class, this); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public List<Intent> compile(PointToPointIntent intent) { |
| 72 | Path path = getPath(intent.ingressPoint(), intent.egressPoint()); |
| 73 | |
| 74 | List<Link> links = new ArrayList<>(); |
| 75 | links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true)); |
| 76 | links.addAll(path.links()); |
| 77 | links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false)); |
| 78 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 79 | return asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2, |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 80 | path.annotations()), intent)); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates a path intent from the specified path and original |
| 85 | * connectivity intent. |
| 86 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 87 | * @param path path to create an intent for |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 88 | * @param intent original intent |
| 89 | */ |
| 90 | private Intent createPathIntent(Path path, |
| 91 | PointToPointIntent intent) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 92 | return new PathIntent(intent.appId(), |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 93 | intent.selector(), intent.treatment(), path, |
| 94 | new LinkResourceRequest[0]); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Computes a path between two ConnectPoints. |
| 99 | * |
| 100 | * @param one start of the path |
| 101 | * @param two end of the path |
| 102 | * @return Path between the two |
| 103 | * @throws PathNotFoundException if a path cannot be found |
| 104 | */ |
| 105 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 106 | Topology topology = topologyService.currentTopology(); |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 107 | LinkWeight weight = new LinkWeight() { |
| 108 | @Override |
| 109 | public double weight(TopologyEdge edge) { |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 110 | return edge.link().type() == Link.Type.OPTICAL ? -1 : +1; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 114 | Set<Path> paths = topologyService.getPaths(topology, one.deviceId(), |
| 115 | two.deviceId(), weight); |
| 116 | if (paths.isEmpty()) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 117 | throw new PathNotFoundException("No packet path from " + one + " to " + two); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 118 | } |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 119 | |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 120 | // TODO: let's be more intelligent about this eventually |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 121 | return paths.iterator().next(); |
Ray Milkey | a058c73 | 2014-10-08 13:52:34 -0700 | [diff] [blame] | 122 | } |
| 123 | } |