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; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.Path; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.intent.HostToHostIntent; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentEvent; |
| 34 | import org.onosproject.net.intent.IntentListener; |
| 35 | import org.onosproject.net.intent.IntentOperations; |
| 36 | import org.onosproject.net.intent.IntentService; |
| 37 | import org.onosproject.net.intent.IntentState; |
| 38 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 39 | import org.onosproject.net.intent.PointToPointIntent; |
| 40 | import org.onosproject.net.topology.LinkWeight; |
| 41 | import org.onosproject.net.topology.PathService; |
| 42 | import org.onosproject.net.topology.TopologyEdge; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 46 | import java.util.Iterator; |
| 47 | import java.util.List; |
| 48 | import java.util.Set; |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 49 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 50 | /** |
| 51 | * OpticalPathProvisioner listens event notifications from the Intent F/W. |
| 52 | * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W |
| 53 | * for adding/releasing capacity at the packet layer. |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | @Component(immediate = true) |
| 58 | public class OpticalPathProvisioner { |
| 59 | |
| 60 | protected static final Logger log = LoggerFactory |
| 61 | .getLogger(OpticalPathProvisioner.class); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | private IntentService intentService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 67 | protected PathService pathService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected CoreService coreService; |
| 71 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 73 | protected HostService hostService; |
| 74 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 75 | private ApplicationId appId; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 76 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 77 | //protected <IntentId> intentIdGenerator; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 78 | |
| 79 | private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner(); |
| 80 | |
| 81 | @Activate |
| 82 | protected void activate() { |
| 83 | intentService.addListener(pathProvisioner); |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 84 | appId = coreService.registerApplication("org.onosproject.optical"); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 85 | log.info("Starting optical path provisoning..."); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | protected void deactivate() { |
| 90 | intentService.removeListener(pathProvisioner); |
| 91 | } |
| 92 | |
| 93 | public class InternalOpticalPathProvisioner implements IntentListener { |
| 94 | @Override |
| 95 | public void event(IntentEvent event) { |
| 96 | switch (event.type()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 97 | case INSTALL_REQ: |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 98 | break; |
| 99 | case INSTALLED: |
| 100 | break; |
| 101 | case FAILED: |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 102 | 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] | 103 | setupLightpath(event.subject()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 104 | break; |
| 105 | case WITHDRAWN: |
| 106 | log.info("intent {} withdrawn.", event.subject()); |
| 107 | teardownLightpath(event.subject()); |
| 108 | break; |
| 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 114 | private void setupLightpath(Intent intent) { |
| 115 | // TODO support more packet intent types |
| 116 | List<Intent> intents = Lists.newArrayList(); |
| 117 | if (intent instanceof HostToHostIntent) { |
| 118 | HostToHostIntent hostToHostIntent = (HostToHostIntent) intent; |
| 119 | Host one = hostService.getHost(hostToHostIntent.one()); |
| 120 | Host two = hostService.getHost(hostToHostIntent.two()); |
| 121 | // provision both directions |
| 122 | intents.addAll(getOpticalPaths(one.location(), two.location())); |
| 123 | intents.addAll(getOpticalPaths(two.location(), one.location())); |
| 124 | } else if (intent instanceof PointToPointIntent) { |
| 125 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 126 | intents.addAll(getOpticalPaths(p2pIntent.ingressPoint(), p2pIntent.egressPoint())); |
| 127 | } else { |
| 128 | log.info("Unsupported intent type: {}", intent.getClass()); |
| 129 | } |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 130 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 131 | // Build the intent batch |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 132 | IntentOperations.Builder ops = IntentOperations.builder(appId); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 133 | for (Intent i : intents) { |
| 134 | // TODO: don't allow duplicate intents between the same points for now |
| 135 | // we may want to allow this carefully in future to increase capacity |
| 136 | Intent existing = intentService.getIntent(i.id()); |
| 137 | if (existing == null || |
| 138 | !IntentState.WITHDRAWN.equals(intentService.getIntentState(i.id()))) { |
| 139 | ops.addSubmitOperation(i); |
| 140 | } |
| 141 | } |
| 142 | intentService.execute(ops.build()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 145 | private List<Intent> getOpticalPaths(ConnectPoint ingress, ConnectPoint egress) { |
| 146 | Set<Path> paths = pathService.getPaths(ingress.deviceId(), |
| 147 | egress.deviceId(), |
| 148 | new OpticalLinkWeight()); |
| 149 | |
| 150 | if (paths.isEmpty()) { |
| 151 | return Lists.newArrayList(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 152 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 153 | |
| 154 | ConnectPoint srcWdmPoint = null; |
| 155 | ConnectPoint dstWdmPoint = null; |
| 156 | Iterator<Path> itrPath = paths.iterator(); |
| 157 | Path firstPath = itrPath.next(); |
| 158 | log.info(firstPath.links().toString()); |
| 159 | |
| 160 | List<Intent> connectionList = Lists.newArrayList(); |
| 161 | |
| 162 | Iterator<Link> itrLink = firstPath.links().iterator(); |
| 163 | while (itrLink.hasNext()) { |
| 164 | Link link1 = itrLink.next(); |
| 165 | if (!isOpticalLink(link1)) { |
| 166 | continue; |
| 167 | } else { |
| 168 | srcWdmPoint = link1.dst(); |
| 169 | dstWdmPoint = srcWdmPoint; |
| 170 | } |
| 171 | |
| 172 | while (true) { |
| 173 | if (itrLink.hasNext()) { |
| 174 | Link link2 = itrLink.next(); |
| 175 | dstWdmPoint = link2.src(); |
| 176 | } else { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | Intent opticalIntent = new OpticalConnectivityIntent(appId, |
| 182 | srcWdmPoint, |
| 183 | dstWdmPoint); |
| 184 | log.info("Creating optical intent from {} to {}", srcWdmPoint, dstWdmPoint); |
| 185 | connectionList.add(opticalIntent); |
| 186 | } |
| 187 | return connectionList; |
| 188 | } |
| 189 | |
| 190 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 191 | |
| 192 | private void teardownLightpath(Intent intent) { |
| 193 | // TODO: tear down the idle lightpath if the utilization is close to zero. |
| 194 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 195 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 196 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 197 | private static boolean isOpticalLink(Link link) { |
| 198 | boolean isOptical = false; |
| 199 | Link.Type lt = link.type(); |
| 200 | if (lt == Link.Type.OPTICAL) { |
| 201 | isOptical = true; |
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 | return isOptical; |
| 204 | } |
Praseed Balakrishnan | c002965 | 2014-11-14 13:38:49 -0800 | [diff] [blame] | 205 | |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 206 | private static class OpticalLinkWeight implements LinkWeight { |
| 207 | @Override |
| 208 | public double weight(TopologyEdge edge) { |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 209 | if (edge.link().state() == Link.State.INACTIVE) { |
| 210 | return -1; // ignore inactive links |
| 211 | } |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 212 | if (isOpticalLink(edge.link())) { |
| 213 | return 1000.0; // optical links |
| 214 | } else { |
| 215 | return 1.0; // packet links |
| 216 | } |
| 217 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } |