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; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 33 | import org.onlab.onos.net.Host; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.Link; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.Path; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.device.DeviceService; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 37 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 38 | import org.onlab.onos.net.flow.TrafficSelector; |
| 39 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 40 | import org.onlab.onos.net.host.HostService; |
| 41 | import org.onlab.onos.net.intent.HostToHostIntent; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.intent.Intent; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.intent.IntentEvent; |
| 44 | import org.onlab.onos.net.intent.IntentExtensionService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.intent.IntentListener; |
| 46 | import org.onlab.onos.net.intent.IntentService; |
| 47 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 48 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 49 | import org.onlab.onos.net.link.LinkService; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 50 | import org.onlab.onos.net.resource.LinkResourceService; |
| 51 | import org.onlab.onos.net.topology.LinkWeight; |
| 52 | import org.onlab.onos.net.topology.Topology; |
| 53 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 54 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 55 | import org.onlab.onos.net.topology.TopologyService; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 56 | import org.onlab.packet.Ethernet; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 60 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 61 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 62 | /** |
| 63 | * OpticalPathProvisioner listens event notifications from the Intent F/W. |
| 64 | * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W |
| 65 | * for adding/releasing capacity at the packet layer. |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | @Component(immediate = true) |
| 70 | public class OpticalPathProvisioner { |
| 71 | |
| 72 | protected static final Logger log = LoggerFactory |
| 73 | .getLogger(OpticalPathProvisioner.class); |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | private IntentService intentService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | private IntentExtensionService intentExtensionService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected LinkService linkService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected DeviceService deviceService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected TopologyService topologyService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected CoreService coreService; |
| 92 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected LinkResourceService resourceService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 95 | |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected HostService hostService; |
| 98 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 99 | private ApplicationId appId; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 100 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 101 | //protected <IntentId> intentIdGenerator; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 102 | |
| 103 | private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner(); |
| 104 | |
| 105 | @Activate |
| 106 | protected void activate() { |
| 107 | intentService.addListener(pathProvisioner); |
| 108 | appId = coreService.registerApplication("org.onlab.onos.optical"); |
| 109 | log.info("Starting optical path provisoning..."); |
| 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | protected void deactivate() { |
| 114 | intentService.removeListener(pathProvisioner); |
| 115 | } |
| 116 | |
| 117 | public class InternalOpticalPathProvisioner implements IntentListener { |
| 118 | @Override |
| 119 | public void event(IntentEvent event) { |
| 120 | switch (event.type()) { |
| 121 | case SUBMITTED: |
| 122 | break; |
| 123 | case INSTALLED: |
| 124 | break; |
| 125 | case FAILED: |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 126 | log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 127 | setuplightpath(event.subject()); |
| 128 | break; |
| 129 | case WITHDRAWN: |
| 130 | log.info("intent {} withdrawn.", event.subject()); |
| 131 | teardownLightpath(event.subject()); |
| 132 | break; |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private void setuplightpath(Intent intent) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 139 | // TODO support more packet intent types |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 140 | |
| 141 | if (intent instanceof HostToHostIntent) { |
| 142 | HostToHostIntent hostToHostIntent = (HostToHostIntent) intent; |
| 143 | Host one = hostService.getHost(hostToHostIntent.one()); |
| 144 | Host two = hostService.getHost(hostToHostIntent.two()); |
| 145 | |
| 146 | TrafficSelector selector = buildTrafficSelector(); |
| 147 | TrafficTreatment treatment = builder().build(); |
| 148 | |
| 149 | PointToPointIntent intentOneToTwo = |
| 150 | new PointToPointIntent(appId, selector, treatment, |
| 151 | one.location(), two.location()); |
| 152 | intentService.submit(intentOneToTwo); |
| 153 | log.info("Submitting P2P intent {} ", intentOneToTwo); |
| 154 | |
| 155 | PointToPointIntent intentTwoToOne = |
| 156 | new PointToPointIntent(appId, selector, treatment, |
| 157 | two.location(), one.location()); |
| 158 | intentService.submit(intentTwoToOne); |
| 159 | log.info("Submitting P2P intent for {} ", intentTwoToOne); |
| 160 | return; |
| 161 | } else if (!intent.getClass().equals(PointToPointIntent.class)) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
| 165 | PointToPointIntent pktIntent = (PointToPointIntent) intent; |
| 166 | if (pktIntent.ingressPoint() == null || pktIntent.egressPoint() == null) { |
| 167 | return; |
| 168 | } |
| 169 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 170 | Topology topology = topologyService.currentTopology(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 171 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 172 | LinkWeight weight = new LinkWeight() { |
| 173 | @Override |
| 174 | public double weight(TopologyEdge edge) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 175 | if (isOpticalLink(edge.link())) { |
| 176 | return 1000.0; // optical links |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 177 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 178 | return 1.0; // packet links |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 182 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 183 | Set<Path> paths = topologyService.getPaths(topology, |
| 184 | pktIntent.ingressPoint().deviceId(), |
| 185 | pktIntent.egressPoint().deviceId(), |
| 186 | weight); |
| 187 | |
| 188 | if (paths.isEmpty()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 192 | ConnectPoint srcWdmPoint = null; |
| 193 | ConnectPoint dstWdmPoint = null; |
| 194 | Iterator<Path> itrPath = paths.iterator(); |
| 195 | Path firstPath = itrPath.next(); |
weibit | 51234a8 | 2014-11-13 17:34:50 -0800 | [diff] [blame] | 196 | log.info(firstPath.links().toString()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 197 | |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 198 | ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = |
| 199 | new ArrayList<>(); |
| 200 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 201 | |
| 202 | Iterator<Link> itrLink = firstPath.links().iterator(); |
| 203 | while (itrLink.hasNext()) { |
| 204 | Link link1 = itrLink.next(); |
| 205 | if (!isOpticalLink(link1)) { |
| 206 | continue; |
| 207 | } else { |
| 208 | srcWdmPoint = link1.dst(); |
| 209 | dstWdmPoint = srcWdmPoint; |
| 210 | } |
| 211 | |
| 212 | while (true) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 213 | if (itrLink.hasNext()) { |
| 214 | Link link2 = itrLink.next(); |
| 215 | dstWdmPoint = link2.src(); |
| 216 | } else { |
| 217 | break; |
| 218 | } |
weibit | 51234a8 | 2014-11-13 17:34:50 -0800 | [diff] [blame] | 219 | |
| 220 | /* |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 221 | if (itrLink.hasNext()) { |
| 222 | Link link3 = itrLink.next(); |
weibit | 51234a8 | 2014-11-13 17:34:50 -0800 | [diff] [blame] | 223 | if (isOpticalLink(link3)) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } else { |
| 227 | break; |
weibit | 51234a8 | 2014-11-13 17:34:50 -0800 | [diff] [blame] | 228 | }*/ |
| 229 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | Map<ConnectPoint, ConnectPoint> pair = |
| 233 | new HashMap<ConnectPoint, ConnectPoint>(); |
| 234 | pair.put(srcWdmPoint, dstWdmPoint); |
| 235 | |
| 236 | connectionList.add(pair); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 237 | } |
| 238 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 239 | for (Map<ConnectPoint, ConnectPoint> map : connectionList) { |
| 240 | for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 241 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 242 | ConnectPoint src = entry.getKey(); |
| 243 | ConnectPoint dst = entry.getValue(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 244 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 245 | Intent opticalIntent = new OpticalConnectivityIntent(appId, |
| 246 | srcWdmPoint, |
| 247 | dstWdmPoint); |
| 248 | |
| 249 | intentService.submit(opticalIntent); |
| 250 | |
weibit | 51234a8 | 2014-11-13 17:34:50 -0800 | [diff] [blame] | 251 | log.info(srcWdmPoint.toString()); |
| 252 | log.info(dstWdmPoint.toString()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | } |
| 257 | |
| 258 | private boolean isOpticalLink(Link link) { |
| 259 | boolean isOptical = false; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 260 | Link.Type lt = link.type(); |
| 261 | if (lt == Link.Type.OPTICAL) { |
| 262 | isOptical = true; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 263 | } |
| 264 | return isOptical; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | private void teardownLightpath(Intent intent) { |
| 268 | // TODO: tear down the idle lightpath if the utilization is close to zero. |
| 269 | } |
| 270 | |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame^] | 271 | private TrafficSelector buildTrafficSelector() { |
| 272 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 273 | Short ethType = Ethernet.TYPE_IPV4; |
| 274 | |
| 275 | selectorBuilder.matchEthType(ethType); |
| 276 | |
| 277 | return selectorBuilder.build(); |
| 278 | } |
| 279 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | } |