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;

	}
	
}

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.GraphDBOperation;

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

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

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

	public void finalize() {
		close();
	}
	
	@Override
	public void close() {
		op.close();
	}
 
	@Override
	public List<Link> getActiveLinks() {
		// TODO Auto-generated method stub
		op = new GraphDBOperation(GraphDBConnection.getInstance(""));
		op.close(); //Commit to ensure we see latest data
		Iterable<ISwitchObject> switches = op.getActiveSwitches();
		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);
			}
						
		}
		op.commit();
		return links;
	}

	@Override
	public List<Link> getLinksOnSwitch(String dpid) {
		// TODO Auto-generated method stub
		List<Link> links = new ArrayList<Link>(); 
		ISwitchObject sw = op.searchSwitch(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;

	}
	
}

refactor TopoLinkServiceImpl class to use GraphDBOperation class.