tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 2 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 3 | import static org.onlab.util.Tools.namedThreads; |
| 4 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 5 | import java.util.HashSet; |
| 6 | import java.util.Set; |
| 7 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 8 | import java.util.concurrent.ExecutorService; |
| 9 | import java.util.concurrent.Executors; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 10 | import java.util.concurrent.locks.Lock; |
| 11 | import java.util.concurrent.locks.ReentrantLock; |
| 12 | |
| 13 | import org.apache.felix.scr.annotations.Activate; |
| 14 | import org.apache.felix.scr.annotations.Component; |
| 15 | import org.apache.felix.scr.annotations.Deactivate; |
| 16 | import org.apache.felix.scr.annotations.Service; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 17 | import org.onlab.onos.openflow.controller.DefaultOpenFlowPacketContext; |
| 18 | import org.onlab.onos.openflow.controller.Dpid; |
| 19 | import org.onlab.onos.openflow.controller.OpenFlowController; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 20 | import org.onlab.onos.openflow.controller.OpenFlowEventListener; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 21 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
| 22 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 23 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 24 | import org.onlab.onos.openflow.controller.PacketListener; |
| 25 | import org.onlab.onos.openflow.controller.RoleState; |
| 26 | import org.onlab.onos.openflow.controller.driver.OpenFlowAgent; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 28 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 29 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import com.google.common.collect.ArrayListMultimap; |
| 34 | import com.google.common.collect.Multimap; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 35 | import com.google.common.collect.Sets; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 36 | |
| 37 | @Component(immediate = true) |
| 38 | @Service |
| 39 | public class OpenFlowControllerImpl implements OpenFlowController { |
| 40 | |
| 41 | private static final Logger log = |
| 42 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 43 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 44 | private final ExecutorService executor = Executors.newFixedThreadPool(16, |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 45 | namedThreads("of-event-%d")); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 46 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 47 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
| 48 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 49 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
| 50 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 51 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
| 52 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 53 | |
| 54 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 55 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new HashSet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 56 | |
| 57 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 58 | ArrayListMultimap.create(); |
| 59 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 60 | protected Set<OpenFlowEventListener> ofEventListener = Sets.newHashSet(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 61 | |
| 62 | private final Controller ctrl = new Controller(); |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | ctrl.start(agent); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | ctrl.stop(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 76 | return connectedSwitches.values(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 81 | return activeMasterSwitches.values(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 86 | return activeEqualSwitches.values(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 91 | return connectedSwitches.get(dpid); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 96 | return activeMasterSwitches.get(dpid); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 101 | return activeEqualSwitches.get(dpid); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 106 | if (!ofSwitchListener.contains(listener)) { |
| 107 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 113 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void addPacketListener(int priority, PacketListener listener) { |
| 118 | ofPacketListener.put(priority, listener); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void removePacketListener(PacketListener listener) { |
| 123 | ofPacketListener.values().remove(listener); |
| 124 | } |
| 125 | |
| 126 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 127 | public void addEventListener(OpenFlowEventListener listener) { |
| 128 | ofEventListener.add(listener); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void removeEventListener(OpenFlowEventListener listener) { |
| 133 | ofEventListener.remove(listener); |
| 134 | } |
| 135 | |
| 136 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 137 | public void write(Dpid dpid, OFMessage msg) { |
| 138 | this.getSwitch(dpid).sendMsg(msg); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void processPacket(Dpid dpid, OFMessage msg) { |
| 143 | switch (msg.getType()) { |
| 144 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 145 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 146 | l.portChanged(dpid, (OFPortStatus) msg); |
| 147 | } |
| 148 | break; |
| 149 | case PACKET_IN: |
| 150 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 151 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 152 | (OFPacketIn) msg); |
| 153 | for (PacketListener p : ofPacketListener.values()) { |
| 154 | p.handlePacket(pktCtx); |
| 155 | } |
| 156 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 157 | case FLOW_REMOVED: |
| 158 | case ERROR: |
| 159 | case STATS_REPLY: |
| 160 | case BARRIER_REPLY: |
| 161 | executor.submit(new OFMessageHandler(dpid, msg)); |
| 162 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 163 | default: |
| 164 | log.warn("Handling message type {} not yet implemented {}", |
| 165 | msg.getType(), msg); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 171 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 172 | if (sw == null) { |
| 173 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 174 | return; |
| 175 | } |
| 176 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Implementation of an OpenFlow Agent which is responsible for |
| 181 | * keeping track of connected switches and the state in which |
| 182 | * they are. |
| 183 | */ |
| 184 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 185 | |
| 186 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 187 | private final Lock switchLock = new ReentrantLock(); |
| 188 | |
| 189 | @Override |
| 190 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 191 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 192 | if (connectedSwitches.get(dpid) != null) { |
| 193 | log.error("Trying to add connectedSwitch but found a previous " |
| 194 | + "value for dpid: {}", dpid); |
| 195 | return false; |
| 196 | } else { |
| 197 | log.error("Added switch {}", dpid); |
| 198 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 199 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 200 | l.switchAdded(dpid); |
| 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public boolean validActivation(Dpid dpid) { |
| 208 | if (connectedSwitches.get(dpid) == null) { |
| 209 | log.error("Trying to activate switch but is not in " |
| 210 | + "connected switches: dpid {}. Aborting ..", |
| 211 | dpid); |
| 212 | return false; |
| 213 | } |
| 214 | if (activeMasterSwitches.get(dpid) != null || |
| 215 | activeEqualSwitches.get(dpid) != null) { |
| 216 | log.error("Trying to activate switch but it is already " |
| 217 | + "activated: dpid {}. Found in activeMaster: {} " |
| 218 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 219 | dpid, |
| 220 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 221 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 222 | return false; |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | @Override |
| 229 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 230 | switchLock.lock(); |
| 231 | try { |
| 232 | if (!validActivation(dpid)) { |
| 233 | return false; |
| 234 | } |
| 235 | activeMasterSwitches.put(dpid, sw); |
| 236 | return true; |
| 237 | } finally { |
| 238 | switchLock.unlock(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 244 | switchLock.lock(); |
| 245 | try { |
| 246 | if (!validActivation(dpid)) { |
| 247 | return false; |
| 248 | } |
| 249 | activeEqualSwitches.put(dpid, sw); |
| 250 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 251 | return true; |
| 252 | } finally { |
| 253 | switchLock.unlock(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void transitionToMasterSwitch(Dpid dpid) { |
| 259 | switchLock.lock(); |
| 260 | try { |
| 261 | if (activeMasterSwitches.containsKey(dpid)) { |
| 262 | return; |
| 263 | } |
| 264 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 265 | if (sw == null) { |
| 266 | sw = getSwitch(dpid); |
| 267 | if (sw == null) { |
| 268 | log.error("Transition to master called on sw {}, but switch " |
| 269 | + "was not found in controller-cache", dpid); |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | log.info("Transitioned switch {} to MASTER", dpid); |
| 274 | activeMasterSwitches.put(dpid, sw); |
| 275 | } finally { |
| 276 | switchLock.unlock(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | @Override |
| 282 | public void transitionToEqualSwitch(Dpid dpid) { |
| 283 | switchLock.lock(); |
| 284 | try { |
| 285 | if (activeEqualSwitches.containsKey(dpid)) { |
| 286 | return; |
| 287 | } |
| 288 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 289 | if (sw == null) { |
| 290 | sw = getSwitch(dpid); |
| 291 | if (sw == null) { |
| 292 | log.error("Transition to equal called on sw {}, but switch " |
| 293 | + "was not found in controller-cache", dpid); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 298 | activeEqualSwitches.put(dpid, sw); |
| 299 | } finally { |
| 300 | switchLock.unlock(); |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void removeConnectedSwitch(Dpid dpid) { |
| 307 | connectedSwitches.remove(dpid); |
| 308 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 309 | if (sw == null) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 310 | log.warn("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 311 | sw = activeEqualSwitches.remove(dpid); |
| 312 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 313 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame] | 314 | log.warn("removal for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 315 | l.switchRemoved(dpid); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void processMessage(Dpid dpid, OFMessage m) { |
| 321 | processPacket(dpid, m); |
| 322 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 323 | |
| 324 | @Override |
| 325 | public void returnRoleAssertFailed(Dpid dpid, RoleState role) { |
| 326 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 327 | l.roleAssertFailed(dpid, role); |
| 328 | } |
| 329 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 330 | } |
| 331 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 332 | private final class OFMessageHandler implements Runnable { |
| 333 | |
| 334 | private final OFMessage msg; |
| 335 | private final Dpid dpid; |
| 336 | |
| 337 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 338 | this.msg = msg; |
| 339 | this.dpid = dpid; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 344 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 345 | listener.handleMessage(dpid, msg); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | } |
| 350 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 351 | } |