| package org.onlab.onos.of.controller.impl.internal; |
| |
| import java.util.ArrayList; |
| import java.util.concurrent.ConcurrentHashMap; |
| import java.util.concurrent.locks.Lock; |
| import java.util.concurrent.locks.ReentrantLock; |
| |
| import org.apache.felix.scr.annotations.Activate; |
| import org.apache.felix.scr.annotations.Component; |
| import org.apache.felix.scr.annotations.Deactivate; |
| import org.apache.felix.scr.annotations.Service; |
| import org.onlab.onos.of.controller.Dpid; |
| import org.onlab.onos.of.controller.OpenFlowController; |
| import org.onlab.onos.of.controller.OpenFlowSwitch; |
| import org.onlab.onos.of.controller.OpenFlowSwitchListener; |
| import org.onlab.onos.of.controller.PacketListener; |
| import org.onlab.onos.of.controller.RoleState; |
| import org.onlab.onos.of.controller.driver.AbstractOpenFlowSwitch; |
| import org.onlab.onos.of.controller.driver.OpenFlowAgent; |
| import org.projectfloodlight.openflow.protocol.OFMessage; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| @Component(immediate = true) |
| @Service |
| public class OpenFlowControllerImpl implements OpenFlowController { |
| |
| private static final Logger log = |
| LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| |
| protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
| new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
| new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
| new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); |
| |
| protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
| protected ArrayList<OpenFlowSwitchListener> ofEventListener = |
| new ArrayList<OpenFlowSwitchListener>(); |
| |
| private final Controller ctrl = new Controller(); |
| |
| @Activate |
| public void activate() { |
| ctrl.start(agent); |
| } |
| |
| @Deactivate |
| public void deactivate() { |
| ctrl.stop(); |
| } |
| |
| @Override |
| public Iterable<OpenFlowSwitch> getSwitches() { |
| return connectedSwitches.values(); |
| } |
| |
| @Override |
| public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| return activeMasterSwitches.values(); |
| } |
| |
| @Override |
| public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| return activeEqualSwitches.values(); |
| } |
| |
| @Override |
| public OpenFlowSwitch getSwitch(Dpid dpid) { |
| return connectedSwitches.get(dpid.value()); |
| } |
| |
| @Override |
| public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| return activeMasterSwitches.get(dpid.value()); |
| } |
| |
| @Override |
| public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| return activeEqualSwitches.get(dpid.value()); } |
| |
| @Override |
| public void addListener(OpenFlowSwitchListener listener) { |
| if (!ofEventListener.contains(listener)) { |
| this.ofEventListener.add(listener); |
| } |
| } |
| |
| @Override |
| public void removeListener(OpenFlowSwitchListener listener) { |
| this.ofEventListener.remove(listener); |
| } |
| |
| @Override |
| public void addPacketListener(int priority, PacketListener listener) { |
| // TODO Auto-generated method stub |
| |
| } |
| |
| @Override |
| public void removePacketListener(PacketListener listener) { |
| // TODO Auto-generated method stub |
| |
| } |
| |
| @Override |
| public void write(Dpid dpid, OFMessage msg) { |
| this.getSwitch(dpid).sendMsg(msg); |
| } |
| |
| @Override |
| public void processPacket(OFMessage msg) { |
| log.info("Got message {}", msg); |
| } |
| |
| @Override |
| public void setRole(Dpid dpid, RoleState role) { |
| ((AbstractOpenFlowSwitch) getSwitch(dpid)).setRole(role); |
| } |
| |
| public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| |
| private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| private final Lock switchLock = new ReentrantLock(); |
| |
| @Override |
| public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| if (connectedSwitches.get(dpid) != null) { |
| log.error("Trying to add connectedSwitch but found a previous " |
| + "value for dpid: {}", dpid); |
| return false; |
| } else { |
| log.error("Added switch {}", dpid); |
| connectedSwitches.put(dpid, sw); |
| for (OpenFlowSwitchListener l : ofEventListener) { |
| l.switchAdded(dpid); |
| } |
| return true; |
| } |
| } |
| |
| @Override |
| public boolean validActivation(Dpid dpid) { |
| if (connectedSwitches.get(dpid) == null) { |
| log.error("Trying to activate switch but is not in " |
| + "connected switches: dpid {}. Aborting ..", |
| dpid); |
| return false; |
| } |
| if (activeMasterSwitches.get(dpid) != null || |
| activeEqualSwitches.get(dpid) != null) { |
| log.error("Trying to activate switch but it is already " |
| + "activated: dpid {}. Found in activeMaster: {} " |
| + "Found in activeEqual: {}. Aborting ..", new Object[] { |
| dpid, |
| (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| return false; |
| } |
| return true; |
| } |
| |
| |
| @Override |
| public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| switchLock.lock(); |
| try { |
| if (!validActivation(dpid)) { |
| return false; |
| } |
| activeMasterSwitches.put(dpid, sw); |
| return true; |
| } finally { |
| switchLock.unlock(); |
| } |
| } |
| |
| @Override |
| public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| switchLock.lock(); |
| try { |
| if (!validActivation(dpid)) { |
| return false; |
| } |
| activeEqualSwitches.put(dpid, sw); |
| log.info("Added Activated EQUAL Switch {}", dpid); |
| return true; |
| } finally { |
| switchLock.unlock(); |
| } |
| } |
| |
| @Override |
| public void transitionToMasterSwitch(Dpid dpid) { |
| switchLock.lock(); |
| try { |
| if (activeMasterSwitches.containsKey(dpid)) { |
| return; |
| } |
| OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| if (sw == null) { |
| log.error("Transition to master called on sw {}, but switch " |
| + "was not found in controller-cache", dpid); |
| return; |
| } |
| activeMasterSwitches.put(dpid, sw); |
| } finally { |
| switchLock.unlock(); |
| } |
| } |
| |
| |
| @Override |
| public void transitionToEqualSwitch(Dpid dpid) { |
| switchLock.lock(); |
| try { |
| if (activeEqualSwitches.containsKey(dpid)) { |
| return; |
| } |
| OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| if (sw == null) { |
| log.error("Transition to equal called on sw {}, but switch " |
| + "was not found in controller-cache", dpid); |
| return; |
| } |
| activeEqualSwitches.put(dpid, sw); |
| } finally { |
| switchLock.unlock(); |
| } |
| |
| } |
| |
| @Override |
| public void removeConnectedSwitch(Dpid dpid) { |
| connectedSwitches.remove(dpid); |
| OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| if (sw == null) { |
| sw = activeEqualSwitches.remove(dpid); |
| } |
| for (OpenFlowSwitchListener l : ofEventListener) { |
| l.switchRemoved(dpid); |
| } |
| } |
| |
| @Override |
| public void processMessage(OFMessage m) { |
| processPacket(m); |
| } |
| } |
| |
| |
| |
| } |