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