MaoLu | c201ae4 | 2017-02-06 17:57:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 java.util.ArrayList; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
| 24 | import org.jboss.netty.buffer.ChannelBuffers; |
| 25 | import org.onosproject.drivers.optical.OpticalAdjacencyLinkService; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.Annotations; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DefaultPortDescription; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.device.PortDescription; |
| 36 | import org.onosproject.net.link.DefaultLinkDescription; |
| 37 | import org.onosproject.net.link.LinkService; |
| 38 | import org.onosproject.net.optical.OpticalAnnotations; |
| 39 | import org.onosproject.openflow.controller.Dpid; |
| 40 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
| 41 | import org.projectfloodlight.openflow.protocol.OFCircuitPortsRequest; |
| 42 | import org.projectfloodlight.openflow.protocol.OFExpExtAdId; |
| 43 | import org.projectfloodlight.openflow.protocol.OFExpPortAdidOtn; |
| 44 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacency; |
| 45 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacencyId; |
| 46 | import org.projectfloodlight.openflow.protocol.OFExpPortAdjacencyRequest; |
| 47 | import org.projectfloodlight.openflow.protocol.OFOplinkPortPower; |
| 48 | import org.projectfloodlight.openflow.protocol.OFOplinkPortPowerRequest; |
| 49 | |
| 50 | /** |
| 51 | * Oplink handshaker utility. |
| 52 | */ |
| 53 | public class OplinkHandshakerUtil { |
| 54 | |
| 55 | // Parent driver instance |
| 56 | private OpenFlowSwitchDriver driver; |
| 57 | // Total count of opspec in OFExpPortAdidOtn |
| 58 | private static final int OPSPEC_BYTES = 32; |
| 59 | // Bit count of id in opspec |
| 60 | private static final int OPSPEC_ID_BITS = 4; |
| 61 | // Start byte position of mac info |
| 62 | private static final int OPSPEC_MAC_POS = 18; |
| 63 | // Bit offset for mac |
| 64 | private static final int OPSPEC_MAC_BIT_OFF = 16; |
| 65 | // Start byte position of port info |
| 66 | private static final int OPSPEC_PORT_POS = 24; |
| 67 | // Right bit offset for mac |
| 68 | private static final int OPSPEC_PORT_BIT_OFF = 32; |
| 69 | |
| 70 | /** |
| 71 | * Create a new OplinkHandshakerUtil. |
| 72 | * @param driver parent driver instance |
| 73 | */ |
| 74 | public OplinkHandshakerUtil(OpenFlowSwitchDriver driver) { |
| 75 | this.driver = driver; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates an oplink port power request OF message. |
| 80 | * |
| 81 | * @return OF message of oplink port power request |
| 82 | */ |
| 83 | public OFOplinkPortPowerRequest buildPortPowerRequest() { |
| 84 | OFOplinkPortPowerRequest request = driver.factory().buildOplinkPortPowerRequest() |
| 85 | .setXid(driver.getNextTransactionId()) |
| 86 | .build(); |
| 87 | return request; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates port adjacency request OF message. |
| 92 | * |
| 93 | * @return OF message of oplink port adjacency request |
| 94 | */ |
| 95 | public OFExpPortAdjacencyRequest buildPortAdjacencyRequest() { |
| 96 | OFExpPortAdjacencyRequest request = driver.factory().buildExpPortAdjacencyRequest() |
| 97 | .setXid(driver.getNextTransactionId()) |
| 98 | .build(); |
| 99 | return request; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates an oplink port description request OF message. |
| 104 | * |
| 105 | * @return OF message of oplink port description request |
| 106 | */ |
| 107 | public OFCircuitPortsRequest buildCircuitPortsRequest() { |
| 108 | OFCircuitPortsRequest request = driver.factory().buildCircuitPortsRequest() |
| 109 | .setXid(driver.getNextTransactionId()) |
| 110 | .build(); |
| 111 | return request; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Creates port descriptions with current power. |
| 116 | * |
| 117 | * @param portPowers current power |
| 118 | * @return port descriptions |
| 119 | */ |
| 120 | public List<PortDescription> buildPortPowerDescriptions(List<OFOplinkPortPower> portPowers) { |
| 121 | DeviceService deviceService = driver.handler().get(DeviceService.class); |
| 122 | List<Port> ports = deviceService.getPorts(driver.data().deviceId()); |
| 123 | HashMap<Long, OFOplinkPortPower> powerMap = new HashMap<>(portPowers.size()); |
| 124 | // Get each port power value |
| 125 | portPowers.forEach(power -> powerMap.put((long) power.getPort(), power)); |
| 126 | final List<PortDescription> portDescs = new ArrayList<>(); |
| 127 | for (Port port : ports) { |
| 128 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 129 | builder.putAll(port.annotations()); |
| 130 | OFOplinkPortPower power = powerMap.get(port.number().toLong()); |
| 131 | if (power != null) { |
| 132 | // power value is actually signed-short value, down casting to recover sign bit. |
| 133 | builder.set(OpticalAnnotations.CURRENT_POWER, Short.toString((short) power.getPowerValue())); |
| 134 | } |
| 135 | portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(), |
| 136 | port.type(), port.portSpeed(), builder.build())); |
| 137 | } |
| 138 | return portDescs; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Creates port descriptions with adjacency. |
| 143 | * |
| 144 | * @param portAds adjacency information |
| 145 | * @return port descriptions |
| 146 | */ |
| 147 | public List<PortDescription> buildPortAdjacencyDescriptions(List<OFExpPortAdjacency> portAds) { |
| 148 | DeviceService deviceService = driver.handler().get(DeviceService.class); |
| 149 | List<Port> ports = deviceService.getPorts(driver.data().deviceId()); |
| 150 | // Map port's number with port's adjacency |
| 151 | HashMap<Long, OFExpPortAdjacency> adMap = new HashMap<>(portAds.size()); |
| 152 | portAds.forEach(ad -> adMap.put((long) ad.getPortNo().getPortNumber(), ad)); |
| 153 | List<PortDescription> portDescs = new ArrayList<>(); |
| 154 | for (Port port : ports) { |
| 155 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 156 | Annotations oldAnnotations = port.annotations(); |
| 157 | builder.putAll(oldAnnotations); |
| 158 | OFExpPortAdjacency ad = adMap.get(port.number().toLong()); |
| 159 | OplinkPortAdjacency neighbor = getNeighbor(ad); |
| 160 | if (neighbor == null) { |
| 161 | // no neighbors found |
| 162 | builder.remove(OpticalAnnotations.NEIGHBOR_ID); |
| 163 | builder.remove(OpticalAnnotations.NEIGHBOR_PORT); |
| 164 | removeLink(port.number()); |
| 165 | } else { |
| 166 | // neighbor discovered, add to port descriptions |
| 167 | String newId = neighbor.getDeviceId().toString(); |
| 168 | String newPort = neighbor.getPort().toString(); |
| 169 | // Check if annotation already exists |
| 170 | if (!newId.equals(oldAnnotations.value(OpticalAnnotations.NEIGHBOR_ID)) || |
| 171 | !newPort.equals(oldAnnotations.value(OpticalAnnotations.NEIGHBOR_PORT))) { |
| 172 | builder.set(OpticalAnnotations.NEIGHBOR_ID, newId); |
| 173 | builder.set(OpticalAnnotations.NEIGHBOR_PORT, newPort); |
| 174 | } |
| 175 | addLink(port.number(), neighbor); |
| 176 | } |
| 177 | portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(), |
| 178 | port.type(), port.portSpeed(), builder.build())); |
| 179 | } |
| 180 | return portDescs; |
| 181 | } |
| 182 | |
| 183 | private OplinkPortAdjacency getNeighbor(OFExpPortAdjacency ad) { |
| 184 | // Check input parameter |
| 185 | if (ad == null) { |
| 186 | return null; |
| 187 | } |
| 188 | // Get adjacency properties |
| 189 | for (OFExpPortAdjacencyId adid : ad.getProperties()) { |
| 190 | List<OFExpExtAdId> otns = adid.getAdId(); |
| 191 | if (otns != null && otns.size() > 0) { |
| 192 | OFExpPortAdidOtn otn = (OFExpPortAdidOtn) otns.get(0); |
| 193 | // ITU-T G.7714 ETH MAC Format (in second 16 bytes of the following) |
| 194 | // |---------------------------------------------------------------------------| |
| 195 | // | Other format (16 bytes) | |
| 196 | // |---------------------------------------------------------------------------| |
| 197 | // | Header (2 bytes) | ID (4 BITS) | MAC (6 bytes) | Port (4 bytes) | Unused | |
| 198 | // |---------------------------------------------------------------------------| |
| 199 | ChannelBuffer buffer = ChannelBuffers.buffer(OPSPEC_BYTES); |
| 200 | otn.getOpspec().write32Bytes(buffer); |
| 201 | long mac = buffer.getLong(OPSPEC_MAC_POS) << OPSPEC_ID_BITS >>> OPSPEC_MAC_BIT_OFF; |
| 202 | int port = (int) (buffer.getLong(OPSPEC_PORT_POS) << OPSPEC_ID_BITS >>> OPSPEC_PORT_BIT_OFF); |
| 203 | // Oplink does not use the 4 most significant bytes of Dpid so Dpid can be |
| 204 | // constructed from MAC address |
| 205 | return new OplinkPortAdjacency(DeviceId.deviceId(Dpid.uri(new Dpid(mac))), |
| 206 | PortNumber.portNumber(port)); |
| 207 | } |
| 208 | } |
| 209 | // Returns null if no properties found |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | // Add incoming link with port |
| 214 | private void addLink(PortNumber portNumber, OplinkPortAdjacency neighbor) { |
| 215 | ConnectPoint dst = new ConnectPoint(driver.handler().data().deviceId(), portNumber); |
| 216 | ConnectPoint src = new ConnectPoint(neighbor.getDeviceId(), neighbor.getPort()); |
| 217 | OpticalAdjacencyLinkService adService = driver.handler().get(OpticalAdjacencyLinkService.class); |
| 218 | adService.linkDetected(new DefaultLinkDescription(src, dst, Link.Type.OPTICAL)); |
| 219 | } |
| 220 | |
| 221 | // Remove incoming link with port if there are any. |
| 222 | private void removeLink(PortNumber portNumber) { |
| 223 | ConnectPoint dst = new ConnectPoint(driver.handler().data().deviceId(), portNumber); |
| 224 | // Check so only incoming links are removed |
| 225 | Set<Link> links = driver.handler().get(LinkService.class).getIngressLinks(dst); |
| 226 | if (links.isEmpty()) { |
| 227 | return; |
| 228 | } |
| 229 | // If link exists, remove it |
| 230 | OpticalAdjacencyLinkService adService = driver.handler().get(OpticalAdjacencyLinkService.class); |
| 231 | adService.linksVanished(dst); |
| 232 | } |
| 233 | |
| 234 | private class OplinkPortAdjacency { |
| 235 | private DeviceId deviceId; |
| 236 | private PortNumber portNumber; |
| 237 | |
| 238 | public OplinkPortAdjacency(DeviceId deviceId, PortNumber portNumber) { |
| 239 | this.deviceId = deviceId; |
| 240 | this.portNumber = portNumber; |
| 241 | } |
| 242 | |
| 243 | public DeviceId getDeviceId() { |
| 244 | return deviceId; |
| 245 | } |
| 246 | |
| 247 | public PortNumber getPort() { |
| 248 | return portNumber; |
| 249 | } |
| 250 | } |
| 251 | } |