Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 19 | package org.onlab.onos.optical.provisioner; |
| 20 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 23 | import java.util.Iterator; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Map.Entry; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | |
| 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame^] | 33 | import org.onlab.onos.core.ApplicationId; |
| 34 | import org.onlab.onos.core.CoreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.ConnectPoint; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.Link; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.Path; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.device.DeviceService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.Intent; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.intent.IntentEvent; |
| 41 | import org.onlab.onos.net.intent.IntentExtensionService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.intent.IntentListener; |
| 43 | import org.onlab.onos.net.intent.IntentService; |
| 44 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 45 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 46 | import org.onlab.onos.net.link.LinkService; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.resource.LinkResourceService; |
| 48 | import org.onlab.onos.net.topology.LinkWeight; |
| 49 | import org.onlab.onos.net.topology.Topology; |
| 50 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 51 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.topology.TopologyService; |
| 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
| 56 | /** |
| 57 | * OpticalPathProvisioner listens event notifications from the Intent F/W. |
| 58 | * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W |
| 59 | * for adding/releasing capacity at the packet layer. |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | @Component(immediate = true) |
| 64 | public class OpticalPathProvisioner { |
| 65 | |
| 66 | protected static final Logger log = LoggerFactory |
| 67 | .getLogger(OpticalPathProvisioner.class); |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | private IntentService intentService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | private IntentExtensionService intentExtensionService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected LinkService linkService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected DeviceService deviceService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected TopologyService topologyService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected CoreService coreService; |
| 86 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected LinkResourceService resourceService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 89 | |
| 90 | private ApplicationId appId; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 91 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 92 | //protected <IntentId> intentIdGenerator; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 93 | |
| 94 | private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner(); |
| 95 | |
| 96 | @Activate |
| 97 | protected void activate() { |
| 98 | intentService.addListener(pathProvisioner); |
| 99 | appId = coreService.registerApplication("org.onlab.onos.optical"); |
| 100 | log.info("Starting optical path provisoning..."); |
| 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | protected void deactivate() { |
| 105 | intentService.removeListener(pathProvisioner); |
| 106 | } |
| 107 | |
| 108 | public class InternalOpticalPathProvisioner implements IntentListener { |
| 109 | @Override |
| 110 | public void event(IntentEvent event) { |
| 111 | switch (event.type()) { |
| 112 | case SUBMITTED: |
| 113 | break; |
| 114 | case INSTALLED: |
| 115 | break; |
| 116 | case FAILED: |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 117 | log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 118 | setuplightpath(event.subject()); |
| 119 | break; |
| 120 | case WITHDRAWN: |
| 121 | log.info("intent {} withdrawn.", event.subject()); |
| 122 | teardownLightpath(event.subject()); |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private void setuplightpath(Intent intent) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 130 | // TODO support more packet intent types |
| 131 | if (!intent.getClass().equals(PointToPointIntent.class)) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | PointToPointIntent pktIntent = (PointToPointIntent) intent; |
| 136 | if (pktIntent.ingressPoint() == null || pktIntent.egressPoint() == null) { |
| 137 | return; |
| 138 | } |
| 139 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 140 | Topology topology = topologyService.currentTopology(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 141 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 142 | LinkWeight weight = new LinkWeight() { |
| 143 | @Override |
| 144 | public double weight(TopologyEdge edge) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 145 | if (isOpticalLink(edge.link())) { |
| 146 | return 1000.0; // optical links |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 147 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 148 | return 1.0; // packet links |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 152 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 153 | Set<Path> paths = topologyService.getPaths(topology, |
| 154 | pktIntent.ingressPoint().deviceId(), |
| 155 | pktIntent.egressPoint().deviceId(), |
| 156 | weight); |
| 157 | |
| 158 | if (paths.isEmpty()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 162 | ConnectPoint srcWdmPoint = null; |
| 163 | ConnectPoint dstWdmPoint = null; |
| 164 | Iterator<Path> itrPath = paths.iterator(); |
| 165 | Path firstPath = itrPath.next(); |
| 166 | log.info(firstPath.toString()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 167 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 168 | ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>(); |
| 169 | |
| 170 | Iterator<Link> itrLink = firstPath.links().iterator(); |
| 171 | while (itrLink.hasNext()) { |
| 172 | Link link1 = itrLink.next(); |
| 173 | if (!isOpticalLink(link1)) { |
| 174 | continue; |
| 175 | } else { |
| 176 | srcWdmPoint = link1.dst(); |
| 177 | dstWdmPoint = srcWdmPoint; |
| 178 | } |
| 179 | |
| 180 | while (true) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 181 | if (itrLink.hasNext()) { |
| 182 | Link link2 = itrLink.next(); |
| 183 | dstWdmPoint = link2.src(); |
| 184 | } else { |
| 185 | break; |
| 186 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 187 | if (itrLink.hasNext()) { |
| 188 | Link link3 = itrLink.next(); |
| 189 | if (!isOpticalLink(link3)) { |
| 190 | break; |
| 191 | } |
| 192 | } else { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Map<ConnectPoint, ConnectPoint> pair = |
| 198 | new HashMap<ConnectPoint, ConnectPoint>(); |
| 199 | pair.put(srcWdmPoint, dstWdmPoint); |
| 200 | |
| 201 | connectionList.add(pair); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 202 | } |
| 203 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 204 | for (Map<ConnectPoint, ConnectPoint> map : connectionList) { |
| 205 | for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 206 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 207 | ConnectPoint src = entry.getKey(); |
| 208 | ConnectPoint dst = entry.getValue(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 209 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 210 | Intent opticalIntent = new OpticalConnectivityIntent(appId, |
| 211 | srcWdmPoint, |
| 212 | dstWdmPoint); |
| 213 | |
| 214 | intentService.submit(opticalIntent); |
| 215 | |
| 216 | log.info(opticalIntent.toString()); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | private boolean isOpticalLink(Link link) { |
| 223 | boolean isOptical = false; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 224 | Link.Type lt = link.type(); |
| 225 | if (lt == Link.Type.OPTICAL) { |
| 226 | isOptical = true; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 227 | } |
| 228 | return isOptical; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private void teardownLightpath(Intent intent) { |
| 232 | // TODO: tear down the idle lightpath if the utilization is close to zero. |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | } |