Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame^] | 20 | import com.google.common.collect.Multimap; |
| 21 | import com.google.common.collect.Sets; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 27 | import org.onosproject.openflow.controller.Dpid; |
| 28 | import org.onosproject.openflow.controller.OpenFlowController; |
| 29 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 30 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 31 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 32 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 33 | import org.onosproject.openflow.controller.PacketListener; |
| 34 | import org.onosproject.openflow.controller.RoleState; |
| 35 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 37 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 39 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 40 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 42 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 43 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 44 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 46 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame^] | 54 | import java.util.Collection; |
| 55 | import java.util.HashSet; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.ConcurrentHashMap; |
| 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
| 60 | import java.util.concurrent.locks.Lock; |
| 61 | import java.util.concurrent.locks.ReentrantLock; |
| 62 | |
| 63 | import static org.onlab.util.Tools.groupedThreads; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | |
| 65 | @Component(immediate = true) |
| 66 | @Service |
| 67 | public class OpenFlowControllerImpl implements OpenFlowController { |
| 68 | |
| 69 | private static final Logger log = |
| 70 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 71 | |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 72 | private final ExecutorService executorMsgs = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame^] | 73 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d")); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 74 | |
| 75 | private final ExecutorService executorBarrier = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame^] | 76 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d")); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 77 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 78 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
| 79 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 80 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
| 81 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 82 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
| 83 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 84 | |
| 85 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 86 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new HashSet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 87 | |
| 88 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 89 | ArrayListMultimap.create(); |
| 90 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 91 | protected Set<OpenFlowEventListener> ofEventListener = Sets.newHashSet(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 92 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 93 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 94 | ArrayListMultimap.create(); |
| 95 | |
| 96 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 97 | ArrayListMultimap.create(); |
| 98 | |
| 99 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 100 | ArrayListMultimap.create(); |
| 101 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 102 | private final Controller ctrl = new Controller(); |
| 103 | |
| 104 | @Activate |
| 105 | public void activate() { |
| 106 | ctrl.start(agent); |
| 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
| 111 | ctrl.stop(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 116 | return connectedSwitches.values(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 121 | return activeMasterSwitches.values(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 126 | return activeEqualSwitches.values(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 131 | return connectedSwitches.get(dpid); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 136 | return activeMasterSwitches.get(dpid); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 141 | return activeEqualSwitches.get(dpid); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 146 | if (!ofSwitchListener.contains(listener)) { |
| 147 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 153 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void addPacketListener(int priority, PacketListener listener) { |
| 158 | ofPacketListener.put(priority, listener); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void removePacketListener(PacketListener listener) { |
| 163 | ofPacketListener.values().remove(listener); |
| 164 | } |
| 165 | |
| 166 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 167 | public void addEventListener(OpenFlowEventListener listener) { |
| 168 | ofEventListener.add(listener); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void removeEventListener(OpenFlowEventListener listener) { |
| 173 | ofEventListener.remove(listener); |
| 174 | } |
| 175 | |
| 176 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 177 | public void write(Dpid dpid, OFMessage msg) { |
| 178 | this.getSwitch(dpid).sendMsg(msg); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 183 | Collection<OFFlowStatsEntry> flowStats; |
| 184 | Collection<OFGroupStatsEntry> groupStats; |
| 185 | Collection<OFGroupDescStatsEntry> groupDescStats; |
| 186 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 187 | switch (msg.getType()) { |
| 188 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 189 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 190 | l.portChanged(dpid, (OFPortStatus) msg); |
| 191 | } |
| 192 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 193 | case FEATURES_REPLY: |
| 194 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 195 | l.switchChanged(dpid); |
| 196 | } |
| 197 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 198 | case PACKET_IN: |
| 199 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 200 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 201 | (OFPacketIn) msg); |
| 202 | for (PacketListener p : ofPacketListener.values()) { |
| 203 | p.handlePacket(pktCtx); |
| 204 | } |
| 205 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 206 | // TODO: Consider using separate threadpool for sensitive messages. |
| 207 | // ie. Back to back error could cause us to starve. |
| 208 | case FLOW_REMOVED: |
| 209 | case ERROR: |
| 210 | executorMsgs.submit(new OFMessageHandler(dpid, msg)); |
| 211 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 212 | case STATS_REPLY: |
| 213 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 214 | switch (reply.getStatsType()) { |
| 215 | case PORT_DESC: |
| 216 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 217 | l.switchChanged(dpid); |
| 218 | } |
| 219 | break; |
| 220 | case FLOW: |
| 221 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 222 | if (flowStats != null) { |
| 223 | OFFlowStatsReply.Builder rep = |
| 224 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 225 | rep.setEntries(Lists.newLinkedList(flowStats)); |
| 226 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 227 | } |
| 228 | break; |
| 229 | case GROUP: |
| 230 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 231 | if (groupStats != null) { |
| 232 | OFGroupStatsReply.Builder rep = |
| 233 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 234 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 235 | rep.setXid(reply.getXid()); |
| 236 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 237 | } |
| 238 | break; |
| 239 | case GROUP_DESC: |
| 240 | groupDescStats = publishGroupDescStats(dpid, |
| 241 | (OFGroupDescStatsReply) reply); |
| 242 | if (groupDescStats != null) { |
| 243 | OFGroupDescStatsReply.Builder rep = |
| 244 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
| 245 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 246 | rep.setXid(reply.getXid()); |
| 247 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 248 | } |
| 249 | break; |
| 250 | default: |
| 251 | log.warn("Unsupported stats type : {}", reply.getStatsType()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 252 | } |
| 253 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 254 | case BARRIER_REPLY: |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 255 | executorBarrier.submit(new OFMessageHandler(dpid, msg)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 256 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 257 | case EXPERIMENTER: |
| 258 | // Handle optical port stats |
| 259 | if (((OFExperimenter) msg).getExperimenter() == 0x748771) { |
| 260 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
| 261 | OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus(); |
| 262 | OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc(); |
| 263 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 264 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 265 | .setName(circuitPortStatus.getName()) |
| 266 | .setConfig(circuitPortStatus.getConfig()) |
| 267 | .setState(circuitPortStatus.getState()); |
| 268 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 269 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 270 | l.portChanged(dpid, portStatus.build()); |
| 271 | } |
| 272 | } else { |
| 273 | log.warn("Handling experimenter type {} not yet implemented", |
| 274 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 275 | } |
| 276 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 277 | default: |
| 278 | log.warn("Handling message type {} not yet implemented {}", |
| 279 | msg.getType(), msg); |
| 280 | } |
| 281 | } |
| 282 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 283 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 284 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 285 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 286 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 287 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 288 | return fullFlowStats.removeAll(dpid); |
| 289 | } |
| 290 | return null; |
| 291 | } |
| 292 | |
| 293 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 294 | OFGroupStatsReply reply) { |
| 295 | //TODO: Get rid of synchronized |
| 296 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 297 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 298 | return fullGroupStats.removeAll(dpid); |
| 299 | } |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 304 | OFGroupDescStatsReply reply) { |
| 305 | //TODO: Get rid of synchronized |
| 306 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 307 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 308 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 309 | } |
| 310 | return null; |
| 311 | } |
| 312 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 313 | @Override |
| 314 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 315 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 316 | if (sw == null) { |
| 317 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 318 | return; |
| 319 | } |
| 320 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Implementation of an OpenFlow Agent which is responsible for |
| 325 | * keeping track of connected switches and the state in which |
| 326 | * they are. |
| 327 | */ |
| 328 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 329 | |
| 330 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 331 | private final Lock switchLock = new ReentrantLock(); |
| 332 | |
| 333 | @Override |
| 334 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 335 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 336 | if (connectedSwitches.get(dpid) != null) { |
| 337 | log.error("Trying to add connectedSwitch but found a previous " |
| 338 | + "value for dpid: {}", dpid); |
| 339 | return false; |
| 340 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 341 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 342 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 343 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 344 | l.switchAdded(dpid); |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public boolean validActivation(Dpid dpid) { |
| 352 | if (connectedSwitches.get(dpid) == null) { |
| 353 | log.error("Trying to activate switch but is not in " |
| 354 | + "connected switches: dpid {}. Aborting ..", |
| 355 | dpid); |
| 356 | return false; |
| 357 | } |
| 358 | if (activeMasterSwitches.get(dpid) != null || |
| 359 | activeEqualSwitches.get(dpid) != null) { |
| 360 | log.error("Trying to activate switch but it is already " |
| 361 | + "activated: dpid {}. Found in activeMaster: {} " |
| 362 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 363 | dpid, |
| 364 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 365 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 366 | return false; |
| 367 | } |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | @Override |
| 373 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 374 | switchLock.lock(); |
| 375 | try { |
| 376 | if (!validActivation(dpid)) { |
| 377 | return false; |
| 378 | } |
| 379 | activeMasterSwitches.put(dpid, sw); |
| 380 | return true; |
| 381 | } finally { |
| 382 | switchLock.unlock(); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 388 | switchLock.lock(); |
| 389 | try { |
| 390 | if (!validActivation(dpid)) { |
| 391 | return false; |
| 392 | } |
| 393 | activeEqualSwitches.put(dpid, sw); |
| 394 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 395 | return true; |
| 396 | } finally { |
| 397 | switchLock.unlock(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void transitionToMasterSwitch(Dpid dpid) { |
| 403 | switchLock.lock(); |
| 404 | try { |
| 405 | if (activeMasterSwitches.containsKey(dpid)) { |
| 406 | return; |
| 407 | } |
| 408 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 409 | if (sw == null) { |
| 410 | sw = getSwitch(dpid); |
| 411 | if (sw == null) { |
| 412 | log.error("Transition to master called on sw {}, but switch " |
| 413 | + "was not found in controller-cache", dpid); |
| 414 | return; |
| 415 | } |
| 416 | } |
| 417 | log.info("Transitioned switch {} to MASTER", dpid); |
| 418 | activeMasterSwitches.put(dpid, sw); |
| 419 | } finally { |
| 420 | switchLock.unlock(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | |
| 425 | @Override |
| 426 | public void transitionToEqualSwitch(Dpid dpid) { |
| 427 | switchLock.lock(); |
| 428 | try { |
| 429 | if (activeEqualSwitches.containsKey(dpid)) { |
| 430 | return; |
| 431 | } |
| 432 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 433 | if (sw == null) { |
| 434 | sw = getSwitch(dpid); |
| 435 | if (sw == null) { |
| 436 | log.error("Transition to equal called on sw {}, but switch " |
| 437 | + "was not found in controller-cache", dpid); |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 442 | activeEqualSwitches.put(dpid, sw); |
| 443 | } finally { |
| 444 | switchLock.unlock(); |
| 445 | } |
| 446 | |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public void removeConnectedSwitch(Dpid dpid) { |
| 451 | connectedSwitches.remove(dpid); |
| 452 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 453 | if (sw == null) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 454 | log.warn("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 455 | sw = activeEqualSwitches.remove(dpid); |
| 456 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 457 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 458 | log.warn("removal for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 459 | l.switchRemoved(dpid); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void processMessage(Dpid dpid, OFMessage m) { |
| 465 | processPacket(dpid, m); |
| 466 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 467 | |
| 468 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 469 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 470 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 471 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 476 | private final class OFMessageHandler implements Runnable { |
| 477 | |
| 478 | private final OFMessage msg; |
| 479 | private final Dpid dpid; |
| 480 | |
| 481 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 482 | this.msg = msg; |
| 483 | this.dpid = dpid; |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 488 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 489 | listener.handleMessage(dpid, msg); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | } |
| 494 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 495 | } |