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