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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.ArrayList; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 21 | import java.util.Iterator; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 30 | import org.onlab.onos.core.CoreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.ConnectPoint; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.Link; |
| 33 | import org.onlab.onos.net.Path; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.Intent; |
| 35 | import org.onlab.onos.net.intent.IntentCompiler; |
| 36 | import org.onlab.onos.net.intent.IntentExtensionService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 38 | import org.onlab.onos.net.intent.OpticalPathIntent; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.provider.ProviderId; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.resource.LinkResourceService; |
| 41 | import org.onlab.onos.net.topology.LinkWeight; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.topology.PathService; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.topology.Topology; |
| 44 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.topology.TopologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | /** |
| 49 | * Optical compiler for OpticalConnectivityIntent. |
| 50 | * It firstly computes K-shortest paths in the optical-layer, then choose the optimal one to assign a wavelength. |
| 51 | * Finally, it generates one or more opticalPathintent(s) with opticalMatchs and opticalActions. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected IntentExtensionService intentManager; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected PathService pathService; |
| 64 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 67 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected LinkResourceService resourceService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected CoreService coreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 73 | |
| 74 | @Activate |
| 75 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 76 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
| 81 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public List<Intent> compile(OpticalConnectivityIntent intent) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 86 | // TODO: compute multiple paths using the K-shortest path algorithm |
| 87 | List<Intent> retList = new ArrayList<>(); |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 88 | log.info("The system is comipling the OpticalConnectivityIntent:" + intent.toString()); |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 89 | Path path = calculateOpticalPath(intent.getSrcConnectPoint(), intent.getDst()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 90 | if (path == null) { |
| 91 | return retList; |
| 92 | } else { |
| 93 | log.info("the computed lightpath is : {}.", path.toString()); |
| 94 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 95 | |
| 96 | List<Link> links = new ArrayList<>(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 97 | // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 98 | links.addAll(path.links()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 99 | //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 100 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 101 | // create a new opticalPathIntent |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 102 | Intent newIntent = new OpticalPathIntent(intent.appId(), |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 103 | intent.getSrcConnectPoint(), |
| 104 | intent.getDst(), |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 105 | path); |
| 106 | |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 107 | log.info("a new OpticalPathIntent was created: " + newIntent.toString()); |
| 108 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 109 | retList.add(newIntent); |
| 110 | |
| 111 | return retList; |
| 112 | } |
| 113 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 114 | private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 115 | // TODO: support user policies |
| 116 | Topology topology = topologyService.currentTopology(); |
| 117 | LinkWeight weight = new LinkWeight() { |
| 118 | @Override |
| 119 | public double weight(TopologyEdge edge) { |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 120 | Link.Type lt = edge.link().type(); |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 121 | if (lt == Link.Type.OPTICAL) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 122 | return 1.0; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 123 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 124 | return 1000.0; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 128 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 129 | Set<Path> paths = topologyService.getPaths(topology, |
| 130 | start.deviceId(), |
| 131 | end.deviceId(), |
| 132 | weight); |
| 133 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 134 | ArrayList<Path> localPaths = new ArrayList<>(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 135 | Iterator<Path> itr = paths.iterator(); |
| 136 | while (itr.hasNext()) { |
| 137 | Path path = itr.next(); |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 138 | if (path.cost() >= 1000) { |
| 139 | continue; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 140 | } |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 141 | localPaths.add(path); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 144 | if (localPaths.isEmpty()) { |
| 145 | throw new PathNotFoundException("No fiber path from " + start + " to " + end); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 146 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 147 | return localPaths.iterator().next(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 148 | } |
| 149 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } |