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