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