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