alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 1 | package org.onlab.onos.of.controller.impl; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 2 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.List; |
| 6 | import java.util.Set; |
| 7 | import java.util.concurrent.ConcurrentHashMap; |
| 8 | import java.util.concurrent.locks.Lock; |
| 9 | import java.util.concurrent.locks.ReentrantLock; |
| 10 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
tom | 6901237 | 2014-09-08 01:58:45 -0700 | [diff] [blame] | 12 | import org.apache.felix.scr.annotations.Component; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 13 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 6901237 | 2014-09-08 01:58:45 -0700 | [diff] [blame] | 14 | import org.apache.felix.scr.annotations.Service; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 15 | import org.onlab.onos.of.controller.DefaultPacketContext; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 16 | import org.onlab.onos.of.controller.Dpid; |
| 17 | import org.onlab.onos.of.controller.OpenFlowController; |
| 18 | import org.onlab.onos.of.controller.OpenFlowSwitch; |
| 19 | import org.onlab.onos.of.controller.OpenFlowSwitchListener; |
| 20 | import org.onlab.onos.of.controller.PacketListener; |
| 21 | import org.onlab.onos.of.controller.RoleState; |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 22 | import org.onlab.onos.of.controller.driver.OpenFlowAgent; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 23 | import org.projectfloodlight.openflow.protocol.OFMessage; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 24 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
tom | 6901237 | 2014-09-08 01:58:45 -0700 | [diff] [blame] | 29 | @Component(immediate = true) |
| 30 | @Service |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 31 | public class OpenFlowControllerImpl implements OpenFlowController { |
| 32 | |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame] | 33 | private static final Logger log = |
| 34 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 35 | |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 36 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
| 37 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 38 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
| 39 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| 40 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
| 41 | new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 42 | |
| 43 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 44 | protected Set<OpenFlowSwitchListener> ofEventListener = |
| 45 | new HashSet<>(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 46 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 47 | protected List<PacketListener> ofPacketListener = |
| 48 | new ArrayList<>(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 49 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 50 | private final Controller ctrl = new Controller(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 51 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 52 | @Activate |
| 53 | public void activate() { |
| 54 | ctrl.start(agent); |
| 55 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 56 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 57 | @Deactivate |
| 58 | public void deactivate() { |
| 59 | ctrl.stop(); |
| 60 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 61 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 64 | return connectedSwitches.values(); |
| 65 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 66 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 67 | @Override |
| 68 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 69 | return activeMasterSwitches.values(); |
| 70 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 71 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 72 | @Override |
| 73 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 74 | return activeEqualSwitches.values(); |
| 75 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 76 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 79 | return connectedSwitches.get(dpid); |
| 80 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 81 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 84 | return activeMasterSwitches.get(dpid); |
| 85 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 86 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 89 | return activeEqualSwitches.get(dpid); |
| 90 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 91 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public void addListener(OpenFlowSwitchListener listener) { |
| 94 | if (!ofEventListener.contains(listener)) { |
| 95 | this.ofEventListener.add(listener); |
| 96 | } |
| 97 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 98 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void removeListener(OpenFlowSwitchListener listener) { |
| 101 | this.ofEventListener.remove(listener); |
| 102 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 103 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public void addPacketListener(int priority, PacketListener listener) { |
| 106 | ofPacketListener.add(priority, listener); |
| 107 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 108 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public void removePacketListener(PacketListener listener) { |
| 111 | ofPacketListener.remove(listener); |
| 112 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 113 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public void write(Dpid dpid, OFMessage msg) { |
| 116 | this.getSwitch(dpid).sendMsg(msg); |
| 117 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 118 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void processPacket(Dpid dpid, OFMessage msg) { |
| 121 | switch (msg.getType()) { |
| 122 | case PORT_STATUS: |
| 123 | for (OpenFlowSwitchListener l : ofEventListener) { |
| 124 | l.portChanged(dpid, (OFPortStatus) msg); |
| 125 | } |
| 126 | break; |
| 127 | case PACKET_IN: |
| 128 | for (PacketListener p : ofPacketListener) { |
| 129 | //TODO fix me! |
| 130 | p.handlePacket(DefaultPacketContext |
| 131 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 132 | (OFPacketIn) msg)); |
| 133 | } |
| 134 | break; |
| 135 | default: |
| 136 | log.warn("Handling message type {} not yet implemented {}", |
| 137 | msg.getType(), msg); |
| 138 | } |
| 139 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 140 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public void setRole(Dpid dpid, RoleState role) { |
| 143 | getSwitch(dpid).setRole(role); |
| 144 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 145 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Implementation of an OpenFlow Agent which is responsible for |
| 148 | * keeping track of connected switches and the state in which |
| 149 | * they are. |
| 150 | */ |
| 151 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 152 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 153 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 154 | private final Lock switchLock = new ReentrantLock(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 155 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 158 | if (connectedSwitches.get(dpid) != null) { |
| 159 | log.error("Trying to add connectedSwitch but found a previous " |
| 160 | + "value for dpid: {}", dpid); |
| 161 | return false; |
| 162 | } else { |
| 163 | log.error("Added switch {}", dpid); |
| 164 | connectedSwitches.put(dpid, sw); |
| 165 | for (OpenFlowSwitchListener l : ofEventListener) { |
| 166 | l.switchAdded(dpid); |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 171 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public boolean validActivation(Dpid dpid) { |
| 174 | if (connectedSwitches.get(dpid) == null) { |
| 175 | log.error("Trying to activate switch but is not in " |
| 176 | + "connected switches: dpid {}. Aborting ..", |
| 177 | dpid); |
| 178 | return false; |
| 179 | } |
| 180 | if (activeMasterSwitches.get(dpid) != null || |
| 181 | activeEqualSwitches.get(dpid) != null) { |
| 182 | log.error("Trying to activate switch but it is already " |
| 183 | + "activated: dpid {}. Found in activeMaster: {} " |
| 184 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 185 | dpid, |
| 186 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 187 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 192 | |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 193 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 196 | switchLock.lock(); |
| 197 | try { |
| 198 | if (!validActivation(dpid)) { |
| 199 | return false; |
| 200 | } |
| 201 | activeMasterSwitches.put(dpid, sw); |
| 202 | return true; |
| 203 | } finally { |
| 204 | switchLock.unlock(); |
| 205 | } |
| 206 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 207 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 208 | @Override |
| 209 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 210 | switchLock.lock(); |
| 211 | try { |
| 212 | if (!validActivation(dpid)) { |
| 213 | return false; |
| 214 | } |
| 215 | activeEqualSwitches.put(dpid, sw); |
| 216 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 217 | return true; |
| 218 | } finally { |
| 219 | switchLock.unlock(); |
| 220 | } |
| 221 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 222 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 223 | @Override |
| 224 | public void transitionToMasterSwitch(Dpid dpid) { |
| 225 | switchLock.lock(); |
| 226 | try { |
| 227 | if (activeMasterSwitches.containsKey(dpid)) { |
| 228 | return; |
| 229 | } |
| 230 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 231 | if (sw == null) { |
| 232 | sw = getSwitch(dpid); |
| 233 | if (sw == null) { |
| 234 | log.error("Transition to master called on sw {}, but switch " |
| 235 | + "was not found in controller-cache", dpid); |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | log.info("Transitioned switch {} to MASTER", dpid); |
| 240 | activeMasterSwitches.put(dpid, sw); |
| 241 | } finally { |
| 242 | switchLock.unlock(); |
| 243 | } |
| 244 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 245 | |
| 246 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public void transitionToEqualSwitch(Dpid dpid) { |
| 249 | switchLock.lock(); |
| 250 | try { |
| 251 | if (activeEqualSwitches.containsKey(dpid)) { |
| 252 | return; |
| 253 | } |
| 254 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 255 | if (sw == null) { |
| 256 | log.error("Transition to equal called on sw {}, but switch " |
| 257 | + "was not found in controller-cache", dpid); |
| 258 | return; |
| 259 | } |
| 260 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 261 | activeEqualSwitches.put(dpid, sw); |
| 262 | } finally { |
| 263 | switchLock.unlock(); |
| 264 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 265 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 266 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 267 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 268 | @Override |
| 269 | public void removeConnectedSwitch(Dpid dpid) { |
| 270 | connectedSwitches.remove(dpid); |
| 271 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 272 | if (sw == null) { |
| 273 | sw = activeEqualSwitches.remove(dpid); |
| 274 | } |
| 275 | for (OpenFlowSwitchListener l : ofEventListener) { |
| 276 | l.switchRemoved(dpid); |
| 277 | } |
| 278 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 279 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 280 | @Override |
| 281 | public void processMessage(Dpid dpid, OFMessage m) { |
| 282 | processPacket(dpid, m); |
| 283 | } |
| 284 | } |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 285 | |
| 286 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 287 | } |