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