Squashed commit of the following:
commit e9604083c5374bf5eac889e82cf54bb0b22ef763
Author: Naoki Shiota <n-shiota@onlab.us>
Date: Wed Oct 23 11:51:00 2013 -0700
Refactored NetworkGraphPublisher and related modules to be ready for implementing event notification.
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java b/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
index ce2379b..483fbda 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/ILinkStorage.java
@@ -15,26 +15,27 @@
/*
* Generic operation method
*/
- public void update(Link link, LinkInfo linkinfo, DM_OPERATION op);
+ public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION op);
/*
* Link creation
*/
- public void addLink(Link link);
- public void addLinks(List<Link> links);
+ public boolean addLink(Link link);
+ public boolean addLink(Link link, LinkInfo linfo);
+ public boolean addLinks(List<Link> links);
/*
* Link deletion
*/
- public void deleteLink(Link link);
- public void deleteLinks(List<Link> links);
+ public boolean deleteLink(Link link);
+ public boolean deleteLinks(List<Link> links);
/*
* Utility method to delete links associated with dpid and port
* If only dpid is used, All links associated for switch are removed
* Useful for port up/down and also switch join/remove events
*/
- public void deleteLinksOnPort(Long dpid, short port);
+ public boolean deleteLinksOnPort(Long dpid, short port);
/*
* Get Links from Storage
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java b/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
index bc50049..b7825f9 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/ISwitchStorage.java
@@ -18,27 +18,29 @@
/*
* Update the switch details
*/
- public void update(String dpid,SwitchState state, DM_OPERATION op);
- /*
- * Associate a port on switch
- */
- public void addPort(String dpid, OFPhysicalPort port);
- /*
- * Delete a port on a switch by num
- */
- public void deletePort(String dpid, short port);
+ public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION op);
/*
* Add a switch and all its associated ports
*/
- public void addSwitch(IOFSwitch sw);
+ public boolean addSwitch(IOFSwitch sw);
/*
* Add a switch
*/
- public void addSwitch(String dpid);
+ public boolean addSwitch(String dpid);
/*
* Delete switch and associated ports
*/
- public void deleteSwitch(String dpid);
-
-
+ public boolean deleteSwitch(String dpid);
+ /*
+ * Update the port details
+ */
+ public boolean updatePort(String dpid, short port, int state, String desc);
+ /*
+ * Associate a port on switch
+ */
+ public boolean addPort(String dpid, OFPhysicalPort port);
+ /*
+ * Delete a port on a switch by num
+ */
+ public boolean deletePort(String dpid, short port);
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
index 753563e..da34c22 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
@@ -14,7 +14,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.thinkaurelius.titan.core.TitanException;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.PathPipe;
@@ -25,7 +24,7 @@
public class LinkStorageImpl implements ILinkStorage {
protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
- protected GraphDBOperation dbop;
+ protected GraphDBOperation op;
/**
@@ -34,37 +33,105 @@
*/
@Override
public void init(String conf) {
- this.dbop = new GraphDBOperation(conf);
+ this.op = new GraphDBOperation(conf);
}
+ // Method designing policy:
+ // op.commit() and op.rollback() MUST called in public (first-class) methods.
+ // A first-class method MUST NOT call other first-class method.
+ // Routine process should be implemented in private method.
+ // A private method MUST NOT call commit or rollback.
+
+
/**
- * Update a record in the LinkStorage in a way provided by op.
+ * Update a record in the LinkStorage in a way provided by dmop.
* @param link Record of a link to be updated.
- * @param op Operation to be done.
+ * @param linkinfo Meta-information of a link to be updated.
+ * @param dmop Operation to be done.
*/
@Override
- public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
- switch (op) {
+ public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
+ boolean success = false;
+
+ switch (dmop) {
case CREATE:
case INSERT:
- addLinkImpl(link,op);
+ if (link != null) {
+ try {
+ if (addLinkImpl(link)) {
+ op.commit();
+ success = true;
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
+ }
+ }
break;
case UPDATE:
- if (linkinfo != null) {
- setLinkInfo(link, linkinfo);
- } else {
- deleteLinkInfo(link);
+ if (link != null && linkinfo != null) {
+ try {
+ if (setLinkInfoImpl(link, linkinfo)) {
+ op.commit();
+ success = true;
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
+ }
}
break;
case DELETE:
- deleteLink(link);
+ if (link != null) {
+ try {
+ if (deleteLinkImpl(link)) {
+ op.commit();
+ success = true;
+ log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
+ } else {
+ op.rollback();
+ log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
+ }
+ }
break;
}
+
+ return success;
}
@Override
- public void addLink(Link link) {
- addLinkImpl(link, DM_OPERATION.INSERT);
+ public boolean addLink(Link link) {
+ return addLink(link, null);
+ }
+
+ @Override
+ public boolean addLink(Link link, LinkInfo linfo) {
+ boolean success = false;
+
+ try {
+ if (addLinkImpl(link)) {
+ // Set LinkInfo only if linfo is non-null.
+ if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
+ op.rollback();
+ }
+ op.commit();
+ success = true;
+ } else {
+ op.rollback();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
+ }
+
+ return success;
}
/**
@@ -73,143 +140,82 @@
* @param op Operation to be done.
*/
@Override
- public void addLinks(List<Link> links) {
+ public boolean addLinks(List<Link> links) {
+ boolean success = false;
+
for (Link lt: links) {
- addLinkImpl(lt, DM_OPERATION.INSERT);
+ if (! addLinkImpl(lt)) {
+ return false;
+ }
}
+
+ try {
+ op.commit();
+ success = true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ log.error("LinkStorageImpl:addLinks link:s{} failed", links);
+ }
+
+ return success;
}
/**
- * Delete a record in the LinkStorage.
- * @param lt Record to be deleted.
- */
- @Override
- public void deleteLink(Link lt) {
- IPortObject vportSrc = null, vportDst = null;
-
- log.debug("deleteLink(): {}", lt);
-
- try {
- // get source port vertex
- String dpid = HexString.toHexString(lt.getSrc());
- short port = lt.getSrcPort();
- vportSrc = dbop.searchPort(dpid, port);
-
- // get dst port vertex
- dpid = HexString.toHexString(lt.getDst());
- port = lt.getDstPort();
- vportDst = dbop.searchPort(dpid, port);
- // FIXME: This needs to remove all edges
-
- if (vportSrc != null && vportDst != null) {
- vportSrc.removeLink(vportDst);
- dbop.commit();
- log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
- lt, vportSrc, vportDst});
-
- // TODO publish DELETE_LINK event here
- } else {
- log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
- dbop.rollback();
- }
-
- } catch (TitanException e) {
- /*
- * retry till we succeed?
- */
- log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
- dbop.rollback();
- e.printStackTrace();
- }
- }
+ * Delete a record in the LinkStorage.
+ * @param lt Record to be deleted.
+ */
+ @Override
+ public boolean deleteLink(Link lt) {
+ boolean success = false;
+
+ log.debug("LinkStorageImpl:deleteLink(): {}", lt);
+
+ try {
+ if (deleteLinkImpl(lt)) {
+ op.commit();
+ success = true;
+ log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
+ } else {
+ op.rollback();
+ log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
+ }
+ } catch (Exception e) {
+ op.rollback();
+ log.error("LinkStorageImpl:deleteLink(): failed {} {}",
+ new Object[]{lt, e.toString()});
+ e.printStackTrace();
+ }
+
+ return success;
+ }
/**
* Delete multiple records in LinkStorage.
* @param links List of records to be deleted.
*/
@Override
- public void deleteLinks(List<Link> links) {
- for (Link lt : links) {
- deleteLink(lt);
+ public boolean deleteLinks(List<Link> links) {
+ boolean success = false;
+
+ try {
+ for (Link lt : links) {
+ if (! deleteLinkImpl(lt)) {
+ op.rollback();
+ return false;
+ }
+ }
+ op.commit();
+ success = true;
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
}
- }
-
- /**
- * Update a record of link with meta-information in the LinkStorage in a way provided by op.
- * @param link Record of a link to update.
- * @param linkinfo Meta-information of a link to be updated.
- * @param op Operation to be done.
- */
- private void setLinkInfo(Link link, LinkInfo linkinfo) {
- // TODO implement this
- // TODO publish UPDATE_LINK event here
- }
-
- private void deleteLinkInfo(Link link) {
- // TODO implement this
-
- // TODO publish UPDATE_LINK event here
+ return success;
}
/**
- * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
- * @param lt Record of a link to be updated.
- * @param linkinfo Meta-information of a link to be updated.
- * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
- */
- private void addLinkImpl(Link lt, DM_OPERATION op) {
- IPortObject vportSrc = null, vportDst = null;
-
- log.trace("updateLink(): op {} {}", new Object[]{op, lt});
-
- try {
- // get source port vertex
- String dpid = HexString.toHexString(lt.getSrc());
- short port = lt.getSrcPort();
- vportSrc = dbop.searchPort(dpid, port);
-
- // get dest port vertex
- dpid = HexString.toHexString(lt.getDst());
- port = lt.getDstPort();
- vportDst = dbop.searchPort(dpid, port);
-
- if (vportSrc != null && vportDst != null) {
- // check if the link exists
-
- Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
- List<IPortObject> currLinks = new ArrayList<IPortObject>();
- for (IPortObject V : currPorts) {
- currLinks.add(V);
- }
-
- if (currLinks.contains(vportDst)) {
- if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
- log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
- new Object[]{op, lt, vportSrc, vportDst});
- }
- } else {
- vportSrc.setLinkPort(vportDst);
-
- dbop.commit();
- log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
-
- // TODO publish ADD_LINK event here
- }
- } else {
- log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
- dbop.rollback();
- }
- } catch (TitanException e) {
- /*
- * retry till we succeed?
- */
- e.printStackTrace();
- log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
- }
- }
-
- /**
* Get list of all links connected to the port specified by given DPID and port number.
* @param dpid DPID of desired port.
* @param port Port number of desired port.
@@ -219,7 +225,7 @@
public List<Link> getLinks(Long dpid, short port) {
List<Link> links = new ArrayList<Link>();
- IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
+ IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
ISwitchObject srcSw = srcPort.getSwitch();
if(srcSw != null && srcPort != null) {
@@ -243,12 +249,27 @@
* @param port Port number of desired port.
*/
@Override
- public void deleteLinksOnPort(Long dpid, short port) {
- List<Link> linksToDelete = getLinks(dpid, port);
+ public boolean deleteLinksOnPort(Long dpid, short port) {
+ boolean success = false;
- for(Link l : linksToDelete) {
- deleteLink(l);
+ List<Link> linksToDelete = getLinks(dpid, port);
+ try {
+ for(Link l : linksToDelete) {
+ if (! deleteLinkImpl(l)) {
+ op.rollback();
+ log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
+ return false;
+ }
+ }
+ op.commit();
+ success = true;
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
}
+
+ return success;
}
/**
@@ -260,7 +281,7 @@
public List<Link> getLinks(String dpid) {
List<Link> links = new ArrayList<Link>();
- ISwitchObject srcSw = dbop.searchSwitch(dpid);
+ ISwitchObject srcSw = op.searchSwitch(dpid);
if(srcSw != null) {
for(IPortObject srcPort : srcSw.getPorts()) {
@@ -285,7 +306,7 @@
* @return List of active links. Empty list if no port was found.
*/
public List<Link> getActiveLinks() {
- Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
+ Iterable<ISwitchObject> switches = op.getActiveSwitches();
List<Link> links = new ArrayList<Link>();
@@ -329,6 +350,79 @@
// graph.shutdown();
}
+ /**
+ * Update a record of link with meta-information in the LinkStorage.
+ * @param link Record of a link to update.
+ * @param linkinfo Meta-information of a link to be updated.
+ */
+ private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
+ // TODO implement this
+
+ return false;
+ }
+
+ private boolean addLinkImpl(Link lt) {
+ boolean success = false;
+
+ IPortObject vportSrc = null, vportDst = null;
+
+ // get source port vertex
+ String dpid = HexString.toHexString(lt.getSrc());
+ short port = lt.getSrcPort();
+ vportSrc = op.searchPort(dpid, port);
+
+ // get dest port vertex
+ dpid = HexString.toHexString(lt.getDst());
+ port = lt.getDstPort();
+ vportDst = op.searchPort(dpid, port);
+
+ if (vportSrc != null && vportDst != null) {
+ IPortObject portExist = null;
+ // check if the link exists
+ for (IPortObject V : vportSrc.getLinkedPorts()) {
+ if (V.equals(vportDst)) {
+ portExist = V;
+ break;
+ }
+ }
+
+ if (portExist == null) {
+ vportSrc.setLinkPort(vportDst);
+ success = true;
+ } else {
+ log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
+ new Object[]{op, lt, vportSrc, vportDst});
+ }
+ }
+
+ return success;
+ }
+
+ private boolean deleteLinkImpl(Link lt) {
+ boolean success = false;
+ IPortObject vportSrc = null, vportDst = null;
+
+ // get source port vertex
+ String dpid = HexString.toHexString(lt.getSrc());
+ short port = lt.getSrcPort();
+ vportSrc = op.searchPort(dpid, port);
+
+ // get dst port vertex
+ dpid = HexString.toHexString(lt.getDst());
+ port = lt.getDstPort();
+ vportDst = op.searchPort(dpid, port);
+
+ // FIXME: This needs to remove all edges
+ if (vportSrc != null && vportDst != null) {
+ vportSrc.removeLink(vportDst);
+ log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{
+ lt, vportSrc, vportDst});
+ success = true;
+ }
+
+ return success;
+ }
+
// TODO should be moved to TopoLinkServiceImpl (never used in this class)
static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
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 d9f24e0..8399d6f 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,8 +1,5 @@
package net.onrc.onos.ofcontroller.core.internal;
-import java.util.ArrayList;
-import java.util.List;
-
import net.floodlightcontroller.core.IOFSwitch;
import net.onrc.onos.graph.GraphDBConnection;
import net.onrc.onos.graph.GraphDBOperation;
@@ -17,7 +14,7 @@
import org.slf4j.LoggerFactory;
/**
- * This is the class for storing the information of switches into CassandraDB
+ * This is the class for storing the information of switches into GraphDB
*/
public class SwitchStorageImpl implements ISwitchStorage {
protected GraphDBOperation op;
@@ -50,23 +47,12 @@
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);
- // TODO publish UPDATE_SWITCH event here
- }
- } catch(Exception e) {
- e.printStackTrace();
- op.rollback();
- log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
- }
- }
-
+ // Method designing policy:
+ // op.commit() and op.rollback() MUST called in public (first-class) methods.
+ // A first-class method MUST NOT call other first-class method.
+ // Routine process should be implemented in private method.
+ // A private method MUST NOT call commit or rollback.
+
/***
* This function is for updating the switch into the DB.
* @param dpid The switch dpid you want to update from the DB
@@ -74,71 +60,221 @@
* @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);
+ public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
+ boolean success = false;
+ ISwitchObject sw = null;
+
+ log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
switch(dmope) {
case UPDATE:
+ try {
+ sw = op.searchSwitch(dpid);
+ if (sw != null) {
+ setSwitchStateImpl(sw, state);
+ op.commit();
+ success = true;
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
+ }
+ break;
case INSERT:
case CREATE:
- addSwitch(dpid);
- if (state != SwitchState.ACTIVE) {
- setStatus(dpid, state);
- }
+ try {
+ sw = addSwitchImpl(dpid);
+ if (sw != null) {
+ if (state != SwitchState.ACTIVE) {
+ setSwitchStateImpl(sw, state);
+ }
+ op.commit();
+ success = true;
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
+ }
break;
case DELETE:
- deleteSwitch(dpid);
+ try {
+ sw = op.searchSwitch(dpid);
+ if (sw != null) {
+ deleteSwitchImpl(sw);
+ op.commit();
+ success = true;
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
+ }
break;
default:
}
+
+ return success;
+ }
+
+ @Override
+ public boolean addSwitch(IOFSwitch sw) {
+ boolean success = false;
+
+ String dpid = sw.getStringId();
+ log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
+
+ try {
+ ISwitchObject curr = op.searchSwitch(dpid);
+ if (curr != null) {
+ //If existing the switch. set The SW state ACTIVE.
+ log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
+ setSwitchStateImpl(curr, SwitchState.ACTIVE);
+ } else {
+ curr = addSwitchImpl(dpid);
+ }
+
+ for (OFPhysicalPort port: sw.getPorts()) {
+ IPortObject p = op.searchPort(dpid, port.getPortNumber());
+ if (p != null) {
+ log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
+ setPortStateImpl(p, port.getState(), port.getName());
+ p.setState("ACTIVE");
+ } else {
+ p = addPortImpl(curr, port.getPortNumber());
+ setPortStateImpl(p, port.getState(), port.getName());
+ }
+ }
+ op.commit();
+ success = true;
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("SwitchStorage:addSwitch dpid:{} failed", dpid);
+ }
+
+ return success;
+ }
+
+ /***
+ * This function is for adding the switch into the DB.
+ * @param dpid The switch dpid you want to add into the DB.
+ */
+ @Override
+ public boolean addSwitch(String dpid) {
+ boolean success = false;
+
+ 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);
+ setSwitchStateImpl(sw, SwitchState.ACTIVE);
+ } else {
+ addSwitchImpl(dpid);
+ }
+ op.commit();
+ success = true;
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
+ }
+
+ return success;
+ }
+
+ /***
+ * This function is for deleting the switch into the DB.
+ * @param dpid The switch dpid you want to delete from the DB.
+ */
+ @Override
+ public boolean deleteSwitch(String dpid) {
+ boolean success = false;
+
+ try {
+ ISwitchObject sw = op.searchSwitch(dpid);
+ if (sw != null) {
+ deleteSwitchImpl(sw);
+ op.commit();
+ }
+ success = true;
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("SwitchStorage:deleteSwitch {} failed", dpid);
+ }
+
+ return success;
+ }
+
+ public boolean updatePort(String dpid, short portNum, int state, String desc) {
+ boolean success = false;
+
+ try {
+ ISwitchObject sw = op.searchSwitch(dpid);
+
+ if (sw != null) {
+ IPortObject p = sw.getPort(portNum);
+ log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
+ if (p != null) {
+ setPortStateImpl(p, state, desc);
+ }
+ op.commit();
+ success = true;
+ } else {
+ log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
+ }
+ } catch (Exception e) {
+ op.rollback();
+ e.printStackTrace();
+ log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
+ }
+
+ return success;
}
/***
* 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.
+ * @param phport The port you want to add the switch.
*/
@Override
- public void addPort(String dpid, OFPhysicalPort port) {
+ public boolean addPort(String dpid, OFPhysicalPort phport) {
+ boolean success = false;
- if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
- ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) {
- deletePort(dpid, port.getPortNumber());
- return;
- }
+ if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
+ ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
+ // just dispatch to deletePort()
+ return deletePort(dpid, phport.getPortNumber());
+ }
try {
ISwitchObject sw = op.searchSwitch(dpid);
if (sw != null) {
- IPortObject p = op.searchPort(dpid, port.getPortNumber());
- log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
+ IPortObject p = sw.getPort(phport.getPortNumber());
+ log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, phport.getPortNumber());
if (p != null) {
- log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, port.getPortNumber());
- p.setState("ACTIVE");
- p.setPortState(port.getState());
- p.setDesc(port.getName());
- op.commit();
-
- // TODO publish UPDATE_PORT event here
+ setPortStateImpl(p, phport.getState(), phport.getName());
+ log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, phport.getPortNumber());
} else {
- p = op.newPort(dpid, port.getPortNumber());
- p.setState("ACTIVE");
- p.setPortState(port.getState());
- p.setDesc(port.getName());
- sw.addPort(p);
- op.commit();
-
- // TODO publish ADD_PORT event here
+ addPortImpl(sw, phport.getPortNumber());
+ setPortStateImpl(p, phport.getState(), phport.getName());
}
+ op.commit();
+ success = true;
} else {
- log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
+ log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
}
} catch (Exception e) {
- e.printStackTrace();
op.rollback();
- log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
+ e.printStackTrace();
+ log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
}
-
+
+ return success;
}
/***
@@ -147,111 +283,36 @@
* @param port The port you want to delete the switch.
*/
@Override
- public void deletePort(String dpid, short port) {
+ public boolean deletePort(String dpid, short port) {
+ boolean success = false;
+
try {
ISwitchObject sw = op.searchSwitch(dpid);
if (sw != null) {
- IPortObject p = op.searchPort(dpid, port);
+ IPortObject p = sw.getPort(port);
if (p != null) {
log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
- p.setState("INACTIVE");
+ deletePortImpl(p);
op.commit();
- // TODO publish DELETE_PORT event here
}
}
} catch (Exception e) {
- e.printStackTrace();
op.rollback();
+ e.printStackTrace();
log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
- }
- }
-
- @Override
- public void addSwitch(IOFSwitch sw) {
- String dpid = sw.getStringId();
- log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
-
- ISwitchObject curr = op.searchSwitch(dpid);
- if (curr != null) {
- //If existing the switch. set The SW state ACTIVE.
- log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
- setSwitchStateImpl(curr, SwitchState.ACTIVE);
- } else {
- curr = addSwitchImpl(dpid);
}
- try {
- List<IPortObject> added_ports = new ArrayList<IPortObject>();
- List<IPortObject> updated_ports = new ArrayList<IPortObject>();
- for (OFPhysicalPort port: sw.getPorts()) {
- IPortObject p = op.searchPort(dpid, port.getPortNumber());
- if (p != null) {
- log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
- p.setState("ACTIVE");
- p.setPortState(port.getState());
- p.setDesc(port.getName());
-
- added_ports.add(p);
- } else {
- p = op.newPort(dpid, port.getPortNumber());
- p.setState("ACTIVE");
- p.setPortState(port.getState());
- p.setDesc(port.getName());
- curr.addPort(p);
-
- updated_ports.add(p);
- }
- }
-
- op.commit();
-
- // TODO publish ADD_PORT event here
- // TODO publish UPDATE_PORT event here
- } catch (Exception e) {
- e.printStackTrace();
- op.rollback();
- log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
- }
+ return success;
}
- /***
- * 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);
-
- 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);
- setSwitchStateImpl(sw, SwitchState.ACTIVE);
- } else {
- addSwitchImpl(dpid);
- }
- }
-
private ISwitchObject addSwitchImpl(String dpid) {
- try {
+ if (dpid != null) {
ISwitchObject sw = op.newSwitch(dpid);
- if (sw != null) {
- sw.setState(SwitchState.ACTIVE.toString());
- log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
- } else {
- log.error("switchStorage:addSwitchImpl dpid:{} failed", dpid);
- throw new RuntimeException();
- }
-
- op.commit();
-
- // TODO publish ADD_SWITCH event here
- return sw;
- } catch (Exception e) {
- e.printStackTrace();
- op.rollback();
- log.info("SwitchStorage:addSwitchImpl dpid:{} failed", dpid);
+ sw.setState(SwitchState.ACTIVE.toString());
+ log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
+ return sw;
+ } else {
return null;
}
}
@@ -259,33 +320,48 @@
private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
if (sw != null && state != null) {
sw.setState(state.toString());
- op.commit();
-
- // TODO publish UPDATE_SWITCH event here
+ log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
+ sw.getDPID(), state.toString());
}
}
-
- /***
- * 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);
-
- // TODO publish DELETE_SWITCH event here
- }
- } catch (Exception e) {
- e.printStackTrace();
- op.rollback();
- log.error("SwitchStorage:deleteSwitch {} failed", dpid);
- }
-
- }
+ private void deleteSwitchImpl(ISwitchObject sw) {
+ if (sw != null) {
+ op.removeSwitch(sw);
+ log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
+ sw.getDPID());
+ }
+ }
+
+ private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
+ IPortObject p = op.newPort(sw.getDPID(), portNum);
+ p.setState("ACTIVE");
+ sw.addPort(p);
+ log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
+ sw.getDPID(), portNum);
+
+ return p;
+ }
+
+ private void setPortStateImpl(IPortObject port, Integer state, String desc) {
+ if (port != null) {
+ if (state != null) {
+ port.setPortState(state);
+ }
+ if (desc != null) {
+ port.setDesc(desc);
+ }
+
+ log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
+ new Object[] {port.getPortId(), state, desc});
+ }
+ }
+
+ private void deletePortImpl(IPortObject port) {
+ if (port != null) {
+ op.removePort(port);
+ log.info("SwitchStorage:deletePortImpl port:{} done",
+ port.getPortId());
+ }
+ }
}
\ No newline at end of file