MaoLu | 0cde9ce | 2017-01-23 19:08:35 -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 org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry; |
| 19 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsReply; |
| 20 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsRequest; |
| 21 | import org.projectfloodlight.openflow.protocol.OFCalientPortStatsEntry; |
| 22 | import org.projectfloodlight.openflow.protocol.OFCalientPortStatsRequest; |
| 23 | import org.projectfloodlight.openflow.protocol.OFCalientPortStatsReply; |
| 24 | import org.projectfloodlight.openflow.protocol.OFCalientStatsReply; |
| 25 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 26 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 27 | import org.projectfloodlight.openflow.protocol.OFStatsRequest; |
| 28 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 29 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
| 30 | import org.projectfloodlight.openflow.protocol.OFType; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.HashMap; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | import java.util.concurrent.atomic.AtomicBoolean; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import org.onosproject.net.DefaultAnnotations; |
| 41 | import org.onosproject.net.Device; |
| 42 | import org.onosproject.net.device.DefaultPortDescription; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.device.PortDescription; |
| 45 | import org.onosproject.net.Port; |
| 46 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
| 47 | import org.onosproject.openflow.controller.PortDescPropertyType; |
| 48 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
| 49 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 50 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 51 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
| 52 | import org.projectfloodlight.openflow.protocol.OFObject; |
| 53 | |
| 54 | import com.google.common.collect.ImmutableList; |
| 55 | import com.google.common.collect.ImmutableSet; |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Oplink Open Flow Protection Optical Switch handshaker - for Open Flow 1.3. |
| 60 | * In order to reduce the code changes in the short term, we reuse Calient message structure. |
| 61 | */ |
| 62 | public class OplinkSwitchHandshaker extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch { |
| 63 | |
| 64 | private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false); |
| 65 | private List<OFCalientPortDescStatsEntry> opticalPorts = new ArrayList<>(); |
| 66 | private static final String INPUT_PORT_STATUS = "inputStatus"; |
| 67 | private static final String OUTPUT_PORT_STATUS = "ouputStatus"; |
| 68 | private static final String INPUT_PORT_POWER = "inputPower"; |
| 69 | private static final String OUTPUT_PORT_POWER = "ouputPower"; |
| 70 | private static final String IN_SERVICE_ANNOTATION = "inService"; |
| 71 | private static final String OUT_SERVICE_ANNOTATION = "outOfService"; |
| 72 | |
| 73 | private enum SubType { |
| 74 | PORT_DESC_STATS, // Port description stats openflow message |
| 75 | FLOW_STATS, // Flow stats openflow message |
| 76 | PORT_STATS // Port stats openflow message |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Boolean supportNxRole() { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void startDriverHandshake() { |
| 86 | log.info("OPLK Switch: Starting driver handshake for sw {}", getStringId()); |
| 87 | if (startDriverHandshakeCalled) { |
| 88 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 89 | } |
| 90 | startDriverHandshakeCalled = true; |
| 91 | |
| 92 | log.debug("OPLK Switch: sendHandshakeOFExperimenterPortDescRequest for sw {}", getStringId()); |
| 93 | |
| 94 | try { |
| 95 | sendHandshakeOFExperimenterPortDescRequest(); |
| 96 | } catch (IOException e) { |
| 97 | log.error("OPLK Switch: exception while sending experimenter port desc:", e); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void processDriverHandshakeMessage(OFMessage m) { |
| 103 | if (!startDriverHandshakeCalled) { |
| 104 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 105 | } |
| 106 | if (driverHandshakeComplete.get()) { |
| 107 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 108 | } |
| 109 | |
| 110 | log.info("OPLK Switch: processDriverHandshakeMessage for sw {}", getStringId()); |
| 111 | |
| 112 | switch (m.getType()) { |
| 113 | case STATS_REPLY: // multipart message is reported as STAT |
| 114 | processOFMultipartReply((OFStatsReply) m); |
| 115 | driverHandshakeComplete.set(true); |
| 116 | break; |
| 117 | default: |
| 118 | log.warn("OPLK Switch: Received message {} during switch-driver " + |
| 119 | "subhandshake from switch {} ... " + |
| 120 | "Ignoring message", m, getStringId()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public final void sendMsg(OFMessage m) { |
| 126 | List<OFMessage> messages = new ArrayList<>(); |
| 127 | messages.add(m); |
| 128 | if (m.getType() == OFType.STATS_REQUEST) { |
| 129 | OFStatsRequest sr = (OFStatsRequest) m; |
| 130 | log.debug("OPLK Switch: Rebuilding stats request type {}", sr.getStatsType()); |
| 131 | switch (sr.getStatsType()) { |
| 132 | case PORT: |
| 133 | //Send experiment status request for Optical Fiber switch to device |
| 134 | //Note: We just re-use calient message for a short term. |
| 135 | OFCalientPortStatsRequest portRequest = this.factory().buildCalientPortStatsRequest() |
| 136 | .setXid(sr.getXid()) |
| 137 | .setFlags(sr.getFlags()) |
| 138 | .build(); |
| 139 | messages.add(portRequest); |
| 140 | break; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | } else { |
| 145 | log.debug("OPLK Switch: sends msg:{}, as is", m.getType()); |
| 146 | } |
| 147 | super.sendMsg(messages); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean isDriverHandshakeComplete() { |
| 152 | return driverHandshakeComplete.get(); |
| 153 | } |
| 154 | |
| 155 | private void sendHandshakeOFExperimenterPortDescRequest() throws |
| 156 | IOException { |
| 157 | /**Note: |
| 158 | * Oplink protection switch and Calient switch are both optical fiber switch, |
| 159 | * so Calient port description matches well for Oplink switch. |
| 160 | * OFCalientPortDescStatsRequest is generated by loxi. |
| 161 | * If change the OF message name, we need to change onos-loxi. |
| 162 | * To reduce code change for a short term, we reuse calient message and message name. |
| 163 | * These will be re-processed in the future. |
| 164 | */ |
| 165 | OFCalientPortDescStatsRequest preq = factory() |
| 166 | .buildCalientPortDescStatsRequest() |
| 167 | .setXid(getNextTransactionId()) |
| 168 | .build(); |
| 169 | |
| 170 | log.info("OPLK Switch: Sending experimented port description message {}", preq); |
| 171 | |
| 172 | this.sendHandshakeMessage(preq); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Device.Type deviceType() { |
| 177 | return Device.Type.FIBER_SWITCH; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * OduClt ports are reported as regular ETH ports. |
| 182 | */ |
| 183 | @Override |
| 184 | public List<OFPortDesc> getPorts() { |
| 185 | return ImmutableList.copyOf( |
| 186 | ports.stream().flatMap(p -> p.getEntries().stream()) |
| 187 | .collect(Collectors.toList())); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public List<? extends OFObject> getPortsOf(PortDescPropertyType type) { |
| 192 | return ImmutableList.copyOf(opticalPorts); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Set<PortDescPropertyType> getPortTypes() { |
| 197 | return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public List<PortDescription> processExpPortStats(OFMessage msg) { |
| 202 | OFCalientStatsReply statsReply = (OFCalientStatsReply) msg; |
| 203 | //Sub type from openflowj is start from index 1 |
| 204 | SubType type = SubType.values()[(int) statsReply.getSubtype() - 1]; |
| 205 | switch (type) { |
| 206 | case PORT_STATS: |
| 207 | //Note: We just re-use calient message for a short term. |
| 208 | OFCalientPortStatsReply portStats = (OFCalientPortStatsReply) msg; |
| 209 | return buildPortDescriptions(portStats.getEntries()); |
| 210 | default: |
| 211 | //Ignore other messages |
| 212 | log.warn("OPLK Switch: Received message {} from switch {} ... " + |
| 213 | "Ignoring message", msg, getStringId()); |
| 214 | return null; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private List<PortDescription> buildPortDescriptions(List<OFCalientPortStatsEntry> entries) { |
| 219 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 220 | List<Port> ports = deviceService.getPorts(this.data().deviceId()); |
| 221 | HashMap<Long, OFCalientPortStatsEntry> statsMap = new HashMap<>(entries.size()); |
| 222 | entries.forEach(entry -> statsMap.put((long) entry.getPortNo().getPortNumber(), entry)); |
| 223 | final List<PortDescription> portDescs = new ArrayList<>(); |
| 224 | for (Port port : ports) { |
| 225 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 226 | builder.putAll(port.annotations()); |
| 227 | OFCalientPortStatsEntry entry = statsMap.get(port.number().toLong()); |
| 228 | if (entry == null) { |
| 229 | continue; |
| 230 | } |
| 231 | builder.set(INPUT_PORT_POWER, entry.getInportPower()); |
| 232 | builder.set(OUTPUT_PORT_POWER, entry.getOutportPower()); |
| 233 | //Note: There are some mistakes about bitmask encoding and decoding in openflowj. |
| 234 | //We just use this code for a short term, and will modify in the future. |
| 235 | if (entry.getInOperStatus().isEmpty()) { |
| 236 | builder.set(INPUT_PORT_STATUS, IN_SERVICE_ANNOTATION); |
| 237 | } else { |
| 238 | builder.set(INPUT_PORT_STATUS, OUT_SERVICE_ANNOTATION); |
| 239 | } |
| 240 | if (entry.getOutOperStatus().isEmpty()) { |
| 241 | builder.set(OUTPUT_PORT_STATUS, IN_SERVICE_ANNOTATION); |
| 242 | } else { |
| 243 | builder.set(OUTPUT_PORT_STATUS, OUT_SERVICE_ANNOTATION); |
| 244 | } |
| 245 | portDescs.add(new DefaultPortDescription(port.number(), port.isEnabled(), |
| 246 | port.type(), port.portSpeed(), builder.build())); |
| 247 | } |
| 248 | return portDescs; |
| 249 | } |
| 250 | |
| 251 | private void processOFMultipartReply(OFStatsReply stats) { |
| 252 | log.debug("OPLK Switch: Received message {} during switch-driver " + |
| 253 | "subhandshake from switch {} ... ", stats, getStringId()); |
| 254 | //Process experimenter messages |
| 255 | if (stats.getStatsType() == OFStatsType.EXPERIMENTER) { |
| 256 | try { |
| 257 | //Note: We just re-use calient message for a short term. |
| 258 | OFCalientPortDescStatsReply descReply = (OFCalientPortDescStatsReply) stats; |
| 259 | opticalPorts.addAll(descReply.getPortDesc()); |
| 260 | driverHandshakeComplete.set(true); |
| 261 | } catch (ClassCastException e) { |
| 262 | log.error("OPLK Switch: Unexspected Experimenter Multipart message type {} ", |
| 263 | stats.getClass().getName()); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |