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 | */ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.onos.net.ConnectPoint; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.Link; |
| 26 | import org.onlab.onos.net.Path; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.intent.Intent; |
| 28 | import org.onlab.onos.net.intent.IntentCompiler; |
| 29 | import org.onlab.onos.net.intent.IntentExtensionService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 31 | import org.onlab.onos.net.intent.OpticalPathIntent; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.topology.LinkWeight; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.topology.Topology; |
| 34 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.topology.TopologyService; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 36 | |
| 37 | import java.util.List; |
| 38 | import java.util.Set; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 41 | * An intent compiler for {@link org.onlab.onos.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 45 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected IntentExtensionService intentManager; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 50 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 51 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 52 | @Activate |
| 53 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 54 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 55 | } |
| 56 | |
| 57 | @Deactivate |
| 58 | public void deactivate() { |
| 59 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public List<Intent> compile(OpticalConnectivityIntent intent) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 64 | // TODO: compute multiple paths using the K-shortest path algorithm |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 65 | Path path = calculateOpticalPath(intent.getSrcConnectPoint(), intent.getDst()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 66 | Intent newIntent = new OpticalPathIntent(intent.appId(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 67 | intent.getSrcConnectPoint(), |
| 68 | intent.getDst(), |
| 69 | path); |
| 70 | return ImmutableList.of(newIntent); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 71 | } |
| 72 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 73 | private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 74 | // TODO: support user policies |
| 75 | Topology topology = topologyService.currentTopology(); |
| 76 | LinkWeight weight = new LinkWeight() { |
| 77 | @Override |
| 78 | public double weight(TopologyEdge edge) { |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 79 | return edge.link().type() == Link.Type.OPTICAL ? +1 : -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 80 | } |
| 81 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 83 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
| 84 | end.deviceId(), weight); |
| 85 | if (paths.isEmpty()) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 86 | throw new PathNotFoundException("No fiber path from " + start + " to " + end); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 89 | // TODO: let's be more intelligent about this eventually |
| 90 | return paths.iterator().next(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } |