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; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 33 | |
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(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 123 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 124 | LinkWeight weight = new LinkWeight() { |
| 125 | @Override |
| 126 | public double weight(TopologyEdge edge) { |
| 127 | boolean isOptical = false; |
| 128 | String t = edge.link().annotations().value("linkType"); |
| 129 | if (t.equals("WDM")) { |
| 130 | isOptical = true; |
| 131 | } |
| 132 | if (isOptical) { |
| 133 | return 1000; // optical links |
| 134 | } else { |
| 135 | return 10; // packet links |
| 136 | } |
| 137 | } |
| 138 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 139 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 140 | Set<Path> paths = topologyService.getPaths(topology, |
| 141 | pktIntent.ingressPoint().deviceId(), |
| 142 | pktIntent.egressPoint().deviceId(), |
| 143 | weight); |
| 144 | |
| 145 | if (paths.isEmpty()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 149 | ConnectPoint srcWdmPoint = null; |
| 150 | ConnectPoint dstWdmPoint = null; |
| 151 | Iterator<Path> itrPath = paths.iterator(); |
| 152 | Path firstPath = itrPath.next(); |
| 153 | log.info(firstPath.toString()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 154 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 155 | ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>(); |
| 156 | |
| 157 | Iterator<Link> itrLink = firstPath.links().iterator(); |
| 158 | while (itrLink.hasNext()) { |
| 159 | Link link1 = itrLink.next(); |
| 160 | if (!isOpticalLink(link1)) { |
| 161 | continue; |
| 162 | } else { |
| 163 | srcWdmPoint = link1.dst(); |
| 164 | dstWdmPoint = srcWdmPoint; |
| 165 | } |
| 166 | |
| 167 | while (true) { |
| 168 | |
| 169 | if (itrLink.hasNext()) { |
| 170 | Link link2 = itrLink.next(); |
| 171 | dstWdmPoint = link2.src(); |
| 172 | } else { |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | if (itrLink.hasNext()) { |
| 177 | Link link3 = itrLink.next(); |
| 178 | if (!isOpticalLink(link3)) { |
| 179 | break; |
| 180 | } |
| 181 | } else { |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | Map<ConnectPoint, ConnectPoint> pair = |
| 187 | new HashMap<ConnectPoint, ConnectPoint>(); |
| 188 | pair.put(srcWdmPoint, dstWdmPoint); |
| 189 | |
| 190 | connectionList.add(pair); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 193 | for (Map<ConnectPoint, ConnectPoint> map : connectionList) { |
| 194 | for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 195 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 196 | ConnectPoint src = entry.getKey(); |
| 197 | ConnectPoint dst = entry.getValue(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 198 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 199 | Intent opticalIntent = new OpticalConnectivityIntent(appId, |
| 200 | srcWdmPoint, |
| 201 | dstWdmPoint); |
| 202 | |
| 203 | intentService.submit(opticalIntent); |
| 204 | |
| 205 | log.info(opticalIntent.toString()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | private boolean isOpticalLink(Link link) { |
| 212 | boolean isOptical = false; |
| 213 | String t = link.annotations().value("linkType"); |
| 214 | if (t.equals("WDM") || t.equals("PktOptLink")) { |
| 215 | isOptical = true; |
| 216 | } |
| 217 | return isOptical; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private void teardownLightpath(Intent intent) { |
| 221 | // TODO: tear down the idle lightpath if the utilization is close to zero. |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | } |