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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.optical; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
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; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.Host; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.host.HostService; |
| 30 | import org.onosproject.net.intent.HostToHostIntent; |
| 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentEvent; |
| 33 | import org.onosproject.net.intent.IntentListener; |
| 34 | import org.onosproject.net.intent.IntentOperations; |
| 35 | import org.onosproject.net.intent.IntentService; |
| 36 | import org.onosproject.net.intent.IntentState; |
| 37 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 38 | import org.onosproject.net.intent.PointToPointIntent; |
| 39 | import org.onosproject.net.topology.LinkWeight; |
| 40 | import org.onosproject.net.topology.PathService; |
| 41 | import org.onosproject.net.topology.TopologyEdge; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 45 | import java.util.Iterator; |
| 46 | import java.util.List; |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 47 | import java.util.Map; |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 48 | import java.util.Set; |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 49 | import java.util.concurrent.ConcurrentHashMap; |
| 50 | |
| 51 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 52 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 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) |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 70 | protected PathService pathService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected CoreService coreService; |
| 74 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 76 | protected HostService hostService; |
| 77 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 78 | private ApplicationId appId; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 79 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 80 | // TODO use a shared map for distributed operation |
| 81 | protected final Map<ConnectPoint, OpticalConnectivityIntent> inStatusTportMap = |
| 82 | new ConcurrentHashMap<>(); |
| 83 | protected final Map<ConnectPoint, OpticalConnectivityIntent> outStatusTportMap = |
| 84 | new ConcurrentHashMap<>(); |
| 85 | |
| 86 | protected final Map<ConnectPoint, Map<ConnectPoint, Intent>> intentMap = |
| 87 | new ConcurrentHashMap<>(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 88 | |
| 89 | private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner(); |
| 90 | |
| 91 | @Activate |
| 92 | protected void activate() { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 93 | // TODO elect a leader and have one instance do the provisioning |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 94 | intentService.addListener(pathProvisioner); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 95 | appId = coreService.registerApplication("org.onosproject.optical"); |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 96 | initTport(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 97 | log.info("Starting optical path provisoning..."); |
| 98 | } |
| 99 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 100 | protected void initTport() { |
| 101 | inStatusTportMap.clear(); |
| 102 | outStatusTportMap.clear(); |
| 103 | for (Intent intent : intentService.getIntents()) { |
| 104 | if (intentService.getIntentState(intent.id()) == INSTALLED) { |
| 105 | if (intent instanceof OpticalConnectivityIntent) { |
| 106 | inStatusTportMap.put(((OpticalConnectivityIntent) intent).getSrc(), |
| 107 | (OpticalConnectivityIntent) intent); |
| 108 | outStatusTportMap.put(((OpticalConnectivityIntent) intent).getDst(), |
| 109 | (OpticalConnectivityIntent) intent); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 115 | protected void deactivate() { |
| 116 | intentService.removeListener(pathProvisioner); |
| 117 | } |
| 118 | |
| 119 | public class InternalOpticalPathProvisioner implements IntentListener { |
| 120 | @Override |
| 121 | public void event(IntentEvent event) { |
| 122 | switch (event.type()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 123 | case INSTALL_REQ: |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 124 | break; |
| 125 | case INSTALLED: |
| 126 | break; |
| 127 | case FAILED: |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 128 | log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject()); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 129 | setupLightpath(event.subject()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 130 | break; |
| 131 | case WITHDRAWN: |
| 132 | log.info("intent {} withdrawn.", event.subject()); |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 133 | //FIXME |
| 134 | //teardownLightpath(event.subject()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 135 | break; |
| 136 | default: |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 141 | private void reserveTport(Intent intent) { |
| 142 | // TODO move to resourceManager |
| 143 | if (intent instanceof OpticalConnectivityIntent) { |
| 144 | OpticalConnectivityIntent opticalIntent = |
| 145 | (OpticalConnectivityIntent) intent; |
| 146 | if (inStatusTportMap.containsKey(opticalIntent.getSrc()) || |
| 147 | outStatusTportMap.containsKey(opticalIntent.getDst())) { |
| 148 | //TODO throw an exception, perhaps |
| 149 | log.warn("Overlapping reservation: {}", opticalIntent); |
| 150 | } |
| 151 | inStatusTportMap.put(opticalIntent.getSrc(), opticalIntent); |
| 152 | outStatusTportMap.put(opticalIntent.getDst(), opticalIntent); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Registers an intent from src to dst. |
| 158 | * @param src source point |
| 159 | * @param dst destination point |
| 160 | * @param intent intent to be registered |
| 161 | * @return true if intent has not been previously added, false otherwise |
| 162 | */ |
| 163 | private boolean addIntent(ConnectPoint src, ConnectPoint dst, Intent intent) { |
| 164 | Map<ConnectPoint, Intent> srcMap = intentMap.get(src); |
| 165 | if (srcMap == null) { |
| 166 | srcMap = new ConcurrentHashMap<>(); |
| 167 | intentMap.put(src, srcMap); |
| 168 | } |
| 169 | if (srcMap.containsKey(dst)) { |
| 170 | return false; |
| 171 | } else { |
| 172 | srcMap.put(dst, intent); |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 177 | private void setupLightpath(Intent intent) { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 178 | // TODO change the coordination approach between packet intents and optical intents |
| 179 | // Low speed LLDP may cause multiple calls which are not expected |
| 180 | |
| 181 | if (!IntentState.FAILED.equals(intentService.getIntentState(intent.id()))) { |
| 182 | return; |
| 183 | } |
| 184 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 185 | List<Intent> intents = Lists.newArrayList(); |
| 186 | if (intent instanceof HostToHostIntent) { |
| 187 | HostToHostIntent hostToHostIntent = (HostToHostIntent) intent; |
| 188 | Host one = hostService.getHost(hostToHostIntent.one()); |
| 189 | Host two = hostService.getHost(hostToHostIntent.two()); |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 190 | if (one == null || two == null) { |
| 191 | return; //FIXME |
| 192 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 193 | // provision both directions |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 194 | intents.addAll(getOpticalPath(one.location(), two.location())); |
| 195 | intents.addAll(getOpticalPath(two.location(), one.location())); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 196 | } else if (intent instanceof PointToPointIntent) { |
| 197 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 198 | intents.addAll(getOpticalPath(p2pIntent.ingressPoint(), p2pIntent.egressPoint())); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 199 | } else { |
| 200 | log.info("Unsupported intent type: {}", intent.getClass()); |
| 201 | } |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 202 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 203 | // Build the intent batch |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 204 | IntentOperations.Builder ops = IntentOperations.builder(appId); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 205 | for (Intent i : intents) { |
| 206 | // TODO: don't allow duplicate intents between the same points for now |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 207 | // we may want to allow this carefully in future to increase capacity |
| 208 | if (i instanceof OpticalConnectivityIntent) { |
| 209 | OpticalConnectivityIntent oi = (OpticalConnectivityIntent) i; |
| 210 | if (addIntent(oi.getSrc(), oi.getDst(), oi)) { |
| 211 | ops.addSubmitOperation(i); |
| 212 | reserveTport(i); |
| 213 | } |
| 214 | } else { |
| 215 | log.warn("Invalid intent type: {} for {}", i.getClass(), i); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | intentService.execute(ops.build()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 221 | private List<Intent> getOpticalPath(ConnectPoint ingress, ConnectPoint egress) { |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 222 | Set<Path> paths = pathService.getPaths(ingress.deviceId(), |
| 223 | egress.deviceId(), |
| 224 | new OpticalLinkWeight()); |
| 225 | |
| 226 | if (paths.isEmpty()) { |
| 227 | return Lists.newArrayList(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 228 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 229 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 230 | List<Intent> connectionList = Lists.newArrayList(); |
| 231 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 232 | Iterator<Path> itrPath = paths.iterator(); |
| 233 | while (itrPath.hasNext()) { |
| 234 | boolean usedTportFound = false; |
| 235 | Path nextPath = itrPath.next(); |
| 236 | log.info(nextPath.links().toString()); // TODO drop log level |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 237 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 238 | Iterator<Link> itrLink = nextPath.links().iterator(); |
| 239 | while (itrLink.hasNext()) { |
| 240 | ConnectPoint srcWdmPoint, dstWdmPoint; |
| 241 | Link link1 = itrLink.next(); |
| 242 | if (!isOpticalLink(link1)) { |
| 243 | continue; |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 244 | } else { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 245 | srcWdmPoint = link1.dst(); |
| 246 | dstWdmPoint = srcWdmPoint; |
| 247 | } |
| 248 | |
| 249 | while (itrLink.hasNext()) { |
| 250 | Link link2 = itrLink.next(); |
| 251 | if (isOpticalLink(link2)) { |
| 252 | dstWdmPoint = link2.src(); |
| 253 | } else { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (inStatusTportMap.get(srcWdmPoint) != null || |
| 259 | outStatusTportMap.get(dstWdmPoint) != null) { |
| 260 | usedTportFound = true; |
| 261 | // log.info("used ConnectPoint {} to {} were found", srcWdmPoint, dstWdmPoint); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 262 | break; |
| 263 | } |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 264 | |
| 265 | Intent opticalIntent = new OpticalConnectivityIntent(appId, |
| 266 | srcWdmPoint, |
| 267 | dstWdmPoint); |
| 268 | log.info("Creating optical intent from {} to {}", srcWdmPoint, dstWdmPoint); |
| 269 | connectionList.add(opticalIntent); |
| 270 | |
| 271 | break; |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 274 | if (!usedTportFound) { |
| 275 | break; |
| 276 | } else { |
| 277 | // reset the connection list |
| 278 | connectionList = Lists.newArrayList(); |
| 279 | } |
| 280 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 281 | } |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 282 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 283 | return connectionList; |
| 284 | } |
| 285 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 286 | private void teardownLightpath(Intent intent) { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 287 | /* FIXME this command doesn't make much sense. we need to define the semantics |
| 288 | // TODO move to resourceManager |
| 289 | if (intent instanceof OpticalConnectivityIntent) { |
| 290 | inStatusTportMap.remove(((OpticalConnectivityIntent) intent).getSrc()); |
| 291 | outStatusTportMap.remove(((OpticalConnectivityIntent) intent).getDst()); |
| 292 | // TODO tear down the idle lightpath if the utilization is zero. |
| 293 | |
| 294 | } |
| 295 | */ //end-FIXME |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 296 | } |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 297 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 298 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 299 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 300 | private static boolean isOpticalLink(Link link) { |
| 301 | boolean isOptical = false; |
| 302 | Link.Type lt = link.type(); |
| 303 | if (lt == Link.Type.OPTICAL) { |
| 304 | isOptical = true; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 305 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 306 | return isOptical; |
| 307 | } |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 308 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 309 | private static class OpticalLinkWeight implements LinkWeight { |
| 310 | @Override |
| 311 | public double weight(TopologyEdge edge) { |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 312 | if (edge.link().state() == Link.State.INACTIVE) { |
| 313 | return -1; // ignore inactive links |
| 314 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 315 | if (isOpticalLink(edge.link())) { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 316 | return 1000; // optical links |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 317 | } else { |
Brian O'Connor | c7bdd8c | 2014-12-08 01:29:53 -0800 | [diff] [blame^] | 318 | return 1; // packet links |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | } |