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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame^] | 16 | package org.onosproject.net.intent.impl.compiler; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.Path; |
| 27 | import org.onosproject.net.intent.Intent; |
| 28 | import org.onosproject.net.intent.IntentCompiler; |
| 29 | import org.onosproject.net.intent.IntentExtensionService; |
| 30 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 31 | import org.onosproject.net.intent.OpticalPathIntent; |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame^] | 32 | import org.onosproject.net.intent.impl.PathNotFoundException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 34 | import org.onosproject.net.topology.LinkWeight; |
| 35 | import org.onosproject.net.topology.Topology; |
| 36 | import org.onosproject.net.topology.TopologyEdge; |
| 37 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 38 | |
| 39 | import java.util.List; |
| 40 | import java.util.Set; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 47 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected IntentExtensionService intentManager; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 52 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 53 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 54 | @Activate |
| 55 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 56 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
| 61 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 62 | } |
| 63 | |
| 64 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 65 | public List<Intent> compile(OpticalConnectivityIntent intent, |
| 66 | List<Intent> installable, |
| 67 | Set<LinkResourceAllocations> resources) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 68 | // TODO: compute multiple paths using the K-shortest path algorithm |
Jonathan Hart | c9d7673 | 2014-11-18 10:52:20 -0800 | [diff] [blame] | 69 | Path path = calculateOpticalPath(intent.getSrc(), intent.getDst()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 70 | Intent newIntent = new OpticalPathIntent(intent.appId(), |
Jonathan Hart | c9d7673 | 2014-11-18 10:52:20 -0800 | [diff] [blame] | 71 | intent.getSrc(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 72 | intent.getDst(), |
| 73 | path); |
| 74 | return ImmutableList.of(newIntent); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 77 | private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 78 | // TODO: support user policies |
| 79 | Topology topology = topologyService.currentTopology(); |
| 80 | LinkWeight weight = new LinkWeight() { |
| 81 | @Override |
| 82 | public double weight(TopologyEdge edge) { |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 83 | if (edge.link().state() == Link.State.INACTIVE) { |
| 84 | return -1; |
| 85 | } |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 86 | return edge.link().type() == Link.Type.OPTICAL ? +1 : -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 87 | } |
| 88 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 89 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 90 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
| 91 | end.deviceId(), weight); |
| 92 | if (paths.isEmpty()) { |
Sho SHIMIZU | 877ec2c | 2015-02-09 12:50:36 -0800 | [diff] [blame] | 93 | throw new PathNotFoundException(start.elementId(), end.elementId()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 96 | // TODO: let's be more intelligent about this eventually |
| 97 | return paths.iterator().next(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } |