Merge "Change PlanCalcRuntime class to receive IntentOperationList" into dev/ramcloud-new-datamodel
diff --git a/build-ramcloud-java-bindings.sh b/build-ramcloud-java-bindings.sh
new file mode 100755
index 0000000..7f4dc6a
--- /dev/null
+++ b/build-ramcloud-java-bindings.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+# run this script, when RAMCloud java binding is updated
+
+set -x
+
+ONOS_HOME=~/ONOS
+RAMCLOUD_HOME=~/ramcloud
+
+# create ramcloud lib
+cp -p ${ONOS_HOME}/src/main/java/edu/stanford/ramcloud/JRamCloud.java ${HOME}/ramcloud/bindings/java/edu/stanford/ramcloud/
+cp -p ${ONOS_HOME}/src/main/cpp/edu_stanford_ramcloud_JRamCloud.cc ${HOME}/ramcloud/bindings/java/edu/stanford/ramcloud/
+cd ${HOME}/ramcloud/bindings/java/edu/stanford/ramcloud/
+javac JRamCloud.java
+./build_so.sh
+jar cvf ${RAMCLOUD_HOME}/bindings/java/RamCloud.jar ${RAMCLOUD_HOME}/bindings/java/edu/stanford/ramcloud/*.class
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/PerformanceMonitor.java b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/PerformanceMonitor.java
index 3721ceb..15e5cdc 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/PerformanceMonitor.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/PerformanceMonitor.java
@@ -12,10 +12,26 @@
* Class for collecting performance measurements
*/
public class PerformanceMonitor {
- private final static ConcurrentHashMap<String, Queue<Measurement>> map = new ConcurrentHashMap<>();;
private final static Logger log = LoggerFactory.getLogger(PerformanceMonitor.class);
- private static long overhead;
- private static long experimentStart = Long.MAX_VALUE;
+
+ // experiment name -> PerformanceMonitor
+ private static final ConcurrentHashMap<String,PerformanceMonitor> perfMons = new ConcurrentHashMap<>();
+ public static PerformanceMonitor experiment(String name) {
+ PerformanceMonitor pm = perfMons.get(name);
+ if (pm == null) {
+ pm = new PerformanceMonitor();
+ PerformanceMonitor existing = perfMons.putIfAbsent(name, pm);
+ if (existing != null) {
+ pm = existing;
+ }
+ }
+ return pm;
+ }
+
+ // tag -> Measurements
+ private final ConcurrentHashMap<String, Queue<Measurement>> map = new ConcurrentHashMap<>();
+ private long overhead;
+ private long experimentStart = Long.MAX_VALUE;
private final static double normalization = Math.pow(10, 6);
/**
@@ -26,7 +42,7 @@
*
* @param tag for performance measurement
*/
- public static Measurement start(String tag) {
+ public Measurement startStep(String tag) {
long start = System.nanoTime();
if(start < experimentStart) {
experimentStart = start;
@@ -54,7 +70,7 @@
*
* @param tag for performance measurement
*/
- public static void stop(String tag) {
+ public void stopStep(String tag) {
long time = System.nanoTime();
Queue<Measurement> list = map.get(tag);
if(list == null || list.size() == 0) {
@@ -70,7 +86,7 @@
/**
* Clear all performance measurements.
*/
- public static void clear() {
+ public void reset() {
map.clear();
overhead = 0;
experimentStart = Long.MAX_VALUE;
@@ -79,7 +95,7 @@
/**
* Write all performance measurements to the log
*/
- public static void report() {
+ public void reportAll() {
String result = "Performance Results: (avg/start/stop/count)\n";
if(map.size() == 0) {
result += "No Measurements";
@@ -132,7 +148,7 @@
*
* @param tag the tag name.
*/
- public static void report(String tag) {
+ public void reportStep(String tag) {
Queue<Measurement> list = map.get(tag);
if(list == null) {
return; //TODO
@@ -208,6 +224,34 @@
}
}
+ @Deprecated
+ private static final PerformanceMonitor theInstance = new PerformanceMonitor();
+
+ @Deprecated
+ public static Measurement start(String tag) {
+ return theInstance.startStep(tag);
+ }
+
+ @Deprecated
+ public static void stop(String tag) {
+ theInstance.stopStep(tag);;
+ }
+
+ @Deprecated
+ public static void clear() {
+ theInstance.reset();;
+ }
+
+ @Deprecated
+ public static void report() {
+ theInstance.reportAll();;
+ }
+
+ @Deprecated
+ public static void report(String tag) {
+ theInstance.reportStep(tag);
+ }
+
public static void main(String args[]){
// test the measurement overhead
String tag;
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraph.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraph.java
index 45e4188..58c24ad 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraph.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraph.java
@@ -80,4 +80,17 @@
* @return the network device for the MAC address if found, otherwise null.
*/
public Device getDeviceByMac(MACAddress address);
+
+ /**
+ * Acquire a read lock on the entire topology. The topology will not
+ * change while readers have the lock. Must be released using
+ * {@link releaseLock()}. This method will block until a read lock is
+ * available.
+ */
+ public void acquireLock();
+
+ /**
+ * Release the read lock on the topology.
+ */
+ public void releaseLock();
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
index c7c2011..be0c3c4 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
@@ -43,7 +43,13 @@
this.graph = graph;
}
- public void addSwitch(SwitchEvent sw) {
+ /**
+ * Add a switch to the database.
+ *
+ * @param sw the switch to add.
+ * @return true on success, otherwise false.
+ */
+ public boolean addSwitch(SwitchEvent sw) {
log.debug("Adding switch {}", sw);
ArrayList<WriteOp> groupOp = new ArrayList<>();
@@ -75,10 +81,16 @@
// Conditional operation (Create/Update) then we should retry here.
}
}
-
+ return (! failed);
}
- public void deactivateSwitch(SwitchEvent sw) {
+ /**
+ * Update a switch as inactive in the database.
+ *
+ * @param sw the switch to update.
+ * @return true on success, otherwise false.
+ */
+ public boolean deactivateSwitch(SwitchEvent sw) {
log.debug("Deactivating switch {}", sw);
RCSwitch rcSwitch = new RCSwitch(sw.getDpid());
@@ -100,24 +112,34 @@
log.warn("Trying to deactivate an object that doesn't exist", e);
// We don't care to much if the object wasn't there, it's
// being deactivated anyway
+ return true; // Success: no object in the DB
}
try {
for (RCObject rcObject : objectsToDeactive) {
rcObject.update();
}
- break;
+ return true; // Success
} catch (ObjectDoesntExistException e) {
// Unlikely, and we don't care anyway.
// TODO But, this will cause everything else to fail
log.warn("Trying to deactivate object that doesn't exist", e);
+ return true; // Success: no object in the DB
} catch (WrongVersionException e) {
// Need to re-read and retry
}
}
+
+ return false; // Failure
}
- public void addPort(PortEvent port) {
+ /**
+ * Add a port to the database.
+ *
+ * @param port the port to add.
+ * @return true on success, otherwise false.
+ */
+ public boolean addPort(PortEvent port) {
log.debug("Adding port {}", port);
//RCSwitch rcSwitch = new RCSwitch(sw.getDpid());
@@ -125,7 +147,7 @@
//rcSwitch.read();
//} catch (ObjectDoesntExistException e) {
//log.warn("Add port failed because switch {} doesn't exist", sw.getDpid(), e);
- //return;
+ //return false;
//}
RCPort rcPort = new RCPort(port.getDpid(), port.getNumber());
@@ -134,78 +156,80 @@
//rcPort.setDescription(port.getDescription());
//rcSwitch.addPortId(rcPort.getId());
- writeObject(rcPort);
- //writeObject(rcSwitch);
+ boolean success = writeObject(rcPort);
+ // success &= writeObject(rcSwitch);
+ return success;
}
- public void deactivatePort(PortEvent port) {
+ /**
+ * Update a port as inactive in the database.
+ *
+ * @param port the port to update.
+ * @return true on success, otherwise false.
+ */
+ public boolean deactivatePort(PortEvent port) {
log.debug("Deactivating port {}", port);
RCPort rcPort = new RCPort(port.getDpid(), port.getNumber());
for (int i = 0; i < NUM_RETRIES; i++) {
try {
- rcPort.read();
+ rcPort.read();
} catch (ObjectDoesntExistException e) {
// oh well, we were deactivating anyway
log.warn("Trying to deactivate a port that doesn't exist: {}", port);
- return;
+ return true; // Success: no object in the DB
}
rcPort.setStatus(RCPort.STATUS.INACTIVE);
try {
rcPort.update();
- break;
+ return true; // Success
} catch (ObjectDoesntExistException | WrongVersionException e) {
// retry
}
}
+
+ return false; // Failure
}
- public void addLink(LinkEvent linkEvent) {
- log.debug("Adding link {}", linkEvent);
+ /**
+ * Add a link to the database.
+ *
+ * @param link the link to add.
+ * @return true on success, otherwise false.
+ */
+ public boolean addLink(LinkEvent link) {
+ log.debug("Adding link {}", link);
- RCLink rcLink = new RCLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getNumber(),
- linkEvent.getDst().getDpid(), linkEvent.getDst().getNumber());
+ RCLink rcLink = new RCLink(link.getSrc().getDpid(),
+ link.getSrc().getNumber(),
+ link.getDst().getDpid(),
+ link.getDst().getNumber());
- //RCPort rcSrcPort = new RCPort(link.getSrc().getDpid(), link.getSrc().getNumber());
- //RCPort rcDstPort = new RCPort(link.getDst().getDpid(), link.getDst().getNumber());
+ // XXX This method is called only by discovery,
+ // which means what we are trying to write currently is the truth
+ // so we can force write here
+ //
+ // TODO: We need to check for errors
+ rcLink.setStatus(RCLink.STATUS.ACTIVE);
+ rcLink.forceCreate();
- for (int i = 0; i < NUM_RETRIES; i++) {
- try {
- //rcSrcPort.read();
- //rcDstPort.read();
- rcLink.create();
- //} catch (ObjectDoesntExistException e) {
- // port doesn't exist
- //log.error("Add link failed {}", link, e);
- //return;
- } catch (ObjectExistsException e) {
- log.debug("Link already exists {}", linkEvent);
- return;
- }
-
- //rcSrcPort.addLinkId(rcLink.getId());
- //rcDstPort.addLinkId(rcLink.getId());
-
- rcLink.setStatus(RCLink.STATUS.ACTIVE);
-
- try {
- rcLink.update();
- //rcSrcPort.update();
- //rcDstPort.update();
- break;
- } catch (ObjectDoesntExistException | WrongVersionException e) {
- log.debug(" ", e);
- // retry
- }
- }
+ return true; // Success
}
- public void removeLink(LinkEvent linkEvent) {
- log.debug("Removing link {}", linkEvent);
- RCLink rcLink = new RCLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getNumber(),
- linkEvent.getDst().getDpid(), linkEvent.getDst().getNumber());
+ /**
+ * Remove a link from the database.
+ *
+ * @param link the link to remove.
+ * @return true on success, otherwise false.
+ */
+ public boolean removeLink(LinkEvent link) {
+ log.debug("Removing link {}", link);
+ RCLink rcLink = new RCLink(link.getSrc().getDpid(),
+ link.getSrc().getNumber(),
+ link.getDst().getDpid(),
+ link.getDst().getNumber());
//RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber());
//RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber());
@@ -217,8 +241,8 @@
rcLink.read();
} catch (ObjectDoesntExistException e) {
// XXX Note: This error might be harmless, if triggered by out-dated remove Link event
- log.error("Remove link failed {}", linkEvent, e);
- return;
+ log.error("Remove link failed {}", link, e);
+ return true; // Success: no object in the DB
}
//rcSrcPort.removeLinkId(rcLink.getId());
@@ -228,26 +252,47 @@
//rcSrcPort.update();
//rcDstPort.update();
rcLink.delete();
- return;
+ return true; // Success
} catch (ObjectDoesntExistException e) {
- log.error("Remove link failed {}", linkEvent, e);
- return;
+ log.error("Remove link failed {}", link, e);
+ return true; // Success: no object in the DB
} catch (WrongVersionException e) {
// retry
}
}
+
+ return false; // Failure
}
- public void updateDevice(DeviceEvent device) {
+ /**
+ * Add a device to the database.
+ *
+ * @param device the device to add.
+ * @return true on success, otherwise false.
+ */
+ public boolean updateDevice(DeviceEvent device) {
// TODO implement
+ return false; // Failure: not implemented yet
}
- public void removeDevice(DeviceEvent device) {
+ /**
+ * Remove a device from the database.
+ *
+ * @param device the device to remove.
+ * @return true on success, otherwise false.
+ */
+ public boolean removeDevice(DeviceEvent device) {
// TODO implement
+ return false; // Failure: not implemented yet
}
- // TODO what happens if this fails? why could it fail?
- private void writeObject(RCObject object) {
+ /**
+ * Write an object to the database.
+ *
+ * @param object the object to write.
+ * @return true on success, otherwise false.
+ */
+ private boolean writeObject(RCObject object) {
for (int i = 0; i < NUM_RETRIES; i++) {
try {
object.create();
@@ -257,18 +302,20 @@
} catch (ObjectDoesntExistException e1) {
// TODO Auto-generated catch block
log.error(" ", e);
- return;
+ return false; // Failure
}
}
try {
// TODO check API for writing without caring what's there
object.update();
- break;
+ return true; // Success
} catch (ObjectDoesntExistException | WrongVersionException e) {
log.debug(" ", e);
// re-read and retry
}
}
+
+ return false; // Failure
}
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
index 628ad7c..d690821 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -9,6 +9,9 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.floodlightcontroller.util.MACAddress;
@@ -24,6 +27,11 @@
private ConcurrentMap<InetAddress, Set<Device>> addr2Device;
private ConcurrentMap<MACAddress, Device> mac2Device;
+
+ private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+ private Lock readLock = readWriteLock.readLock();
+ // TODO use the write lock after refactor
+ private Lock writeLock = readWriteLock.writeLock();
public NetworkGraphImpl() {
// TODO: Does these object need to be stored in Concurrent Collection?
@@ -148,4 +156,14 @@
}
}
}
+
+ @Override
+ public void acquireLock() {
+ readLock.lock();
+ }
+
+ @Override
+ public void releaseLock() {
+ readLock.unlock();
+ }
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
index 6ae4777..7a6ef26 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
@@ -53,6 +53,7 @@
new ArrayList<Class<? extends IFloodlightService>>();
dependencies.add(IDatagridService.class);
dependencies.add(IRestApiService.class);
+ dependencies.add(IControllerRegistryService.class);
return dependencies;
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
index fc57b3b..25a322b 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
@@ -54,12 +54,25 @@
private final IControllerRegistryService registryService;
private CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners;
- public TopologyManager(IControllerRegistryService registryService, CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners) {
+ /**
+ * Constructor.
+ *
+ * @param registryService the Registry Service to use.
+ * @param networkGraphListeners the collection of Network Graph Listeners
+ * to use.
+ */
+ public TopologyManager(IControllerRegistryService registryService,
+ CopyOnWriteArrayList<INetworkGraphListener> networkGraphListeners) {
datastore = new NetworkGraphDatastore(this);
this.registryService = registryService;
this.networkGraphListeners = networkGraphListeners;
}
+ /**
+ * Get the Network Graph.
+ *
+ * @return the Network Graph.
+ */
NetworkGraph getNetworkGraph() {
return networkGraph;
}
@@ -213,9 +226,10 @@
}
/**
- * Exception to be thrown when Modification to the Network Graph cannot be continued due to broken invariant.
+ * Exception to be thrown when Modification to the Network Graph cannot be
+ * continued due to broken invariant.
*
- * XXX Should this be checked exception or RuntimeException
+ * XXX Should this be checked exception or RuntimeException?
*/
public static class BrokenInvariantException extends RuntimeException {
private static final long serialVersionUID = 1L;
@@ -233,858 +247,883 @@
* NetworkGraphDiscoveryInterface methods
* ******************************/
- @Override
- public void putSwitchEvent(SwitchEvent switchEvent) {
- if (prepareForAddSwitchEvent(switchEvent)) {
- datastore.addSwitch(switchEvent);
- putSwitch(switchEvent);
- // Send out notification
- TopologyEvent topologyEvent =
- new TopologyEvent(switchEvent, registryService.getControllerId());
- eventChannel.addEntry(topologyEvent.getID(),
- topologyEvent);
+ @Override
+ public void putSwitchEvent(SwitchEvent switchEvent) {
+ if (prepareForAddSwitchEvent(switchEvent)) {
+ datastore.addSwitch(switchEvent);
+ putSwitch(switchEvent);
+ // Send out notification
+ TopologyEvent topologyEvent =
+ new TopologyEvent(switchEvent, registryService.getControllerId());
+ eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
+ }
+ // TODO handle invariant violation
+ }
+
+ @Override
+ public void removeSwitchEvent(SwitchEvent switchEvent) {
+ if (prepareForRemoveSwitchEvent(switchEvent)) {
+ datastore.deactivateSwitch(switchEvent);
+ removeSwitch(switchEvent);
+ // Send out notification
+ eventChannel.removeEntry(switchEvent.getID());
+ }
+ // TODO handle invariant violation
+ }
+
+ @Override
+ public void putPortEvent(PortEvent portEvent) {
+ if (prepareForAddPortEvent(portEvent)) {
+ datastore.addPort(portEvent);
+ putPort(portEvent);
+ // Send out notification
+ TopologyEvent topologyEvent =
+ new TopologyEvent(portEvent, registryService.getControllerId());
+ eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
+ }
+ // TODO handle invariant violation
+ }
+
+ @Override
+ public void removePortEvent(PortEvent portEvent) {
+ if (prepareForRemovePortEvent(portEvent)) {
+ datastore.deactivatePort(portEvent);
+ removePort(portEvent);
+ // Send out notification
+ eventChannel.removeEntry(portEvent.getID());
+ }
+ // TODO handle invariant violation
+ }
+
+ @Override
+ public void putLinkEvent(LinkEvent linkEvent) {
+ if (prepareForAddLinkEvent(linkEvent)) {
+ datastore.addLink(linkEvent);
+ putLink(linkEvent);
+ // Send out notification
+ TopologyEvent topologyEvent =
+ new TopologyEvent(linkEvent, registryService.getControllerId());
+ eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
+ }
+ // TODO handle invariant violation
+ }
+
+ @Override
+ public void removeLinkEvent(LinkEvent linkEvent) {
+ removeLinkEvent(linkEvent, false);
+ }
+
+ private void removeLinkEvent(LinkEvent linkEvent,
+ boolean dstCheckBeforeDBmodify) {
+ if (prepareForRemoveLinkEvent(linkEvent)) {
+ if (dstCheckBeforeDBmodify) {
+ // write to DB only if it is owner of the dst dpid
+ if (registryService.hasControl(linkEvent.getDst().dpid)) {
+ datastore.removeLink(linkEvent);
}
- // TODO handle invariant violation
+ } else {
+ datastore.removeLink(linkEvent);
+ }
+ removeLink(linkEvent);
+ // Send out notification
+ eventChannel.removeEntry(linkEvent.getID());
}
+ // TODO handle invariant violation
+ }
- @Override
- public void removeSwitchEvent(SwitchEvent switchEvent) {
- if (prepareForRemoveSwitchEvent(switchEvent)) {
- datastore.deactivateSwitch(switchEvent);
- removeSwitch(switchEvent);
- // Send out notification
- eventChannel.removeEntry(switchEvent.getID());
- }
- // TODO handle invariant violation
+ @Override
+ public void putDeviceEvent(DeviceEvent deviceEvent) {
+ if (prepareForAddDeviceEvent(deviceEvent)) {
+// datastore.addDevice(deviceEvent);
+// putDevice(deviceEvent);
+ // Send out notification
+ TopologyEvent topologyEvent =
+ new TopologyEvent(deviceEvent,
+ registryService.getControllerId());
+ eventChannel.addEntry(topologyEvent.getID(),
+ topologyEvent);
}
+ // TODO handle invariant violation
+ // XXX if prepareFor~ method returned false, event should be dropped
+ }
- @Override
- public void putPortEvent(PortEvent portEvent) {
- if (prepareForAddPortEvent(portEvent)) {
- datastore.addPort(portEvent);
- putPort(portEvent);
- // Send out notification
- TopologyEvent topologyEvent =
- new TopologyEvent(portEvent, registryService.getControllerId());
- eventChannel.addEntry(topologyEvent.getID(),
- topologyEvent);
- }
- // TODO handle invariant violation
+ @Override
+ public void removeDeviceEvent(DeviceEvent deviceEvent) {
+ if (prepareForRemoveDeviceEvent(deviceEvent)) {
+// datastore.removeDevice(deviceEvent);
+// removeDevice(deviceEvent);
+ // Send out notification
+ eventChannel.removeEntry(deviceEvent.getID());
}
+ // TODO handle invariant violation
+ // XXX if prepareFor~ method returned false, event should be dropped
+ }
- @Override
- public void removePortEvent(PortEvent portEvent) {
- if (prepareForRemovePortEvent(portEvent)) {
- datastore.deactivatePort(portEvent);
- removePort(portEvent);
- // Send out notification
- eventChannel.removeEntry(portEvent.getID());
- }
- // TODO handle invariant violation
- }
+ /* *****************
+ * Internal methods to maintain invariants of the network graph
+ * *****************/
- @Override
- public void putLinkEvent(LinkEvent linkEvent) {
- if (prepareForAddLinkEvent(linkEvent)) {
- datastore.addLink(linkEvent);
- putLink(linkEvent);
- // Send out notification
- TopologyEvent topologyEvent =
- new TopologyEvent(linkEvent, registryService.getControllerId());
- eventChannel.addEntry(topologyEvent.getID(),
- topologyEvent);
- }
- // TODO handle invariant violation
- }
+ /**
+ *
+ * @param swEvent
+ * @return true if ready to accept event.
+ */
+ private boolean prepareForAddSwitchEvent(SwitchEvent swEvent) {
+ // No show stopping precondition
+ // Prep: remove(deactivate) Ports on Switch, which is not on event
+ removePortsNotOnEvent(swEvent);
+ return true;
+ }
- @Override
- public void removeLinkEvent(LinkEvent linkEvent) {
- removeLinkEvent(linkEvent, false);
+ private boolean prepareForRemoveSwitchEvent(SwitchEvent swEvent) {
+ // No show stopping precondition
+ // Prep: remove(deactivate) Ports on Switch, which is not on event
+ // XXX may be remove switch should imply wipe all ports
+ removePortsNotOnEvent(swEvent);
+ return true;
+ }
- }
-
- private void removeLinkEvent(LinkEvent linkEvent, boolean dstCheckBeforeDBmodify) {
- if (prepareForRemoveLinkEvent(linkEvent)) {
- if (dstCheckBeforeDBmodify) {
- // write to DB only if it is owner of the dst dpid
- if (registryService.hasControl(linkEvent.getDst().dpid)) {
- datastore.removeLink(linkEvent);
- }
- } else {
- datastore.removeLink(linkEvent);
- }
- removeLink(linkEvent);
- // Send out notification
- eventChannel.removeEntry(linkEvent.getID());
- }
- // TODO handle invariant violation
- }
-
- @Override
- public void putDeviceEvent(DeviceEvent deviceEvent) {
- if (prepareForAddDeviceEvent(deviceEvent)) {
-// datastore.addDevice(deviceEvent);
-// putDevice(deviceEvent);
- // Send out notification
- TopologyEvent topologyEvent =
- new TopologyEvent(deviceEvent, registryService.getControllerId());
- eventChannel.addEntry(topologyEvent.getID(),
- topologyEvent);
- }
- // TODO handle invariant violation
- // XXX if prepareFor~ method returned false, event should be dropped
- }
-
- @Override
- public void removeDeviceEvent(DeviceEvent deviceEvent) {
- if (prepareForRemoveDeviceEvent(deviceEvent)) {
-// datastore.removeDevice(deviceEvent);
-// removeDevice(deviceEvent);
- // Send out notification
- eventChannel.removeEntry(deviceEvent.getID());
- }
- // TODO handle invariant violation
- // XXX if prepareFor~ method returned false, event should be dropped
- }
-
- /* *****************
- * Internal methods to maintain invariants of the network graph
- * *****************/
-
- /**
- *
- * @param swEvt
- * @return true if ready to accept event.
- */
- private boolean prepareForAddSwitchEvent(SwitchEvent swEvt) {
- // No show stopping precondition
- // Prep: remove(deactivate) Ports on Switch, which is not on event
- removePortsNotOnEvent(swEvt);
- return true;
- }
-
- private boolean prepareForRemoveSwitchEvent(SwitchEvent swEvt) {
- // No show stopping precondition
- // Prep: remove(deactivate) Ports on Switch, which is not on event
- // XXX may be remove switch should imply wipe all ports
- removePortsNotOnEvent(swEvt);
- return true;
- }
-
- private void removePortsNotOnEvent(SwitchEvent swEvt) {
- Switch sw = networkGraph.getSwitch(swEvt.getDpid());
- if ( sw != null ) {
- Set<Long> port_noOnEvent = new HashSet<>();
- for( PortEvent portEvent : swEvt.getPorts()) {
- port_noOnEvent.add(portEvent.getNumber());
- }
- // Existing ports not on event should be removed.
- // TODO Should batch eventually for performance?
- List<Port> portsToRemove = new ArrayList<Port>();
- for( Port p : sw.getPorts() ) {
- if ( !port_noOnEvent.contains(p.getNumber()) ) {
- //PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
- // calling Discovery removePort() API to wipe from DB, etc.
- //removePortEvent(rmEvent);
+ private void removePortsNotOnEvent(SwitchEvent swEvent) {
+ Switch sw = networkGraph.getSwitch(swEvent.getDpid());
+ if (sw != null) {
+ Set<Long> port_noOnEvent = new HashSet<>();
+ for (PortEvent portEvent : swEvent.getPorts()) {
+ port_noOnEvent.add(portEvent.getNumber());
+ }
+ // Existing ports not on event should be removed.
+ // TODO Should batch eventually for performance?
+ List<Port> portsToRemove = new ArrayList<Port>();
+ for (Port p : sw.getPorts()) {
+ if (!port_noOnEvent.contains(p.getNumber())) {
+ //PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
+ // calling Discovery removePort() API to wipe from DB, etc.
+ //removePortEvent(rmEvent);
// We can't remove ports here because this will trigger a remove
// from the switch's port list, which we are currently iterating
// over.
portsToRemove.add(p);
- }
- }
- for (Port p : portsToRemove) {
- PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber());
- // calling Discovery removePort() API to wipe from DB, etc.
- removePortEvent(rmEvent);
}
}
+ for (Port p : portsToRemove) {
+ PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(),
+ p.getNumber());
+ // calling Discovery removePort() API to wipe from DB, etc.
+ removePortEvent(rmEvent);
+ }
+ }
+ }
+
+ private boolean prepareForAddPortEvent(PortEvent portEvent) {
+ // Parent Switch must exist
+ if (networkGraph.getSwitch(portEvent.getDpid()) == null) {
+ log.warn("Dropping add port event because switch doesn't exist: {}",
+ portEvent);
+ return false;
+ }
+ // Prep: None
+ return true;
+ }
+
+ private boolean prepareForRemovePortEvent(PortEvent portEvent) {
+ Port port = networkGraph.getPort(portEvent.getDpid(),
+ portEvent.getNumber());
+ if (port == null) {
+ log.debug("Port already removed? {}", portEvent);
+ // let it pass
+ return true;
}
- private boolean prepareForAddPortEvent(PortEvent portEvt) {
- // Parent Switch must exist
- if ( networkGraph.getSwitch(portEvt.getDpid()) == null) {
- log.warn("Dropping add port event because switch doesn't exist: {}",
- portEvt);
- return false;
- }
- // Prep: None
- return true;
+ // Prep: Remove Link and Device Attachment
+ ArrayList<DeviceEvent> deviceEvents = new ArrayList<>();
+ for (Device device : port.getDevices()) {
+ log.debug("Removing Device {} on Port {}", device, portEvent);
+ DeviceEvent devEvent = new DeviceEvent(device.getMacAddress());
+ devEvent.addAttachmentPoint(new SwitchPort(port.getSwitch().getDpid(),
+ port.getNumber()));
+ deviceEvents.add(devEvent);
+ }
+ for (DeviceEvent devEvent : deviceEvents) {
+ // calling Discovery API to wipe from DB, etc.
+ removeDeviceEvent(devEvent);
}
- private boolean prepareForRemovePortEvent(PortEvent portEvt) {
- Port port = networkGraph.getPort(portEvt.getDpid(),
- portEvt.getNumber());
- if ( port == null ) {
- log.debug("Port already removed? {}", portEvt);
- // let it pass
- return true;
- }
+ Set<Link> links = new HashSet<>();
+ links.add(port.getOutgoingLink());
+ links.add(port.getIncomingLink());
+ for (Link link : links) {
+ if (link == null) {
+ continue;
+ }
+ log.debug("Removing Link {} on Port {}", link, portEvent);
+ LinkEvent linkEvent =
+ new LinkEvent(link.getSourceSwitchDpid(),
+ link.getSourcePortNumber(),
+ link.getDestinationSwitchDpid(),
+ link.getDestinationPortNumber());
+ // calling Discovery API to wipe from DB, etc.
- // Prep: Remove Link and Device Attachment
- ArrayList<DeviceEvent> deviceEvts = new ArrayList<>();
- for (Device device : port.getDevices()) {
- log.debug("Removing Device {} on Port {}", device, portEvt);
- DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
- devEvt.addAttachmentPoint(new SwitchPort(port.getSwitch().getDpid(), port.getNumber()));
- deviceEvts.add(devEvt);
- }
- for (DeviceEvent devEvt : deviceEvts) {
- // calling Discovery API to wipe from DB, etc.
- removeDeviceEvent(devEvt);
- }
-
- Set<Link> links = new HashSet<>();
- links.add(port.getOutgoingLink());
- links.add(port.getIncomingLink());
- for ( Link link : links) {
- if (link == null ) {
- continue;
- }
- log.debug("Removing Link {} on Port {}", link, portEvt);
- LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(), link.getSourcePortNumber(), link.getDestinationSwitchDpid(), link.getDestinationPortNumber());
- // calling Discovery API to wipe from DB, etc.
-
- // Call internal remove Link, which will check
- // ownership of DST dpid and modify DB only if it is the owner
- removeLinkEvent(linkEvent, true);
- }
- return true;
+ // Call internal remove Link, which will check
+ // ownership of DST dpid and modify DB only if it is the owner
+ removeLinkEvent(linkEvent, true);
}
+ return true;
+ }
- private boolean prepareForAddLinkEvent(LinkEvent linkEvt) {
- // Src/Dst Port must exist
- Port srcPort = networkGraph.getPort(linkEvt.getSrc().dpid,
- linkEvt.getSrc().number);
- Port dstPort = networkGraph.getPort(linkEvt.getDst().dpid,
- linkEvt.getDst().number);
- if ( srcPort == null || dstPort == null ) {
+ private boolean prepareForAddLinkEvent(LinkEvent linkEvent) {
+ // Src/Dst Port must exist
+ Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid,
+ linkEvent.getSrc().number);
+ Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid,
+ linkEvent.getDst().number);
+ if (srcPort == null || dstPort == null) {
log.warn("Dropping add link event because port doesn't exist: {}",
- linkEvt);
- return false;
- }
-
- // Prep: remove Device attachment on both Ports
- ArrayList<DeviceEvent> deviceEvents = new ArrayList<>();
- for (Device device : srcPort.getDevices()) {
- DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
- devEvt.addAttachmentPoint(new SwitchPort(srcPort.getSwitch().getDpid(), srcPort.getNumber()));
- deviceEvents.add(devEvt);
- }
- for (Device device : dstPort.getDevices()) {
- DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
- devEvt.addAttachmentPoint(new SwitchPort(dstPort.getSwitch().getDpid(), dstPort.getNumber()));
- deviceEvents.add(devEvt);
- }
- for (DeviceEvent devEvt : deviceEvents) {
- // calling Discovery API to wipe from DB, etc.
- removeDeviceEvent(devEvt);
- }
-
- return true;
+ linkEvent);
+ return false;
}
- private boolean prepareForRemoveLinkEvent(LinkEvent linkEvt) {
- // Src/Dst Port must exist
- Port srcPort = networkGraph.getPort(linkEvt.getSrc().dpid,
- linkEvt.getSrc().number);
- Port dstPort = networkGraph.getPort(linkEvt.getDst().dpid,
- linkEvt.getDst().number);
- if ( srcPort == null || dstPort == null ) {
- log.warn("Dropping remove link event because port doesn't exist {}", linkEvt);
- return false;
- }
-
- Link link = srcPort.getOutgoingLink();
-
- // Link is already gone, or different Link exist in memory
- // XXX Check if we should reject or just accept these cases.
- // it should be harmless to remove the Link on event from DB anyways
- if (link == null ||
- !link.getDestinationPortNumber().equals(linkEvt.getDst().number)
- || !link.getDestinationSwitchDpid().equals(linkEvt.getDst().dpid)) {
- log.warn("Dropping remove link event because link doesn't exist: {}", linkEvt);
- return false;
- }
- // Prep: None
- return true;
+ // Prep: remove Device attachment on both Ports
+ ArrayList<DeviceEvent> deviceEvents = new ArrayList<>();
+ for (Device device : srcPort.getDevices()) {
+ DeviceEvent devEvent = new DeviceEvent(device.getMacAddress());
+ devEvent.addAttachmentPoint(new SwitchPort(srcPort.getSwitch().getDpid(), srcPort.getNumber()));
+ deviceEvents.add(devEvent);
+ }
+ for (Device device : dstPort.getDevices()) {
+ DeviceEvent devEvent = new DeviceEvent(device.getMacAddress());
+ devEvent.addAttachmentPoint(new SwitchPort(dstPort.getSwitch().getDpid(),
+ dstPort.getNumber()));
+ deviceEvents.add(devEvent);
+ }
+ for (DeviceEvent devEvent : deviceEvents) {
+ // calling Discovery API to wipe from DB, etc.
+ removeDeviceEvent(devEvent);
}
- /**
- *
- * @param deviceEvt Event will be modified to remove inapplicable attachemntPoints/ipAddress
- * @return false if this event should be dropped.
- */
- private boolean prepareForAddDeviceEvent(DeviceEvent deviceEvt) {
- boolean preconditionBroken = false;
- ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>();
- for ( PortEvent.SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
- // Attached Ports must exist
- Port port = networkGraph.getPort(swp.dpid, swp.number);
- if ( port == null ) {
- preconditionBroken = true;
- failedSwitchPort.add(swp);
- continue;
- }
- // Attached Ports must not have Link
- if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) {
- preconditionBroken = true;
- failedSwitchPort.add(swp);
- continue;
- }
- }
+ return true;
+ }
- // Rewriting event to exclude failed attachmentPoint
- // XXX Assumption behind this is that inapplicable device event should
- // be dropped, not deferred. If we decide to defer Device event,
- // rewriting can become a problem
- List<SwitchPort> attachmentPoints = deviceEvt.getAttachmentPoints();
- attachmentPoints.removeAll(failedSwitchPort);
- deviceEvt.setAttachmentPoints(attachmentPoints);
+ private boolean prepareForRemoveLinkEvent(LinkEvent linkEvent) {
+ // Src/Dst Port must exist
+ Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid,
+ linkEvent.getSrc().number);
+ Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid,
+ linkEvent.getDst().number);
+ if (srcPort == null || dstPort == null) {
+ log.warn("Dropping remove link event because port doesn't exist {}", linkEvent);
+ return false;
+ }
- if ( deviceEvt.getAttachmentPoints().isEmpty() && deviceEvt.getIpAddresses().isEmpty() ) {
- // return false to represent: Nothing left to do for this event. Caller should drop event
- return false;
+ Link link = srcPort.getOutgoingLink();
+
+ // Link is already gone, or different Link exist in memory
+ // XXX Check if we should reject or just accept these cases.
+ // it should be harmless to remove the Link on event from DB anyways
+ if (link == null ||
+ !link.getDestinationPortNumber().equals(linkEvent.getDst().number)
+ || !link.getDestinationSwitchDpid().equals(linkEvent.getDst().dpid)) {
+ log.warn("Dropping remove link event because link doesn't exist: {}", linkEvent);
+ return false;
+ }
+ // Prep: None
+ return true;
+ }
+
+ /**
+ *
+ * @param deviceEvent Event will be modified to remove inapplicable attachemntPoints/ipAddress
+ * @return false if this event should be dropped.
+ */
+ private boolean prepareForAddDeviceEvent(DeviceEvent deviceEvent) {
+ boolean preconditionBroken = false;
+ ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>();
+ for ( PortEvent.SwitchPort swp : deviceEvent.getAttachmentPoints() ) {
+ // Attached Ports must exist
+ Port port = networkGraph.getPort(swp.dpid, swp.number);
+ if (port == null) {
+ preconditionBroken = true;
+ failedSwitchPort.add(swp);
+ continue;
}
+ // Attached Ports must not have Link
+ if (port.getOutgoingLink() != null ||
+ port.getIncomingLink() != null) {
+ preconditionBroken = true;
+ failedSwitchPort.add(swp);
+ continue;
+ }
+ }
+
+ // Rewriting event to exclude failed attachmentPoint
+ // XXX Assumption behind this is that inapplicable device event should
+ // be dropped, not deferred. If we decide to defer Device event,
+ // rewriting can become a problem
+ List<SwitchPort> attachmentPoints = deviceEvent.getAttachmentPoints();
+ attachmentPoints.removeAll(failedSwitchPort);
+ deviceEvent.setAttachmentPoints(attachmentPoints);
+
+ if (deviceEvent.getAttachmentPoints().isEmpty() &&
+ deviceEvent.getIpAddresses().isEmpty()) {
+ // return false to represent: Nothing left to do for this event.
+ // Caller should drop event
+ return false;
+ }
// Should we return false to tell caller that the event was trimmed?
// if ( preconditionBroken ) {
// return false;
// }
- return true;
+ return true;
+ }
+
+ private boolean prepareForRemoveDeviceEvent(DeviceEvent deviceEvent) {
+ // No show stopping precondition?
+ // Prep: none
+ return true;
+ }
+
+ /* ******************************
+ * NetworkGraphReplicationInterface methods
+ * ******************************/
+
+ @Override
+ public void putSwitchReplicationEvent(SwitchEvent switchEvent) {
+ if (prepareForAddSwitchEvent(switchEvent)) {
+ putSwitch(switchEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchPutSwitchEvent(switchEvent);
+ }
+
+ @Override
+ public void removeSwitchReplicationEvent(SwitchEvent switchEvent) {
+ if (prepareForRemoveSwitchEvent(switchEvent)) {
+ removeSwitch(switchEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchRemoveSwitchEvent(switchEvent);
+ }
+
+ @Override
+ public void putPortReplicationEvent(PortEvent portEvent) {
+ if (prepareForAddPortEvent(portEvent)) {
+ putPort(portEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchPutPortEvent(portEvent);
+ }
+
+ @Override
+ public void removePortReplicationEvent(PortEvent portEvent) {
+ if (prepareForRemovePortEvent(portEvent)) {
+ removePort(portEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchRemovePortEvent(portEvent);
+ }
+
+ @Override
+ public void putLinkReplicationEvent(LinkEvent linkEvent) {
+ if (prepareForAddLinkEvent(linkEvent)) {
+ putLink(linkEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchPutLinkEvent(linkEvent);
+ }
+
+ @Override
+ public void removeLinkReplicationEvent(LinkEvent linkEvent) {
+ if (prepareForRemoveLinkEvent(linkEvent)) {
+ removeLink(linkEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchRemoveLinkEvent(linkEvent);
+ }
+
+ @Override
+ public void putDeviceReplicationEvent(DeviceEvent deviceEvent) {
+ if (prepareForAddDeviceEvent(deviceEvent)) {
+ putDevice(deviceEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchPutDeviceEvent(deviceEvent);
+ }
+
+ @Override
+ public void removeDeviceReplicationEvent(DeviceEvent deviceEvent) {
+ if (prepareForRemoveDeviceEvent(deviceEvent)) {
+ removeDevice(deviceEvent);
+ }
+ // TODO handle invariant violation
+ // trigger instance local topology event handler
+ dispatchRemoveDeviceEvent(deviceEvent);
+ }
+
+ /* ************************************************
+ * Internal In-memory object mutation methods.
+ * ************************************************/
+
+ void putSwitch(SwitchEvent swEvent) {
+ if (swEvent == null) {
+ throw new IllegalArgumentException("Switch cannot be null");
}
- private boolean prepareForRemoveDeviceEvent(DeviceEvent deviceEvt) {
- // No show stopping precondition?
- // Prep: none
- return true;
+ Switch sw = networkGraph.getSwitch(swEvent.getDpid());
+
+ if (sw == null) {
+ sw = new SwitchImpl(networkGraph, swEvent.getDpid());
+ networkGraph.putSwitch(sw);
}
- /* ******************************
- * NetworkGraphReplicationInterface methods
- * ******************************/
+ // Update when more attributes are added to Event object
+ // no attribute to update for now
- @Override
- public void putSwitchReplicationEvent(SwitchEvent switchEvent) {
- if (prepareForAddSwitchEvent(switchEvent)) {
- putSwitch(switchEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchPutSwitchEvent(switchEvent);
+ // TODO handle child Port event properly for performance
+ for (PortEvent portEvent : swEvent.getPorts() ) {
+ putPort(portEvent);
+ }
+ }
+
+ void removeSwitch(SwitchEvent swEvent) {
+ if (swEvent == null) {
+ throw new IllegalArgumentException("Switch cannot be null");
}
- @Override
- public void removeSwitchReplicationEvent(SwitchEvent switchEvent) {
- if (prepareForRemoveSwitchEvent(switchEvent)) {
- removeSwitch(switchEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchRemoveSwitchEvent(switchEvent);
+ // TODO handle child Port event properly for performance
+ for (PortEvent portEvent : swEvent.getPorts() ) {
+ removePort(portEvent);
}
- @Override
- public void putPortReplicationEvent(PortEvent portEvent) {
- if (prepareForAddPortEvent(portEvent)) {
- putPort(portEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchPutPortEvent(portEvent);
+ Switch sw = networkGraph.getSwitch(swEvent.getDpid());
+ if (sw == null) {
+ log.warn("Switch {} already removed, ignoring", swEvent);
+ return;
}
- @Override
- public void removePortReplicationEvent(PortEvent portEvent) {
- if (prepareForRemovePortEvent(portEvent)) {
- removePort(portEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchRemovePortEvent(portEvent);
+ // remove all ports if there still exist
+ ArrayList<PortEvent> portsToRemove = new ArrayList<>();
+ for (Port port : sw.getPorts()) {
+ log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now",
+ port, swEvent);
+ PortEvent portEvent = new PortEvent(port.getDpid(),
+ port.getNumber());
+ portsToRemove.add(portEvent);
+ }
+ for (PortEvent portEvent : portsToRemove) {
+ // XXX calling removePortEvent() may trigger duplicate event,
+ // once at prepare phase, second time here
+ // If event can be squashed, ignored etc. at receiver side it
+ // shouldn't be a problem, but if not need to re-visit this issue.
+
+ // Note: removePortEvent() implies removal of attached Device, etc.
+ // if we decide not to call removePortEvent(), Device needs to be
+ // handled properly
+ removePortEvent(portEvent);
}
- @Override
- public void putLinkReplicationEvent(LinkEvent linkEvent) {
- if (prepareForAddLinkEvent(linkEvent)) {
- putLink(linkEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchPutLinkEvent(linkEvent);
+ networkGraph.removeSwitch(swEvent.getDpid());
+ }
+
+ void putPort(PortEvent portEvent) {
+ if (portEvent == null) {
+ throw new IllegalArgumentException("Port cannot be null");
}
-
- @Override
- public void removeLinkReplicationEvent(LinkEvent linkEvent) {
- if (prepareForRemoveLinkEvent(linkEvent)) {
- removeLink(linkEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchRemoveLinkEvent(linkEvent);
- }
-
- @Override
- public void putDeviceReplicationEvent(DeviceEvent deviceEvent) {
- if (prepareForAddDeviceEvent(deviceEvent)) {
- putDevice(deviceEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchPutDeviceEvent(deviceEvent);
- }
-
- @Override
- public void removeDeviceReplicationEvent(DeviceEvent deviceEvent) {
- if (prepareForRemoveDeviceEvent(deviceEvent)) {
- removeDevice(deviceEvent);
- }
- // TODO handle invariant violation
- // trigger instance local topology event handler
- dispatchRemoveDeviceEvent(deviceEvent);
- }
-
- /* ************************************************
- * Internal In-memory object mutation methods.
- * ************************************************/
-
- void putSwitch(SwitchEvent swEvt) {
- if (swEvt == null) {
- throw new IllegalArgumentException("Switch cannot be null");
- }
-
- Switch sw = networkGraph.getSwitch(swEvt.getDpid());
-
- if (sw == null) {
- sw = new SwitchImpl(networkGraph, swEvt.getDpid());
- networkGraph.putSwitch(sw);
- }
-
- // Update when more attributes are added to Event object
- // no attribute to update for now
-
- // TODO handle child Port event properly for performance
- for (PortEvent portEvt : swEvt.getPorts() ) {
- putPort(portEvt);
- }
-
- }
-
- void removeSwitch(SwitchEvent swEvt) {
- if (swEvt == null) {
- throw new IllegalArgumentException("Switch cannot be null");
- }
-
- // TODO handle child Port event properly for performance
- for (PortEvent portEvt : swEvt.getPorts() ) {
- removePort(portEvt);
- }
-
- Switch sw = networkGraph.getSwitch(swEvt.getDpid());
-
- if (sw == null) {
- log.warn("Switch {} already removed, ignoring", swEvt);
- return;
- }
-
- // remove all ports if there still exist
- ArrayList<PortEvent> portsToRemove = new ArrayList<>();
- for (Port port : sw.getPorts()) {
- log.warn(
- "Port {} on Switch {} should be removed prior to removing Switch. Removing Port now",
- port, swEvt);
- PortEvent portEvt = new PortEvent(port.getDpid(), port.getNumber());
- portsToRemove.add(portEvt);
- }
- for (PortEvent portEvt : portsToRemove) {
- // XXX calling removePortEvent() may trigger duplicate event, once at prepare phase, second time here
- // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
- // need to re-visit this issue.
-
- // Note: removePortEvent() implies removal of attached Device, etc.
- // if we decide not to call removePortEvent(), Device needs to be handled properly
- removePortEvent(portEvt);
- }
-
- networkGraph.removeSwitch(swEvt.getDpid());
- }
-
- void putPort(PortEvent portEvt) {
- if (portEvt == null) {
- throw new IllegalArgumentException("Port cannot be null");
- }
- Switch sw = networkGraph.getSwitch(portEvt.getDpid());
- if (sw == null) {
- throw new BrokenInvariantException(String.format(
+ Switch sw = networkGraph.getSwitch(portEvent.getDpid());
+ if (sw == null) {
+ throw new BrokenInvariantException(String.format(
"Switch with dpid %s did not exist.",
- new Dpid(portEvt.getDpid())));
- }
- Port p = sw.getPort(portEvt.getNumber());
- PortImpl port = null;
- if (p != null) {
- port = getPortImpl(p);
- }
-
- if (port == null) {
- port = new PortImpl(networkGraph, sw, portEvt.getNumber());
- }
-
- // TODO update attributes
-
- SwitchImpl s = getSwitchImpl(sw);
- s.addPort(port);
+ new Dpid(portEvent.getDpid())));
+ }
+ Port p = sw.getPort(portEvent.getNumber());
+ PortImpl port = null;
+ if (p != null) {
+ port = getPortImpl(p);
}
- void removePort(PortEvent portEvt) {
- if (portEvt == null) {
- throw new IllegalArgumentException("Port cannot be null");
- }
-
- Switch sw = networkGraph.getSwitch(portEvt.getDpid());
- if (sw == null) {
- log.warn("Parent Switch for Port {} already removed, ignoring", portEvt);
- return;
- }
-
- Port p = sw.getPort(portEvt.getNumber());
- if (p == null) {
- log.warn("Port {} already removed, ignoring", portEvt);
- return;
- }
-
- // Remove Link and Device Attachment
- for (Device device : p.getDevices()) {
- log.debug("Removing Device {} on Port {}", device, portEvt);
- DeviceEvent devEvt = new DeviceEvent(device.getMacAddress());
- devEvt.addAttachmentPoint(new SwitchPort(p.getSwitch().getDpid(), p.getNumber()));
-
- // XXX calling removeDeviceEvent() may trigger duplicate event, once at prepare phase, second time here
- // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
- // need to re-visit
-
- // calling Discovery API to wipe from DB, etc.
- removeDeviceEvent(devEvt);
- }
- Set<Link> links = new HashSet<>();
- links.add(p.getOutgoingLink());
- links.add(p.getIncomingLink());
- ArrayList<LinkEvent> linksToRemove = new ArrayList<>();
- for (Link link : links) {
- if (link == null) {
- continue;
- }
- log.debug("Removing Link {} on Port {}", link, portEvt);
- LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(), link.getSourcePortNumber(), link.getDestinationSwitchDpid(), link.getDestinationPortNumber());
- linksToRemove.add(linkEvent);
- }
- for (LinkEvent linkEvent : linksToRemove) {
- // XXX calling removeLinkEvent() may trigger duplicate event, once at prepare phase, second time here
- // If event can be squashed, ignored etc. at receiver side it shouldn't be a problem, but if not
- // need to re-visit
-
- // calling Discovery API to wipe from DB, etc.
- removeLinkEvent(linkEvent);
- }
-
- // remove Port from Switch
- SwitchImpl s = getSwitchImpl(sw);
- s.removePort(p);
+ if (port == null) {
+ port = new PortImpl(networkGraph, sw, portEvent.getNumber());
}
- void putLink(LinkEvent linkEvt) {
- if (linkEvt == null) {
- throw new IllegalArgumentException("Link cannot be null");
- }
+ // TODO update attributes
- Port srcPort = networkGraph.getPort(linkEvt.getSrc().dpid,
- linkEvt.getSrc().number);
- if (srcPort == null) {
- throw new BrokenInvariantException(
+ SwitchImpl s = getSwitchImpl(sw);
+ s.addPort(port);
+ }
+
+ void removePort(PortEvent portEvent) {
+ if (portEvent == null) {
+ throw new IllegalArgumentException("Port cannot be null");
+ }
+
+ Switch sw = networkGraph.getSwitch(portEvent.getDpid());
+ if (sw == null) {
+ log.warn("Parent Switch for Port {} already removed, ignoring",
+ portEvent);
+ return;
+ }
+
+ Port p = sw.getPort(portEvent.getNumber());
+ if (p == null) {
+ log.warn("Port {} already removed, ignoring", portEvent);
+ return;
+ }
+
+ // Remove Link and Device Attachment
+ for (Device device : p.getDevices()) {
+ log.debug("Removing Device {} on Port {}", device, portEvent);
+ DeviceEvent devEvent = new DeviceEvent(device.getMacAddress());
+ devEvent.addAttachmentPoint(new SwitchPort(p.getSwitch().getDpid(),
+ p.getNumber()));
+
+ // XXX calling removeDeviceEvent() may trigger duplicate event,
+ // once at prepare phase, second time here
+ // If event can be squashed, ignored etc. at receiver side it
+ // shouldn't be a problem, but if not need to re-visit
+
+ // calling Discovery API to wipe from DB, etc.
+ removeDeviceEvent(devEvent);
+ }
+ Set<Link> links = new HashSet<>();
+ links.add(p.getOutgoingLink());
+ links.add(p.getIncomingLink());
+ ArrayList<LinkEvent> linksToRemove = new ArrayList<>();
+ for (Link link : links) {
+ if (link == null) {
+ continue;
+ }
+ log.debug("Removing Link {} on Port {}", link, portEvent);
+ LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(),
+ link.getSourcePortNumber(),
+ link.getDestinationSwitchDpid(),
+ link.getDestinationPortNumber());
+ linksToRemove.add(linkEvent);
+ }
+ for (LinkEvent linkEvent : linksToRemove) {
+ // XXX calling removeLinkEvent() may trigger duplicate event,
+ // once at prepare phase, second time here
+ // If event can be squashed, ignored etc. at receiver side it
+ // shouldn't be a problem, but if not need to re-visit
+
+ // calling Discovery API to wipe from DB, etc.
+ removeLinkEvent(linkEvent);
+ }
+
+ // remove Port from Switch
+ SwitchImpl s = getSwitchImpl(sw);
+ s.removePort(p);
+ }
+
+ void putLink(LinkEvent linkEvent) {
+ if (linkEvent == null) {
+ throw new IllegalArgumentException("Link cannot be null");
+ }
+
+ Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid,
+ linkEvent.getSrc().number);
+ if (srcPort == null) {
+ throw new BrokenInvariantException(
String.format(
"Src Port %s of a Link did not exist.",
- linkEvt.getSrc() ));
- }
+ linkEvent.getSrc() ));
+ }
- Port dstPort = networkGraph.getPort(linkEvt.getDst().dpid,
- linkEvt.getDst().number);
- if (dstPort == null) {
- throw new BrokenInvariantException(
+ Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid,
+ linkEvent.getDst().number);
+ if (dstPort == null) {
+ throw new BrokenInvariantException(
String.format(
"Dst Port %s of a Link did not exist.",
- linkEvt.getDst() ));
- }
-
- // getting Link instance from destination port incoming Link
- Link l = dstPort.getIncomingLink();
- LinkImpl link = null;
- assert( l == srcPort.getOutgoingLink() );
- if (l != null) {
- link = getLinkImpl(l);
- }
-
- if (link == null) {
- link = new LinkImpl(networkGraph, srcPort, dstPort);
- }
-
-
- PortImpl dstPortMem = getPortImpl(dstPort);
- PortImpl srcPortMem = getPortImpl(srcPort);
-
- // Add Link first to avoid further Device addition
-
- // add Link to Port
- dstPortMem.setIncomingLink(link);
- srcPortMem.setOutgoingLink(link);
-
- // remove Device Pointing to Port if any
- for(Device d : dstPortMem.getDevices() ) {
- log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, dstPort, linkEvt);
- DeviceImpl dev = getDeviceImpl(d);
- dev.removeAttachmentPoint(dstPort);
- // This implies that change is made to Device Object.
- // sending Device attachment point removed event
- DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
- rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), dstPort.getNumber()));
- removeDeviceEvent(rmEvent);
- }
- dstPortMem.removeAllDevice();
- for(Device d : srcPortMem.getDevices() ) {
- log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, srcPort, linkEvt);
- DeviceImpl dev = getDeviceImpl(d);
- dev.removeAttachmentPoint(srcPort);
- // This implies that change is made to Device Object.
- // sending Device attachment point removed event
- DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
- rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(), dstPort.getNumber()));
- removeDeviceEvent(rmEvent);
- }
- srcPortMem.removeAllDevice();
-
+ linkEvent.getDst()));
}
- void removeLink(LinkEvent linkEvt) {
- if (linkEvt == null) {
- throw new IllegalArgumentException("Link cannot be null");
- }
-
- Port srcPort = networkGraph.getPort(linkEvt.getSrc().dpid,
- linkEvt.getSrc().number);
- if (srcPort == null) {
- log.warn("Src Port for Link {} already removed, ignoring", linkEvt);
- return;
- }
-
- Port dstPort = networkGraph.getPort(linkEvt.getDst().dpid,
- linkEvt.getDst().number);
- if (dstPort == null) {
- log.warn("Dst Port for Link {} already removed, ignoring", linkEvt);
- return;
- }
-
- Link l = dstPort.getIncomingLink();
- if ( l == null ) {
- log.warn("Link {} already removed on destination Port", linkEvt);
- }
- l = srcPort.getOutgoingLink();
- if ( l == null ) {
- log.warn("Link {} already removed on src Port", linkEvt);
- }
-
- getPortImpl(dstPort).setIncomingLink(null);
- getPortImpl(srcPort).setOutgoingLink(null);
+ // getting Link instance from destination port incoming Link
+ Link l = dstPort.getIncomingLink();
+ LinkImpl link = null;
+ assert(l == srcPort.getOutgoingLink());
+ if (l != null) {
+ link = getLinkImpl(l);
}
- // XXX Need to rework Device related
- void putDevice(DeviceEvent deviceEvt) {
- if (deviceEvt == null) {
- throw new IllegalArgumentException("Device cannot be null");
- }
-
- Device device = networkGraph.getDeviceByMac(deviceEvt.getMac());
- if ( device == null ) {
- device = new DeviceImpl(networkGraph, deviceEvt.getMac());
- }
- DeviceImpl memDevice = getDeviceImpl(device);
-
- // for each IP address
- for( InetAddress ipAddr : deviceEvt.getIpAddresses() ) {
- memDevice.addIpAddress(ipAddr);
- }
-
- networkGraph.putDevice(device);
-
- // for each attachment point
- for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
- // Attached Ports must exist
- Port port = networkGraph.getPort(swp.dpid, swp.number);
- if ( port == null ) {
- log.warn("Port for the attachment point {} did not exist. skipping mutation", swp);
- continue;
- }
- // Attached Ports must not have Link
- if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) {
- log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", port.getOutgoingLink(), port.getIncomingLink());
- continue;
- }
-
- // finally add Device <-> Port on In-memory structure
- PortImpl memPort = getPortImpl(port);
- memPort.addDevice(device);
- memDevice.addAttachmentPoint(port);
- }
+ if (link == null) {
+ link = new LinkImpl(networkGraph, srcPort, dstPort);
}
- void removeDevice(DeviceEvent deviceEvt) {
- if (deviceEvt == null) {
- throw new IllegalArgumentException("Device cannot be null");
- }
+ PortImpl dstPortMem = getPortImpl(dstPort);
+ PortImpl srcPortMem = getPortImpl(srcPort);
- Device device = networkGraph.getDeviceByMac(deviceEvt.getMac());
- if ( device == null ) {
- log.warn("Device {} already removed, ignoring", deviceEvt);
- return;
- }
- DeviceImpl memDevice = getDeviceImpl(device);
+ // Add Link first to avoid further Device addition
- // for each attachment point
- for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) {
- // Attached Ports must exist
- Port port = networkGraph.getPort(swp.dpid, swp.number);
- if ( port == null ) {
- log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp);
- continue;
- }
+ // add Link to Port
+ dstPortMem.setIncomingLink(link);
+ srcPortMem.setOutgoingLink(link);
- // finally remove Device <-> Port on In-memory structure
- PortImpl memPort = getPortImpl(port);
- memPort.removeDevice(device);
- memDevice.removeAttachmentPoint(port);
- }
- networkGraph.removeDevice(device);
+ // remove Device Pointing to Port if any
+ for (Device d : dstPortMem.getDevices() ) {
+ log.error("Device {} on Port {} should have been removed prior to adding Link {}",
+ d, dstPort, linkEvent);
+ DeviceImpl dev = getDeviceImpl(d);
+ dev.removeAttachmentPoint(dstPort);
+ // This implies that change is made to Device Object.
+ // sending Device attachment point removed event
+ DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
+ rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(),
+ dstPort.getNumber()));
+ removeDeviceEvent(rmEvent);
+ }
+ dstPortMem.removeAllDevice();
+ for (Device d : srcPortMem.getDevices() ) {
+ log.error("Device {} on Port {} should have been removed prior to adding Link {}",
+ d, srcPort, linkEvent);
+ DeviceImpl dev = getDeviceImpl(d);
+ dev.removeAttachmentPoint(srcPort);
+ // This implies that change is made to Device Object.
+ // sending Device attachment point removed event
+ DeviceEvent rmEvent = new DeviceEvent(d.getMacAddress());
+ rmEvent.addAttachmentPoint(new SwitchPort(dstPort.getDpid(),
+ dstPort.getNumber()));
+ removeDeviceEvent(rmEvent);
+ }
+ srcPortMem.removeAllDevice();
+ }
+
+ void removeLink(LinkEvent linkEvent) {
+ if (linkEvent == null) {
+ throw new IllegalArgumentException("Link cannot be null");
}
- private void dispatchPutSwitchEvent(SwitchEvent switchEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.putSwitchEvent(switchEvent);
- }
+ Port srcPort = networkGraph.getPort(linkEvent.getSrc().dpid,
+ linkEvent.getSrc().number);
+ if (srcPort == null) {
+ log.warn("Src Port for Link {} already removed, ignoring",
+ linkEvent);
+ return;
}
- private void dispatchRemoveSwitchEvent(SwitchEvent switchEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.removeSwitchEvent(switchEvent);
- }
+ Port dstPort = networkGraph.getPort(linkEvent.getDst().dpid,
+ linkEvent.getDst().number);
+ if (dstPort == null) {
+ log.warn("Dst Port for Link {} already removed, ignoring",
+ linkEvent);
+ return;
}
- private void dispatchPutPortEvent(PortEvent portEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.putPortEvent(portEvent);
- }
+ Link l = dstPort.getIncomingLink();
+ if ( l == null) {
+ log.warn("Link {} already removed on destination Port", linkEvent);
+ }
+ l = srcPort.getOutgoingLink();
+ if (l == null) {
+ log.warn("Link {} already removed on src Port", linkEvent);
}
- private void dispatchRemovePortEvent(PortEvent portEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.removePortEvent(portEvent);
- }
+ getPortImpl(dstPort).setIncomingLink(null);
+ getPortImpl(srcPort).setOutgoingLink(null);
+ }
+
+ // XXX Need to rework Device related
+ void putDevice(DeviceEvent deviceEvent) {
+ if (deviceEvent == null) {
+ throw new IllegalArgumentException("Device cannot be null");
}
- private void dispatchPutLinkEvent(LinkEvent linkEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.putLinkEvent(linkEvent);
- }
+ Device device = networkGraph.getDeviceByMac(deviceEvent.getMac());
+ if (device == null) {
+ device = new DeviceImpl(networkGraph, deviceEvent.getMac());
+ }
+ DeviceImpl memDevice = getDeviceImpl(device);
+
+ // for each IP address
+ for (InetAddress ipAddr : deviceEvent.getIpAddresses()) {
+ memDevice.addIpAddress(ipAddr);
}
- private void dispatchRemoveLinkEvent(LinkEvent linkEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.removeLinkEvent(linkEvent);
+ networkGraph.putDevice(device);
+
+ // for each attachment point
+ for (SwitchPort swp : deviceEvent.getAttachmentPoints()) {
+ // Attached Ports must exist
+ Port port = networkGraph.getPort(swp.dpid, swp.number);
+ if (port == null) {
+ log.warn("Port for the attachment point {} did not exist. skipping mutation", swp);
+ continue;
}
+ // Attached Ports must not have Link
+ if (port.getOutgoingLink() != null ||
+ port.getIncomingLink() != null) {
+ log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.",
+ port.getOutgoingLink(),
+ port.getIncomingLink());
+ continue;
+ }
+
+ // finally add Device <-> Port on In-memory structure
+ PortImpl memPort = getPortImpl(port);
+ memPort.addDevice(device);
+ memDevice.addAttachmentPoint(port);
+ }
+ }
+
+ void removeDevice(DeviceEvent deviceEvent) {
+ if (deviceEvent == null) {
+ throw new IllegalArgumentException("Device cannot be null");
}
- private void dispatchPutDeviceEvent(DeviceEvent deviceEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.putDeviceEvent(deviceEvent);;
+ Device device = networkGraph.getDeviceByMac(deviceEvent.getMac());
+ if (device == null) {
+ log.warn("Device {} already removed, ignoring", deviceEvent);
+ return;
+ }
+ DeviceImpl memDevice = getDeviceImpl(device);
+
+ // for each attachment point
+ for (SwitchPort swp : deviceEvent.getAttachmentPoints()) {
+ // Attached Ports must exist
+ Port port = networkGraph.getPort(swp.dpid, swp.number);
+ if (port == null) {
+ log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp);
+ continue;
}
+
+ // finally remove Device <-> Port on In-memory structure
+ PortImpl memPort = getPortImpl(port);
+ memPort.removeDevice(device);
+ memDevice.removeAttachmentPoint(port);
+ }
+ networkGraph.removeDevice(device);
+ }
+
+ private void dispatchPutSwitchEvent(SwitchEvent switchEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.putSwitchEvent(switchEvent);
+ }
+ }
+
+ private void dispatchRemoveSwitchEvent(SwitchEvent switchEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.removeSwitchEvent(switchEvent);
+ }
+ }
+
+ private void dispatchPutPortEvent(PortEvent portEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.putPortEvent(portEvent);
+ }
+ }
+
+ private void dispatchRemovePortEvent(PortEvent portEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.removePortEvent(portEvent);
+ }
+ }
+
+ private void dispatchPutLinkEvent(LinkEvent linkEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.putLinkEvent(linkEvent);
+ }
+ }
+
+ private void dispatchRemoveLinkEvent(LinkEvent linkEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.removeLinkEvent(linkEvent);
+ }
+ }
+
+ private void dispatchPutDeviceEvent(DeviceEvent deviceEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.putDeviceEvent(deviceEvent);;
+ }
+ }
+
+ private void dispatchRemoveDeviceEvent(DeviceEvent deviceEvent) {
+ for (INetworkGraphListener listener : this.networkGraphListeners) {
+ // TODO Should copy before handing them over to listener
+ listener.removeDeviceEvent(deviceEvent);
+ }
+ }
+
+ private SwitchImpl getSwitchImpl(Switch sw) {
+ if (sw instanceof SwitchImpl) {
+ return (SwitchImpl) sw;
+ }
+ throw new ClassCastException("SwitchImpl expected, but found: " + sw);
+ }
+
+ private PortImpl getPortImpl(Port p) {
+ if (p instanceof PortImpl) {
+ return (PortImpl) p;
+ }
+ throw new ClassCastException("PortImpl expected, but found: " + p);
+ }
+
+ private LinkImpl getLinkImpl(Link l) {
+ if (l instanceof LinkImpl) {
+ return (LinkImpl) l;
+ }
+ throw new ClassCastException("LinkImpl expected, but found: " + l);
+ }
+
+ private DeviceImpl getDeviceImpl(Device d) {
+ if (d instanceof DeviceImpl) {
+ return (DeviceImpl) d;
+ }
+ throw new ClassCastException("DeviceImpl expected, but found: " + d);
+ }
+
+ @Deprecated
+ public void loadWholeTopologyFromDB() {
+ // XXX May need to clear whole topology first, depending on
+ // how we initially subscribe to replication events
+
+ for (RCSwitch sw : RCSwitch.getAllSwitches()) {
+ if (sw.getStatus() != RCSwitch.STATUS.ACTIVE) {
+ continue;
+ }
+ putSwitchReplicationEvent(new SwitchEvent(sw.getDpid()));
}
- private void dispatchRemoveDeviceEvent(DeviceEvent deviceEvent) {
- for (INetworkGraphListener listener : this.networkGraphListeners) {
- // TODO Should copy before handing them over to listener
- listener.removeDeviceEvent(deviceEvent);
+ for (RCPort p : RCPort.getAllPorts()) {
+ if (p.getStatus() != RCPort.STATUS.ACTIVE) {
+ continue;
}
+ putPortReplicationEvent(new PortEvent(p.getDpid(), p.getNumber()));
}
- private SwitchImpl getSwitchImpl(Switch sw) {
- if (sw instanceof SwitchImpl) {
- return (SwitchImpl) sw;
+ // TODO Is Device going to be in DB? If so, read from DB.
+ // for (RCDevice d : RCDevice.getAllDevices()) {
+ // DeviceEvent devEvent = new DeviceEvent( MACAddress.valueOf(d.getMac()) );
+ // for (byte[] portId : d.getAllPortIds() ) {
+ // devEvent.addAttachmentPoint( new SwitchPort( RCPort.getDpidFromKey(portId), RCPort.getNumberFromKey(portId) ));
+ // }
+ // }
+
+ for (RCLink l : RCLink.getAllLinks()) {
+ // check if src/dst switch/port exist before triggering event
+ Port srcPort = networkGraph.getPort(l.getSrc().dpid,
+ l.getSrc().number);
+ Port dstPort = networkGraph.getPort(l.getDst().dpid,
+ l.getDst().number);
+ if (srcPort == null || dstPort == null) {
+ continue;
}
- throw new ClassCastException("SwitchImpl expected, but found: " + sw);
+ putLinkReplicationEvent(new LinkEvent(l.getSrc().dpid,
+ l.getSrc().number,
+ l.getDst().dpid,
+ l.getDst().number));
}
-
- private PortImpl getPortImpl(Port p) {
- if (p instanceof PortImpl) {
- return (PortImpl) p;
- }
- throw new ClassCastException("PortImpl expected, but found: " + p);
- }
-
- private LinkImpl getLinkImpl(Link l) {
- if (l instanceof LinkImpl) {
- return (LinkImpl) l;
- }
- throw new ClassCastException("LinkImpl expected, but found: " + l);
- }
-
- private DeviceImpl getDeviceImpl(Device d) {
- if (d instanceof DeviceImpl) {
- return (DeviceImpl) d;
- }
- throw new ClassCastException("DeviceImpl expected, but found: " + d);
- }
-
- @Deprecated
- public void loadWholeTopologyFromDB() {
- // XXX May need to clear whole topology first, depending on
- // how we initially subscribe to replication events
-
- for (RCSwitch sw : RCSwitch.getAllSwitches()) {
- if ( sw.getStatus() != RCSwitch.STATUS.ACTIVE ) {
- continue;
- }
- putSwitchReplicationEvent(new SwitchEvent(sw.getDpid()));
- }
-
- for (RCPort p : RCPort.getAllPorts()) {
- if (p.getStatus() != RCPort.STATUS.ACTIVE) {
- continue;
- }
- putPortReplicationEvent(new PortEvent(p.getDpid(), p.getNumber() ));
- }
-
- // TODO Is Device going to be in DB? If so, read from DB.
- // for (RCDevice d : RCDevice.getAllDevices()) {
- // DeviceEvent devEvent = new DeviceEvent( MACAddress.valueOf(d.getMac()) );
- // for (byte[] portId : d.getAllPortIds() ) {
- // devEvent.addAttachmentPoint( new SwitchPort( RCPort.getDpidFromKey(portId), RCPort.getNumberFromKey(portId) ));
- // }
- // }
-
- for (RCLink l : RCLink.getAllLinks()) {
- // check if src/dst switch/port exist before triggering event
- Port srcPort = networkGraph.getPort(l.getSrc().dpid,
- l.getSrc().number);
- Port dstPort = networkGraph.getPort(l.getDst().dpid,
- l.getDst().number);
- if ( srcPort == null || dstPort == null ) {
- continue;
- }
- putLinkReplicationEvent( new LinkEvent(l.getSrc().dpid, l.getSrc().number, l.getDst().dpid, l.getDst().number));
- }
- }
+ }
}