Add the switchStorageImpl unit tests.
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
index ea7fdf0..ec0c993 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
@@ -1,7 +1,5 @@
package net.onrc.onos.ofcontroller.core.internal;
-import java.util.Collection;
-
import net.onrc.onos.ofcontroller.core.ISwitchStorage;
import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
@@ -14,16 +12,99 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * This is the class for storing the information of switches into CassandraDB
+ */
public class SwitchStorageImpl implements ISwitchStorage {
protected GraphDBOperation op;
protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
-
+
+ /***
+ * Initialize function. Before you use this class, please call this method
+ * @param conf configuration file for Cassandra DB
+ */
@Override
- public void update(String dpid, SwitchState state, DM_OPERATION op) {
- // TODO Auto-generated method stub
- log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
- switch(op) {
+ public void init(String conf) {
+ GraphDBConnection conn = GraphDBConnection.getInstance(conf);
+ op = new GraphDBOperation(conn);
+ }
+ /***
+ * Finalize/close function. After you use this class, please call this method.
+ * It will close the DB connection.
+ */
+ public void finalize() {
+ close();
+ }
+
+ /***
+ * Finalize/close function. After you use this class, please call this method.
+ * It will close the DB connection. This is for Java gabage collection.
+ */
+ @Override
+ public void close() {
+ op.close();
+ }
+
+ private void setStatus(String dpid, SwitchState state) {
+ ISwitchObject sw = op.searchSwitch(dpid);
+
+ try {
+ if (sw != null) {
+ sw.setState(state.toString());
+ op.commit();
+ log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
+ }
+ } catch(Exception e) {
+ e.printStackTrace();
+ op.rollback();
+ log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
+ }
+ }
+
+ /***
+ * This function is for adding the switch into the DB.
+ * @param dpid The switch dpid you want to add into the DB.
+ */
+ @Override
+ public void addSwitch(String dpid) {
+
+ log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
+ try {
+ ISwitchObject sw = op.searchSwitch(dpid);
+ if (sw != null) {
+ //If existing the switch. set The SW state ACTIVE.
+ log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
+ sw.setState(SwitchState.ACTIVE.toString());
+ op.commit();
+ } else {
+ sw = op.newSwitch(dpid);
+
+ if (sw != null) {
+ sw.setState(SwitchState.ACTIVE.toString());
+ op.commit();
+ log.info("SwitchStorage:addSwitch dpid:{} added", dpid);
+ } else {
+ log.error("switchStorage:addSwitch dpid:{} failed -> newSwitch failed", dpid);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ op.rollback();
+ log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
+ }
+ }
+
+ /***
+ * This function is for updating the switch into the DB.
+ * @param dpid The switch dpid you want to update from the DB
+ * @param state The state of the switch like ACTIVE, INACTIVE
+ * @param dmope The DM_OPERATION of the switch
+ */
+ @Override
+ public void update(String dpid, SwitchState state, DM_OPERATION dmope) {
+ log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
+ switch(dmope) {
case UPDATE:
case INSERT:
case CREATE:
@@ -39,29 +120,41 @@
}
}
- private void setStatus(String dpid, SwitchState state) {
- ISwitchObject sw = op.searchSwitch(dpid);
- if (sw != null) {
- sw.setState(state.toString());
- op.commit();
- log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
- } else {
- op.rollback();
- log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
+ /***
+ * This function is for deleting the switch into the DB.
+ * @param dpid The switch dpid you want to delete from the DB.
+ */
+ @Override
+ public void deleteSwitch(String dpid) {
+ try {
+ ISwitchObject sw = op.searchSwitch(dpid);
+ if (sw != null) {
+ op.removeSwitch(sw);
+ op.commit();
+ log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ op.rollback();
+ log.error("SwitchStorage:deleteSwitch {} failed", dpid);
}
+
}
+ /***
+ * This function is for adding the switch port into the DB.
+ * @param dpid The switch dpid that has the port.
+ * @param port The port you want to add the switch.
+ */
@Override
public void addPort(String dpid, OFPhysicalPort port) {
- // TODO Auto-generated method stub
- boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
- ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0);
- if (portDown) {
- deletePort(dpid, port.getPortNumber());
- return;
+ if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
+ ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) {
+ deletePort(dpid, port.getPortNumber());
+ return;
}
-
+
try {
ISwitchObject sw = op.searchSwitch(dpid);
@@ -82,95 +175,20 @@
log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
}
} catch (Exception e) {
- // TODO: handle exceptions
e.printStackTrace();
op.rollback();
log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
}
}
-
- @Override
- public Collection<OFPhysicalPort> getPorts(long dpid) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public OFPhysicalPort getPort(String dpid, short portnum) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public OFPhysicalPort getPort(String dpid, String portName) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public void addSwitch(String dpid) {
-
- log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
-
- try {
- ISwitchObject sw = op.searchSwitch(dpid);
- if (sw != null) {
- /*
- * Do nothing or throw exception?
- */
-
- log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
- sw.setState(SwitchState.ACTIVE.toString());
- op.commit();
- } else {
- sw = op.newSwitch(dpid);
-
- if (sw != null) {
- sw.setState(SwitchState.ACTIVE.toString());
- op.commit();
- log.info("SwitchStorage:addSwitch dpid:{} added", dpid);
- } else {
- log.error("switchStorage:addSwitch dpid:{} failed -> newSwitch failed", dpid);
- }
- }
- } catch (Exception e) {
- /*
- * retry?
- */
- e.printStackTrace();
- op.rollback();
- log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
- }
-
-
- }
-
- @Override
- public void deleteSwitch(String dpid) {
- // TODO Setting inactive but we need to eventually remove data
-
- try {
-
- ISwitchObject sw = op.searchSwitch(dpid);
- if (sw != null) {
- op.removeSwitch(sw);
-
- op.commit();
- log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
- }
- } catch (Exception e) {
- // TODO: handle exceptions
- e.printStackTrace();
- op.rollback();
- log.error("SwitchStorage:deleteSwitch {} failed", dpid);
- }
-
- }
-
+
+ /***
+ * This function is for deleting the switch port from the DB.
+ * @param dpid The switch dpid that has the port.
+ * @param port The port you want to delete the switch.
+ */
@Override
public void deletePort(String dpid, short port) {
- // TODO Auto-generated method stub
try {
ISwitchObject sw = op.searchSwitch(dpid);
@@ -184,37 +202,9 @@
}
}
} catch (Exception e) {
- // TODO: handle exceptions
e.printStackTrace();
op.rollback();
log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
}
}
-
- @Override
- public void deletePort(String dpid, String portName) {
- // TODO Auto-generated method stub
-
- }
-
-
-
- @Override
- public void init(String conf) {
- GraphDBConnection conn = GraphDBConnection.getInstance(conf);
- op = new GraphDBOperation(conn);
- }
-
-
-
- public void finalize() {
- close();
- }
-
- @Override
- public void close() {
- op.close();
- }
-
-
}