Merge branch 'original' into netgraphrefactor

Conflicts:
	src/main/java/net/onrc/onos/ofcontroller/core/internal/LinkStorageImpl.java
diff --git a/README.md b/README.md
index ea1553a..bfd8d57 100644
--- a/README.md
+++ b/README.md
@@ -17,11 +17,18 @@
 
     $ mvn clean
     $ mvn compile
-     
+
+
+Dependencies
+------------
+1. Zookeeper
+    Download and install apache-zookeeper-3.4.5: http://zookeeper.apache.org/releases.html
+2. Cassandra
+    Download and install apache-cassandra-1.2.2: http://cassandra.apache.org/download/
+
 Running ONOS
 ------------
 
-
 1. Start zookeeper
 
     $ cd (ZOOKEEPER-INSTALL-DIR)/bin
@@ -38,8 +45,30 @@
 
 3. Start ONOS instance
 
+    $ cd (ONOS-INSTALL-DIR)/
+
     $ ./start-onos.sh start
     
 4. Start ONOS rest apis
 
     $ ./start-rest.sh start
+
+Running ONOS with Cassandra embedded (Optional)
+-----------------------------------------------
+
+1. Start Zookeeper
+
+    $ cd (ZOOKEEPER-INSTALL-DIR)/bin
+    
+    $ ./zkServer.sh start
+    
+2. Start ONOS and Cassandra embedded
+
+    $ cd (ONOS-INSTAL_DIR)/
+    
+    $ ./start-onos-embedded.sh start
+    
+3. Start ONOS rest apis
+
+    $ ./start-rest.sh start
+
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
index 5b3ed74..dcb28ce 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
@@ -14,8 +14,7 @@
 import net.onrc.onos.ofcontroller.core.IDeviceStorage;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
-import net.onrc.onos.util.GraphDBConnection;
-import net.onrc.onos.util.GraphDBConnection.Transaction;
+import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
 import net.onrc.onos.util.GraphDBOperation;
 
 /**
@@ -24,7 +23,6 @@
  */
 public class DeviceStorageImpl implements IDeviceStorage {
 	
-	private GraphDBConnection conn;
 	private GraphDBOperation ope;
 	protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
 
@@ -35,10 +33,7 @@
 	@Override
 	public void init(String conf) {
 		try{
-			if((conn = GraphDBConnection.getInstance(conf)) != null)
-			{
-				ope = new GraphDBOperation(conn);
-			}
+			ope = new GraphDBOperation(conf);
 		} catch(Exception e) {
 			log.error(e.getMessage());
 		}
@@ -50,7 +45,7 @@
 	 */
 	@Override
 	public void close() {
-		conn.close();
+		ope.close();
 	}
 	
 	/***
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 2eaf535..b33591e 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
@@ -8,7 +8,6 @@
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.openflow.util.HexString;
@@ -227,7 +226,7 @@
 	@Override
 	public void init(String conf) {
 		//TODO extract the DB location from properties
-		this.dbop = new GraphDBOperation(GraphDBConnection.getInstance(conf));
+		this.dbop = new GraphDBOperation(conf);
 	}
 
 	/**
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..2e9ea60 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
@@ -5,7 +5,6 @@
 import net.onrc.onos.ofcontroller.core.ISwitchStorage;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.openflow.protocol.OFPhysicalPort;
@@ -201,8 +200,7 @@
 
 	@Override
 	public void init(String conf) {
-		GraphDBConnection conn = GraphDBConnection.getInstance(conf);
-		op = new GraphDBOperation(conn);
+		op = new GraphDBOperation(conf);
 	}
 
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
index 4a0c35c..1222ff3 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
@@ -7,7 +7,6 @@
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
 import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl.ExtractLink;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.slf4j.Logger;
@@ -33,8 +32,8 @@
 	@Override
 	public List<Link> getActiveLinks() {
 		// TODO Auto-generated method stub
-		op = new GraphDBOperation(GraphDBConnection.getInstance(""));
-		op.close(); //Commit to ensure we see latest data
+		op = new GraphDBOperation("");
+		op.commit(); //Commit to ensure we see latest data
 		Iterable<ISwitchObject> switches = op.getActiveSwitches();
 		List<Link> links = new ArrayList<Link>(); 
 		for (ISwitchObject sw : switches) {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoSwitchServiceImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoSwitchServiceImpl.java
index e279422..c1659fb 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoSwitchServiceImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoSwitchServiceImpl.java
@@ -3,7 +3,6 @@
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.slf4j.Logger;
@@ -15,7 +14,7 @@
 	protected static Logger log = LoggerFactory.getLogger(TopoSwitchServiceImpl.class);
 
 	public TopoSwitchServiceImpl(String conf) {
-		op = new GraphDBOperation(GraphDBConnection.getInstance(conf));
+		op = new GraphDBOperation(conf);
 	}
 
 	public TopoSwitchServiceImpl() {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/devicemanager/web/TopoDevicesResource.java b/src/main/java/net/onrc/onos/ofcontroller/devicemanager/web/TopoDevicesResource.java
index 1cd6b90..97ecfcc 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/devicemanager/web/TopoDevicesResource.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/devicemanager/web/TopoDevicesResource.java
@@ -3,7 +3,6 @@
 import java.util.Iterator;
 
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.restlet.resource.Get;
@@ -13,12 +12,8 @@
 	
 	@Get("json")
 	public Iterator<IDeviceObject> retrieve() {
-		
-		GraphDBConnection conn = GraphDBConnection.getInstance("");
-		GraphDBOperation op = new GraphDBOperation(conn);
+		GraphDBOperation op = new GraphDBOperation("");
 		
 		return op.getDevices().iterator();
-		
 	}
-	
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java b/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
index 9a382f2..bb7318f 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/floodlightlistener/NetworkGraphPublisher.java
@@ -252,7 +252,7 @@
 		// TODO Auto-generated method stub
 		Map<String, String> configMap = context.getConfigParams(this);
 		String conf = configMap.get(DBConfigFile);
-		op = new GraphDBOperation(GraphDBConnection.getInstance(conf));
+		op = new GraphDBOperation(conf);
 		
 		log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
 		floodlightProvider =
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
index 3b566d1..2dc230c 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
@@ -49,7 +49,6 @@
 import net.onrc.onos.ofcontroller.util.IPv4Net;
 import net.onrc.onos.ofcontroller.util.Port;
 import net.onrc.onos.ofcontroller.util.SwitchPort;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.openflow.protocol.OFFlowMod;
@@ -413,7 +412,7 @@
 
     @Override
     public void init(String conf) {
-    	op = new GraphDBOperation(GraphDBConnection.getInstance(conf));
+    	op = new GraphDBOperation(conf);
     }
 
     public void finalize() {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/routing/TopoRouteService.java b/src/main/java/net/onrc/onos/ofcontroller/routing/TopoRouteService.java
index 864bb74..0184915 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/routing/TopoRouteService.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/routing/TopoRouteService.java
@@ -23,7 +23,6 @@
 import net.onrc.onos.ofcontroller.util.FlowEntry;
 import net.onrc.onos.ofcontroller.util.Port;
 import net.onrc.onos.ofcontroller.util.SwitchPort;
-import net.onrc.onos.util.GraphDBConnection;
 import net.onrc.onos.util.GraphDBOperation;
 
 import org.openflow.util.HexString;
@@ -133,7 +132,7 @@
     public void init(FloodlightModuleContext context)
 	throws FloodlightModuleException {
 	// TODO: Add the appropriate initialization
-    	op = new GraphDBOperation(GraphDBConnection.getInstance(""));
+    	op = new GraphDBOperation("");
     }
 
     @Override
diff --git a/src/main/java/net/onrc/onos/util/GraphDBConnection.java b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
index e25a0c6..9a50f47 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBConnection.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBConnection.java
@@ -80,7 +80,6 @@
 	}
 
 	public FramedGraph<TitanGraph> getFramedGraph() {
-
 		if (isValid()) {
 			FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);
 			return fg;
@@ -91,7 +90,6 @@
 	}
 
 	protected EventTransactionalGraph<TitanGraph> getEventGraph() {
-
 		if (isValid()) {
 			return eg;
 		} else {
@@ -106,65 +104,28 @@
 	}
 
 	public Boolean isValid() {
-
 		return (graph != null || graph.isOpen());
 	}
 
-	public void startTx() {
-
-	}
-
-	public void endTx(Transaction tx) {
+	public void commit() {
 		try {
-			switch (tx) {
-			case COMMIT:
-				graph.commit();
-			case ROLLBACK:
-				graph.rollback();
-			}
-		} catch (Exception e) {
-			// TODO Auto-generated catch block
+			graph.commit();
+		}
+		catch (Exception e) {
 			log.error("{}", e.toString());
 		}
 	}
 
-	public void endTx(TransactionHandle tr, Transaction tx) {
-		switch (tx) {
-		case COMMIT:
-			if (tr != null && tr.tr != null) {
-				tr.tr.commit();
-			} else {
-				graph.commit();
-			}
-		case ROLLBACK:
-			if (tr != null && tr.tr != null) {
-				tr.tr.rollback();
-			} else {
-				graph.rollback();
-			}
-		}
-	}
-
-	public void endTx(Transaction tx, GenerateEvent fire) {
-
+	public void rollback() {
 		try {
-			if (fire.equals(GenerateEvent.TRUE)) {
-				switch (tx) {
-				case COMMIT:
-					eg.commit();
-				case ROLLBACK:
-					eg.rollback();
-				}
-			} else {
-				endTx(tx);
-			}
-		} catch (Exception e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+			graph.rollback();
+		}
+		catch (Exception e) {
+			log.error("{}", e.toString());
 		}
 	}
 
 	public void close() {
-		endTx(Transaction.COMMIT);
+		commit();
 	}
 }
diff --git a/src/main/java/net/onrc/onos/util/GraphDBOperation.java b/src/main/java/net/onrc/onos/util/GraphDBOperation.java
index 5a70dbc..da41dbd 100644
--- a/src/main/java/net/onrc/onos/util/GraphDBOperation.java
+++ b/src/main/java/net/onrc/onos/util/GraphDBOperation.java
@@ -12,16 +12,6 @@
 import net.onrc.onos.ofcontroller.util.FlowEntryId;
 import net.onrc.onos.ofcontroller.util.FlowId;
 
-//import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
-//import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
-//import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
-//import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
-//import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
-//import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
-//import net.floodlightcontroller.util.FlowEntryId;
-//import net.floodlightcontroller.util.FlowId;
-import net.onrc.onos.util.GraphDBConnection.Transaction;
-
 import com.thinkaurelius.titan.core.TitanGraph;
 import com.tinkerpop.blueprints.Vertex;
 import com.tinkerpop.frames.FramedGraph;
@@ -34,6 +24,10 @@
 	public GraphDBOperation(GraphDBConnection dbConnection) {
 		this.conn = dbConnection;
 	}
+	
+	public GraphDBOperation(final String dbConfPath) {
+		this.conn = GraphDBConnection.getInstance(dbConfPath);
+	}
 
 	@Override
 	public ISwitchObject newSwitch(String dpid) {
@@ -209,7 +203,6 @@
 
 	@Override
 	public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
-		FramedGraph<TitanGraph> fg = conn.getFramedGraph();
 		GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
 		pipe.start(flowEntry.asVertex());
 		pipe.out("flow");
@@ -273,11 +266,11 @@
 	}
 	
 	public void commit() {
-		conn.endTx(Transaction.COMMIT);
+		conn.commit();
 	}
 	
 	public void rollback() {
-		conn.endTx(Transaction.ROLLBACK);
+		conn.rollback();
 	}
 
 	public void close() {
diff --git a/src/main/java/net/onrc/onos/util/IDBConnection.java b/src/main/java/net/onrc/onos/util/IDBConnection.java
index e599a5e..995ce39 100644
--- a/src/main/java/net/onrc/onos/util/IDBConnection.java
+++ b/src/main/java/net/onrc/onos/util/IDBConnection.java
@@ -1,9 +1,5 @@
 package net.onrc.onos.util;
 
-import net.onrc.onos.util.GraphDBConnection.GenerateEvent;
-import net.onrc.onos.util.GraphDBConnection.Transaction;
-import net.onrc.onos.util.GraphDBConnection.TransactionHandle;
-
 import com.thinkaurelius.titan.core.TitanGraph;
 import com.tinkerpop.frames.FramedGraph;
 
@@ -11,9 +7,7 @@
 	public FramedGraph<TitanGraph> getFramedGraph();
 	public void addEventListener(final LocalGraphChangedListener listener);
 	public Boolean isValid();
-	public void startTx();
-	public void endTx(Transaction tx);
-	public void endTx(TransactionHandle tr, Transaction tx);
-	public void endTx(Transaction tx, GenerateEvent fire);
+	public void commit();
+	public void rollback();
 	public void close();
 }
diff --git a/src/main/java/net/onrc/onos/util/IDBOperation.java b/src/main/java/net/onrc/onos/util/IDBOperation.java
index 46245ae..335b08b 100644
--- a/src/main/java/net/onrc/onos/util/IDBOperation.java
+++ b/src/main/java/net/onrc/onos/util/IDBOperation.java
@@ -8,7 +8,7 @@
 import net.onrc.onos.ofcontroller.util.FlowEntryId;
 import net.onrc.onos.ofcontroller.util.FlowId;
 
-public interface IDBOperation {	
+public interface IDBOperation {
 	public ISwitchObject newSwitch(String dpid);
 	public ISwitchObject searchSwitch(String dpid);
 	public ISwitchObject searchActiveSwitch(String dpid);
diff --git a/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java b/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java
index 294c043..223ac67 100644
--- a/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java
+++ b/src/test/java/net/onrc/onos/util/GraphDBOperationTest.java
@@ -43,7 +43,6 @@
 @PrepareForTest({TitanFactory.class})
 public class GraphDBOperationTest extends TestCase {
 	private static TitanGraph testdb;
-	private static GraphDBConnection conn;
 	private static GraphDBOperation op;
 
 	/**
@@ -75,8 +74,7 @@
 		EasyMock.expect(TitanFactory.open(dummyPath)).andReturn(testdb);
 		PowerMock.replay(TitanFactory.class);
 		
-		conn = GraphDBConnection.getInstance(dummyPath);
-		op = new GraphDBOperation(conn);
+		op = new GraphDBOperation(dummyPath);
 	}
 
 	/**
@@ -84,7 +82,7 @@
 	 */
 	@After
 	public void tearDown() throws Exception {
-		conn.close();
+		op.close();
 		testdb.shutdown();
 		PowerMock.verifyAll();
 	}