Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.handshaker; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 17 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 18 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
| 19 | import org.onosproject.openflow.controller.PortDescPropertyType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
| 21 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 22 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 23 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 24 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.OFCircuitPortsReply; |
| 26 | import org.projectfloodlight.openflow.protocol.OFCircuitPortsRequest; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 28 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 29 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 30 | import org.projectfloodlight.openflow.protocol.OFFlowModCommand; |
| 31 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFObject; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 36 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFType; |
| 38 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 39 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 40 | import org.projectfloodlight.openflow.protocol.action.OFActionCircuit; |
| 41 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 42 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions; |
| 43 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 44 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 45 | import org.projectfloodlight.openflow.protocol.OFActionType; |
| 46 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
| 47 | import org.projectfloodlight.openflow.types.OFPort; |
| 48 | import org.projectfloodlight.openflow.types.U8; |
alshabib | 452234e | 2014-11-25 00:03:49 -0500 | [diff] [blame] | 49 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 50 | import com.google.common.collect.ImmutableList; |
| 51 | import com.google.common.collect.ImmutableSet; |
| 52 | |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 53 | import java.io.IOException; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 54 | import java.util.List; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 55 | import java.util.Map; |
| 56 | import java.util.ArrayList; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 58 | import java.util.BitSet; |
| 59 | import java.util.stream.Collectors; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 60 | import java.util.concurrent.atomic.AtomicBoolean; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ConcurrentMap; |
| 62 | import java.util.concurrent.ConcurrentHashMap; |
| 63 | |
| 64 | import static org.projectfloodlight.openflow.protocol.OFFlowMod.Builder; |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * LINC-OE Optical Emulator switch class. |
| 68 | */ |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 69 | public class OFOpticalSwitchImplLINC13 |
| 70 | extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch { |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 71 | // default number of lambdas, assuming 50GHz channels. |
| 72 | private static final int NUM_CHLS = 80; |
| 73 | private final OFFactory factory = OFFactories.getFactory(OFVersion.OF_13); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 74 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 75 | private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 76 | private long barrierXidToWaitFor = -1; |
| 77 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 78 | private OFCircuitPortsReply wPorts; |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 79 | // book-keeping maps for allocated Linc-OE lambdas |
| 80 | protected final ConcurrentMap<OFPort, BitSet> portChannelMap = new ConcurrentHashMap<>(); |
| 81 | protected final ConcurrentMap<Match, Integer> matchMap = new ConcurrentHashMap<>(); |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 82 | |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public void startDriverHandshake() { |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 85 | log.warn("Starting driver handshake for sw {}", getStringId()); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 86 | if (startDriverHandshakeCalled) { |
| 87 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 88 | } |
| 89 | startDriverHandshakeCalled = true; |
| 90 | try { |
| 91 | sendHandshakeOFExperimenterPortDescRequest(); |
| 92 | } catch (IOException e) { |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 93 | log.error("LINC-OE exception while sending experimenter port desc:", |
| 94 | e.getMessage()); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 95 | e.printStackTrace(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean isDriverHandshakeComplete() { |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 101 | return driverHandshakeComplete.get(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void processDriverHandshakeMessage(OFMessage m) { |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 106 | if (!startDriverHandshakeCalled) { |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 107 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 108 | } |
| 109 | if (driverHandshakeComplete.get()) { |
| 110 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 111 | } |
| 112 | |
| 113 | switch (m.getType()) { |
| 114 | case BARRIER_REPLY: |
| 115 | if (m.getXid() == barrierXidToWaitFor) { |
| 116 | log.debug("LINC-OE Received barrier response"); |
| 117 | } |
| 118 | break; |
| 119 | case ERROR: |
Yuta HIGUCHI | f5416d8 | 2014-10-24 21:17:40 -0700 | [diff] [blame] | 120 | log.error("Switch {} Error {}", getStringId(), m); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 121 | break; |
| 122 | case FEATURES_REPLY: |
| 123 | break; |
| 124 | case FLOW_REMOVED: |
| 125 | break; |
| 126 | case GET_ASYNC_REPLY: |
| 127 | break; |
| 128 | case PACKET_IN: |
| 129 | break; |
| 130 | case PORT_STATUS: |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 131 | log.warn("****LINC-OE Port Status {} {}", getStringId(), m); |
| 132 | processOFPortStatus((OFCircuitPortStatus) m); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 133 | break; |
| 134 | case QUEUE_GET_CONFIG_REPLY: |
| 135 | break; |
| 136 | case ROLE_REPLY: |
| 137 | break; |
| 138 | case STATS_REPLY: |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 139 | OFStatsReply stats = (OFStatsReply) m; |
| 140 | if (stats.getStatsType() == OFStatsType.EXPERIMENTER) { |
| 141 | log.warn("LINC-OE : Received stats reply message {}", m); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 142 | wPorts = (OFCircuitPortsReply) m; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 143 | driverHandshakeComplete.set(true); |
| 144 | } |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 145 | break; |
| 146 | default: |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 147 | log.warn("Received message {} during switch-driver " + |
| 148 | "subhandshake " + "from switch {} ... " + |
| 149 | "Ignoring message", m, |
| 150 | getStringId()); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 151 | |
| 152 | } |
| 153 | } |
| 154 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 155 | public void processOFPortStatus(OFCircuitPortStatus ps) { |
| 156 | log.debug("LINC-OE ..OF Port Status :", ps); |
| 157 | |
| 158 | } |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 159 | |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 160 | private void sendHandshakeOFExperimenterPortDescRequest() throws |
| 161 | IOException { |
| 162 | // send multi part message for port description for optical switches |
| 163 | OFCircuitPortsRequest circuitPortsRequest = factory() |
| 164 | .buildCircuitPortsRequest().setXid(getNextTransactionId()) |
| 165 | .build(); |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 166 | log.warn("LINC-OE : Sending experimented circuit port stats " + |
| 167 | "message " + |
| 168 | "{}", |
| 169 | circuitPortsRequest.toString()); |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 170 | this.sendHandshakeMessage(circuitPortsRequest); |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 173 | @Override |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Returns a list of standard (Ethernet) ports. |
| 176 | * |
| 177 | * @return List of ports |
| 178 | */ |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 179 | public List<OFPortDesc> getPorts() { |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 180 | return ImmutableList.copyOf(super.getPorts()); |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 183 | |
| 184 | @Override |
| 185 | public Boolean supportNxRole() { |
| 186 | return false; |
| 187 | } |
| 188 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public boolean isOptical() { |
| 191 | return true; |
| 192 | } |
| 193 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public List<? extends OFObject> getPortsOf(PortDescPropertyType type) { |
| 196 | return ImmutableList.copyOf(wPorts.getEntries()); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Set<PortDescPropertyType> getPortTypes() { |
| 201 | return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT); |
| 202 | } |
| 203 | |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public OFMessage prepareMessage(OFMessage msg) { |
| 206 | if (OFVersion.OF_13 != msg.getVersion() || msg.getType() != OFType.FLOW_MOD) { |
| 207 | return msg; |
| 208 | } |
| 209 | OFFlowMod fm = (OFFlowMod) msg; |
| 210 | Match match = fm.getMatch(); |
| 211 | // Don't touch FlowMods that aren't Optical-related. |
| 212 | if (match.get(MatchField.OCH_SIGTYPE) == null) { |
| 213 | return msg; |
| 214 | } |
| 215 | |
| 216 | OFMessage newFM; |
| 217 | Builder builder = null; |
| 218 | List<OFAction> actions = new ArrayList<>(); |
| 219 | if (fm.getCommand() == OFFlowModCommand.ADD) { |
| 220 | builder = factory.buildFlowAdd(); |
| 221 | int lambda = allocateLambda(match.get(MatchField.IN_PORT), match); |
| 222 | CircuitSignalID sigid = new CircuitSignalID((byte) 1, (byte) 2, (short) lambda, (short) 1); |
| 223 | List<OFInstruction> instructions = fm.getInstructions(); |
| 224 | |
| 225 | newFM = buildFlowMod(builder, fm, buildMatch(match, sigid), buildActions(instructions, sigid)); |
| 226 | } else if (fm.getCommand() == OFFlowModCommand.DELETE) { |
| 227 | builder = factory.buildFlowDelete(); |
| 228 | int lambda = freeLambda(match.get(MatchField.IN_PORT), match); |
| 229 | CircuitSignalID sigid = new CircuitSignalID((byte) 1, (byte) 2, (short) lambda, (short) 1); |
| 230 | |
| 231 | newFM = buildFlowMod(builder, fm, buildMatch(match, sigid), actions); |
| 232 | } else { |
| 233 | newFM = msg; |
| 234 | } |
| 235 | log.debug("new FM = {}", newFM); |
| 236 | return newFM; |
| 237 | } |
| 238 | |
| 239 | // fetch the next available channel as the flat lambda value, or the lambda |
| 240 | // associated with a port/match combination |
| 241 | private int allocateLambda(OFPort port, Match match) { |
| 242 | Integer lambda = null; |
| 243 | synchronized (this) { |
| 244 | BitSet channels = portChannelMap.getOrDefault(port, new BitSet(NUM_CHLS + 1)); |
| 245 | lambda = matchMap.get(match); |
| 246 | if (lambda == null) { |
| 247 | // TODO : double check behavior when bitset is full |
| 248 | // Linc lambdas start at 1. |
| 249 | lambda = channels.nextClearBit(1); |
| 250 | channels.set(lambda); |
| 251 | portChannelMap.put(port, channels); |
| 252 | matchMap.put(match, lambda); |
| 253 | } |
| 254 | } |
| 255 | return lambda; |
| 256 | } |
| 257 | |
| 258 | // free lambda that was mapped to Port/Match combination and return its |
| 259 | // value to caller. |
| 260 | private int freeLambda(OFPort port, Match match) { |
| 261 | synchronized (this) { |
| 262 | Integer lambda = matchMap.get(match); |
| 263 | if (lambda != null) { |
| 264 | portChannelMap.get(port).clear(lambda); |
| 265 | return lambda; |
| 266 | } |
| 267 | // 1 is a sane-ish default for Linc. |
| 268 | return 1; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // build matches - *tons of assumptions are made here based on Linc-OE's behavior.* |
| 273 | // gridType = 1 (DWDM) |
| 274 | // channelSpacing = 2 (50GHz) |
| 275 | // spectralWidth = 1 (fixed grid default value) |
| 276 | private Match buildMatch(Match original, CircuitSignalID sigid) { |
| 277 | Match.Builder mBuilder = factory.buildMatch(); |
| 278 | |
| 279 | original.getMatchFields().forEach(mf -> { |
| 280 | String name = mf.getName(); |
| 281 | if (MatchField.OCH_SIGID.getName().equals(name)) { |
| 282 | mBuilder.setExact(MatchField.OCH_SIGID, sigid); |
| 283 | } else if (MatchField.OCH_SIGTYPE.getName().equals(name)) { |
| 284 | mBuilder.setExact(MatchField.OCH_SIGTYPE, U8.of((short) 1)); |
| 285 | } else if (MatchField.IN_PORT.getName().equals(name)) { |
| 286 | mBuilder.setExact(MatchField.IN_PORT, original.get(MatchField.IN_PORT)); |
| 287 | } |
| 288 | }); |
| 289 | |
| 290 | return mBuilder.build(); |
| 291 | } |
| 292 | |
| 293 | private List<OFAction> buildActions(List<OFInstruction> iList, CircuitSignalID sigid) { |
| 294 | List<OFAction> actions = new ArrayList<>(); |
| 295 | Map<OFInstructionType, OFInstruction> instructions = iList.stream() |
| 296 | .collect(Collectors.toMap(OFInstruction::getType, inst -> inst)); |
| 297 | |
| 298 | OFInstruction inst = instructions.get(OFInstructionType.APPLY_ACTIONS); |
| 299 | if (inst != null) { |
| 300 | OFInstructionApplyActions iaa = (OFInstructionApplyActions) inst; |
| 301 | if (iaa.getActions() == null) { |
| 302 | return actions; |
| 303 | } |
| 304 | iaa.getActions().forEach(action -> { |
| 305 | if (OFActionType.EXPERIMENTER == action.getType()) { |
| 306 | OFActionCircuit.Builder cBuilder = factory.actions().buildCircuit() |
| 307 | .setField(factory.oxms() |
| 308 | .buildOchSigid() |
| 309 | .setValue(sigid) |
| 310 | .build()); |
| 311 | actions.add(cBuilder.build()); |
| 312 | } else { |
| 313 | actions.add(action); |
| 314 | } |
| 315 | }); |
| 316 | } |
| 317 | return actions; |
| 318 | } |
| 319 | |
| 320 | private OFMessage buildFlowMod(Builder builder, OFFlowMod fm, Match m, List<OFAction> act) { |
| 321 | return builder |
| 322 | .setXid(fm.getXid()) |
| 323 | .setCookie(fm.getCookie()) |
| 324 | .setCookieMask(fm.getCookieMask()) |
| 325 | .setTableId(fm.getTableId()) |
| 326 | .setIdleTimeout(fm.getIdleTimeout()) |
| 327 | .setHardTimeout(fm.getHardTimeout()) |
| 328 | .setBufferId(fm.getBufferId()) |
| 329 | .setOutPort(fm.getOutPort()) |
| 330 | .setOutGroup(fm.getOutGroup()) |
| 331 | .setFlags(fm.getFlags()) |
| 332 | .setMatch(m) |
| 333 | .setActions(act) |
| 334 | .build(); |
| 335 | } |
Praseed Balakrishnan | e48aa68 | 2014-10-08 17:31:37 -0700 | [diff] [blame] | 336 | } |