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