package net.floodlightcontroller.linkdiscovery.internal;

import java.util.ArrayList;
import java.util.List;

import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
import net.floodlightcontroller.core.INetMapTopologyService.ITopoLinkService;
import net.floodlightcontroller.linkdiscovery.internal.LinkStorageImpl.ExtractLink;
import net.floodlightcontroller.routing.Link;
import net.onrc.onos.util.GraphDBConnection;
import net.onrc.onos.util.GraphDBConnection.Transaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;

public class TopoLinkServiceImpl implements ITopoLinkService {
	
	public GraphDBConnection conn;
	protected static Logger log = LoggerFactory.getLogger(TopoLinkServiceImpl.class);


	public void finalize() {
		close();
	}
	
	@Override
	public void close() {
		conn.close();
	}
 
	@Override
	public List<Link> getActiveLinks() {
		// TODO Auto-generated method stub
		conn = GraphDBConnection.getInstance("");
		conn.close(); //Commit to ensure we see latest data
		Iterable<ISwitchObject> switches = conn.utils().getActiveSwitches(conn);
		List<Link> links = new ArrayList<Link>(); 
		for (ISwitchObject sw : switches) {
			GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
			ExtractLink extractor = new ExtractLink();

			pipe.start(sw.asVertex());
			pipe.enablePath(true);
			pipe.out("on").out("link").in("on").path().step(extractor);
					
			while (pipe.hasNext() ) {
				Link l = pipe.next();
				links.add(l);
			}
						
		}
		conn.endTx(Transaction.COMMIT);
		return links;
	}

	@Override
	public List<Link> getLinksOnSwitch(String dpid) {
		// TODO Auto-generated method stub
		List<Link> links = new ArrayList<Link>(); 
		ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
		GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
		ExtractLink extractor = new ExtractLink();

		pipe.start(sw.asVertex());
		pipe.enablePath(true);
		pipe.out("on").out("link").in("on").path().step(extractor);
			
		while (pipe.hasNext() ) {
			Link l = pipe.next();
			links.add(l);
		}
		return links;

	}
	
}

@@ -468,26 +468,13 @@
 phDB
-Connection.Transac
+Opera
 tion
@@ -697,21 +697,24 @@
 %09%0A%09p
-ublic
+rotected
  GraphDB
 Conn
@@ -713,23 +713,20 @@
 phDB
-Connec
+Opera
 tion 
-conn
+op
 ;%0A%09p
@@ -807,17 +807,16 @@
 lass);%0A%0A
-%0A
 %09public 
@@ -885,20 +885,18 @@
 e() %7B%0A%09%09
-conn
+op
 .close()
@@ -994,15 +994,34 @@
 b%0A%09%09
-conn = 
+op = new GraphDBOperation(
 Grap
@@ -1053,16 +1053,15 @@
 (%22%22)
+)
 ;%0A%09%09
-conn
+op
 .clo
@@ -1141,28 +1141,18 @@
 tches = 
-conn.utils()
+op
 .getActi
@@ -1162,20 +1162,16 @@
 witches(
-conn
 );%0A%09%09Lis
@@ -1592,37 +1592,18 @@
 %7D%0A%09%09
-conn.endTx(Transaction.COMMIT
+op.commit(
 );%0A%09
@@ -1790,20 +1790,10 @@
 w = 
-conn.utils()
+op
 .sea
@@ -1806,14 +1806,8 @@
 tch(
-conn, 
 dpid

refactor TopoLinkServiceImpl class to use GraphDBOperation class.