yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.driver.optical.handshaker; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | |
| 29 | import org.jboss.netty.buffer.ChannelBuffer; |
| 30 | import org.jboss.netty.buffer.ChannelBuffers; |
| 31 | import org.onosproject.drivers.optical.OpticalAdjacencyLinkService; |
| 32 | import org.onosproject.net.Annotations; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DefaultAnnotations; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Link; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.device.DefaultPortDescription; |
| 41 | import org.onosproject.net.device.DeviceService; |
| 42 | import org.onosproject.net.device.PortDescription; |
| 43 | import org.onosproject.net.link.DefaultLinkDescription; |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 44 | import org.onosproject.net.link.LinkService; |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 45 | import org.onosproject.net.optical.OpticalAnnotations; |
| 46 | import org.onosproject.openflow.controller.Dpid; |
| 47 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
| 48 | import org.onosproject.openflow.controller.PortDescPropertyType; |
| 49 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
| 50 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 51 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 52 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
| 53 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 54 | import org.projectfloodlight.openflow.protocol.OFCircuitPortsReply; |
| 55 | import org.projectfloodlight.openflow.protocol.OFCircuitPortsRequest; |
| 56 | import org.projectfloodlight.openflow.protocol.OFExpExtAdId; |
| 57 | import org.projectfloodlight.openflow.protocol.OFExpPortAdidOtn; |
| 58 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacency; |
| 59 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacencyId; |
| 60 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacencyReply; |
| 61 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacencyRequest; |
| 62 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 63 | import org.projectfloodlight.openflow.protocol.OFObject; |
| 64 | import org.projectfloodlight.openflow.protocol.OFOplinkPortPower; |
| 65 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 66 | import org.projectfloodlight.openflow.protocol.OFPortOptical; |
| 67 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 68 | import org.projectfloodlight.openflow.protocol.OFStatsRequest; |
| 69 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
| 70 | import org.projectfloodlight.openflow.protocol.OFType; |
| 71 | import org.projectfloodlight.openflow.protocol.OFOplinkPortPowerRequest; |
| 72 | import org.projectfloodlight.openflow.protocol.OFOplinkPortPowerReply; |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Driver for Oplink single WSS 8D ROADM. |
| 77 | * |
| 78 | * Driver implements custom handshaker and supports for Optical channel Port based on OpenFlow OTN extension. |
| 79 | * The device consists of Och ports, and performances wavelength cross-connect among the ports. |
| 80 | */ |
| 81 | public class OplinkRoadm extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch { |
| 82 | |
| 83 | private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false); |
| 84 | private List<OFPortOptical> opticalPorts; |
| 85 | |
| 86 | @Override |
| 87 | public List<? extends OFObject> getPortsOf(PortDescPropertyType type) { |
| 88 | return ImmutableList.copyOf(opticalPorts); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | /** |
| 93 | * Returns a list of standard (Ethernet) ports. |
| 94 | * |
| 95 | * @return List of ports |
| 96 | */ |
| 97 | public List<OFPortDesc> getPorts() { |
| 98 | return Collections.EMPTY_LIST; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<PortDescPropertyType> getPortTypes() { |
| 103 | return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Boolean supportNxRole() { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void startDriverHandshake() { |
| 113 | log.warn("Starting driver handshake for sw {}", getStringId()); |
| 114 | if (startDriverHandshakeCalled) { |
| 115 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 116 | } |
| 117 | startDriverHandshakeCalled = true; |
| 118 | try { |
| 119 | sendHandshakeOFExperimenterPortDescRequest(); |
| 120 | } catch (IOException e) { |
| 121 | log.error("OPLK ROADM exception while sending experimenter port desc:", e); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean isDriverHandshakeComplete() { |
| 127 | return driverHandshakeComplete.get(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void processDriverHandshakeMessage(OFMessage m) { |
| 132 | |
| 133 | if (!startDriverHandshakeCalled) { |
| 134 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 135 | } |
| 136 | |
| 137 | if (driverHandshakeComplete.get()) { |
| 138 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 139 | } |
| 140 | |
| 141 | switch (m.getType()) { |
| 142 | case BARRIER_REPLY: |
| 143 | log.debug("OPLK ROADM Received barrier response"); |
| 144 | break; |
| 145 | case ERROR: |
| 146 | log.error("Switch {} Error {}", getStringId(), m); |
| 147 | break; |
| 148 | case FEATURES_REPLY: |
| 149 | break; |
| 150 | case FLOW_REMOVED: |
| 151 | break; |
| 152 | case GET_ASYNC_REPLY: |
| 153 | break; |
| 154 | case PACKET_IN: |
| 155 | break; |
| 156 | case PORT_STATUS: |
| 157 | processOFPortStatus((OFCircuitPortStatus) m); |
| 158 | break; |
| 159 | case QUEUE_GET_CONFIG_REPLY: |
| 160 | break; |
| 161 | case ROLE_REPLY: |
| 162 | break; |
| 163 | case STATS_REPLY: |
| 164 | OFStatsReply stats = (OFStatsReply) m; |
| 165 | if (stats.getStatsType() == OFStatsType.EXPERIMENTER) { |
| 166 | log.warn("OPLK ROADM : Received multipart (port desc) reply message {}", m); |
| 167 | //OTN Optical extension 1.0 port-desc |
| 168 | createOpticalPortList((OFCircuitPortsReply) m); |
| 169 | driverHandshakeComplete.set(true); |
| 170 | } |
| 171 | break; |
| 172 | default: |
| 173 | log.warn("Received message {} during switch-driver " + |
| 174 | "subhandshake " + "from switch {} ... " + |
| 175 | "Ignoring message", m, |
| 176 | getStringId()); |
| 177 | |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void processOFPortStatus(OFCircuitPortStatus ps) { |
| 182 | log.debug("OPLK ROADM ..OF Port Status :", ps); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public Device.Type deviceType() { |
| 187 | return Device.Type.ROADM; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public final void sendMsg(OFMessage m) { |
| 192 | List<OFMessage> messages = new ArrayList<>(); |
| 193 | messages.add(m); |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 194 | |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 195 | if (m.getType() == OFType.STATS_REQUEST) { |
| 196 | OFStatsRequest sr = (OFStatsRequest) m; |
| 197 | log.debug("OPLK ROADM rebuilding stats request type {}", sr.getStatsType()); |
| 198 | switch (sr.getStatsType()) { |
| 199 | case PORT: |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 200 | //add Oplink experiment stats message to get the port's current power |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 201 | OFOplinkPortPowerRequest powerRequest = this.factory().buildOplinkPortPowerRequest() |
| 202 | .setXid(sr.getXid()) |
| 203 | .setFlags(sr.getFlags()) |
| 204 | .build(); |
| 205 | messages.add(powerRequest); |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 206 | // add experiment message to get adjacent ports |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 207 | OFExpPortAdjacencyRequest adjacencyRequest = this.factory().buildExpPortAdjacencyRequest() |
| 208 | .setXid(sr.getXid()) |
| 209 | .setFlags(sr.getFlags()) |
| 210 | .build(); |
| 211 | messages.add(adjacencyRequest); |
| 212 | break; |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | } else { |
| 217 | log.debug("OPLK ROADM sends msg:{}, as is", m.getType()); |
| 218 | } |
| 219 | |
| 220 | for (OFMessage message : messages) { |
| 221 | super.sendMsg(message); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void sendHandshakeOFExperimenterPortDescRequest() throws IOException { |
| 226 | // send multi part message for port description for optical switches |
| 227 | OFCircuitPortsRequest circuitPortsRequest = factory() |
| 228 | .buildCircuitPortsRequest().setXid(getNextTransactionId()) |
| 229 | .build(); |
| 230 | log.info("OPLK ROADM : Sending experimented circuit port stats " + |
| 231 | "message " + |
| 232 | "{}", |
| 233 | circuitPortsRequest); |
| 234 | this.sendHandshakeMessage(circuitPortsRequest); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Builds list of OFPortOptical ports based on the multi-part circuit ports reply. |
| 239 | * Ensure the optical transport port's signal type is configured correctly. |
| 240 | * |
| 241 | * @param wPorts OF reply with circuit ports |
| 242 | */ |
| 243 | private void createOpticalPortList(OFCircuitPortsReply wPorts) { |
| 244 | opticalPorts = new ArrayList<>(); |
| 245 | opticalPorts.addAll(wPorts.getEntries()); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public List<PortDescription> processExpPortStats(OFMessage msg) { |
| 250 | if (msg instanceof OFOplinkPortPowerReply) { |
| 251 | return buildPortPowerDescriptions(((OFOplinkPortPowerReply) msg).getEntries()); |
| 252 | } else if (msg instanceof OFExpPortAdjacencyReply) { |
| 253 | return buildPortAdjacencyDescriptions(((OFExpPortAdjacencyReply) msg).getEntries()); |
| 254 | } |
| 255 | return Collections.emptyList(); |
| 256 | } |
| 257 | |
| 258 | private List<PortDescription> buildPortPowerDescriptions(List<OFOplinkPortPower> portPowers) { |
| 259 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 260 | List<Port> ports = deviceService.getPorts(this.data().deviceId()); |
| 261 | HashMap<Long, OFOplinkPortPower> powerMap = new HashMap<>(portPowers.size()); |
| 262 | portPowers.forEach(power -> powerMap.put((long) power.getPort(), power)); |
| 263 | final List<PortDescription> portDescs = new ArrayList<>(); |
| 264 | for (Port port : ports) { |
| 265 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 266 | builder.putAll(port.annotations()); |
| 267 | OFOplinkPortPower power = powerMap.get(port.number().toLong()); |
| 268 | if (power != null) { |
| 269 | builder.set(OpticalAnnotations.CURRENT_POWER, Long.toString(power.getPowerValue())); |
| 270 | } |
| 271 | portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(), |
| 272 | port.type(), port.portSpeed(), builder.build())); |
| 273 | } |
| 274 | return portDescs; |
| 275 | } |
| 276 | |
| 277 | private OplinkPortAdjacency getNeighbor(OFExpPortAdjacency ad) { |
| 278 | for (OFExpPortAdjacencyId adid : ad.getProperties()) { |
| 279 | List<OFExpExtAdId> otns = adid.getAdId(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 280 | if (otns != null && !otns.isEmpty()) { |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 281 | OFExpPortAdidOtn otn = (OFExpPortAdidOtn) otns.get(0); |
| 282 | // ITU-T G.7714 ETH MAC Format (in second 16 bytes of the following) |
| 283 | // |---------------------------------------------------------------------------| |
| 284 | // | Other format (16 bytes) | |
| 285 | // |---------------------------------------------------------------------------| |
| 286 | // | Header (2 bytes) | ID (4 bits) | MAC (6 bytes) | Port (4 bytes) | Unused | |
| 287 | // |---------------------------------------------------------------------------| |
| 288 | ChannelBuffer buffer = ChannelBuffers.buffer(32); |
| 289 | otn.getOpspec().write32Bytes(buffer); |
| 290 | long mac = buffer.getLong(18) << 4 >>> 16; |
| 291 | int port = (int) (buffer.getLong(24) << 4 >>> 32); |
| 292 | // Oplink does not use the 4 most significant bytes of Dpid so Dpid can be |
| 293 | // constructed from MAC address |
| 294 | return new OplinkPortAdjacency(DeviceId.deviceId(Dpid.uri(new Dpid(mac))), |
| 295 | PortNumber.portNumber(port)); |
| 296 | } |
| 297 | } |
| 298 | return null; |
| 299 | } |
| 300 | |
| 301 | private List<PortDescription> buildPortAdjacencyDescriptions(List<OFExpPortAdjacency> portAds) { |
| 302 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 303 | List<Port> ports = deviceService.getPorts(this.data().deviceId()); |
| 304 | |
| 305 | // Map port's number with port's adjacency |
| 306 | HashMap<Long, OFExpPortAdjacency> adMap = new HashMap<>(portAds.size()); |
| 307 | portAds.forEach(ad -> adMap.put((long) ad.getPortNo().getPortNumber(), ad)); |
| 308 | |
| 309 | List<PortDescription> portDescs = new ArrayList<>(); |
| 310 | for (Port port : ports) { |
| 311 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 312 | Annotations oldAnnotations = port.annotations(); |
| 313 | builder.putAll(oldAnnotations); |
| 314 | OFExpPortAdjacency ad = adMap.get(port.number().toLong()); |
| 315 | if (ad != null) { |
| 316 | // neighbor discovered, add to port descriptions |
| 317 | OplinkPortAdjacency neighbor = getNeighbor(ad); |
| 318 | String newId = neighbor.getDeviceId().toString(); |
| 319 | String newPort = neighbor.getPort().toString(); |
| 320 | // Check if annotation already exists |
| 321 | if (!newId.equals(oldAnnotations.value(OpticalAnnotations.NEIGHBOR_ID)) || |
| 322 | !newPort.equals(oldAnnotations.value(OpticalAnnotations.NEIGHBOR_PORT))) { |
| 323 | builder.set(OpticalAnnotations.NEIGHBOR_ID, newId); |
| 324 | builder.set(OpticalAnnotations.NEIGHBOR_PORT, newPort); |
| 325 | } |
| 326 | addLink(port.number(), neighbor); |
| 327 | } else { |
| 328 | // no neighbors found |
| 329 | builder.remove(OpticalAnnotations.NEIGHBOR_ID); |
| 330 | builder.remove(OpticalAnnotations.NEIGHBOR_PORT); |
| 331 | removeLink(port.number()); |
| 332 | } |
| 333 | portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(), |
| 334 | port.type(), port.portSpeed(), builder.build())); |
| 335 | } |
| 336 | return portDescs; |
| 337 | } |
| 338 | |
| 339 | private void addLink(PortNumber portNumber, OplinkPortAdjacency neighbor) { |
| 340 | ConnectPoint dst = new ConnectPoint(handler().data().deviceId(), portNumber); |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 341 | ConnectPoint src = new ConnectPoint(neighbor.getDeviceId(), neighbor.getPort()); |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 342 | OpticalAdjacencyLinkService adService = |
| 343 | this.handler().get(OpticalAdjacencyLinkService.class); |
| 344 | adService.linkDetected(new DefaultLinkDescription(src, dst, Link.Type.OPTICAL)); |
| 345 | } |
| 346 | |
| 347 | // Remove incoming link with port if there are any. |
| 348 | private void removeLink(PortNumber portNumber) { |
| 349 | ConnectPoint dst = new ConnectPoint(handler().data().deviceId(), portNumber); |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 350 | // Check so only incoming links are removed |
| 351 | Set<Link> links = this.handler().get(LinkService.class).getIngressLinks(dst); |
| 352 | if (!links.isEmpty()) { |
| 353 | OpticalAdjacencyLinkService adService = |
| 354 | this.handler().get(OpticalAdjacencyLinkService.class); |
| 355 | adService.linksVanished(dst); |
| 356 | } |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private class OplinkPortAdjacency { |
| 360 | private DeviceId deviceId; |
| 361 | private PortNumber portNumber; |
| 362 | |
| 363 | public OplinkPortAdjacency(DeviceId deviceId, PortNumber portNumber) { |
| 364 | this.deviceId = deviceId; |
| 365 | this.portNumber = portNumber; |
| 366 | } |
| 367 | |
| 368 | public DeviceId getDeviceId() { |
| 369 | return deviceId; |
| 370 | } |
| 371 | |
| 372 | public PortNumber getPort() { |
| 373 | return portNumber; |
| 374 | } |
| 375 | } |
| 376 | } |