Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.onrc.onos.util; |
| 2 | |
| 3 | import com.thinkaurelius.titan.core.TitanGraph; |
| 4 | import com.thinkaurelius.titan.graphdb.transaction.VertexIterable; |
| 5 | import com.tinkerpop.blueprints.Vertex; |
| 6 | import com.tinkerpop.frames.FramedGraph; |
| 7 | import com.tinkerpop.frames.FramedVertexIterable; |
| 8 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 9 | |
| 10 | import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 11 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
| 12 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 13 | import net.floodlightcontroller.routing.Link; |
| 14 | |
| 15 | public class GraphDBUtils implements IDBUtils { |
| 16 | |
| 17 | @Override |
| 18 | public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) { |
| 19 | // TODO Auto-generated method stub |
| 20 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 21 | |
| 22 | return fg.getVertices("dpid",dpid).iterator().hasNext() ? |
| 23 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
| 24 | |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) { |
| 29 | // TODO Auto-generated method stub |
| 30 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 31 | return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr, |
| 32 | IDeviceObject.class).iterator().next() : null; |
| 33 | |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) { |
| 38 | ISwitchObject sw = searchSwitch(conn, dpid); |
| 39 | GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>(); |
| 40 | pipe.start(sw.asVertex()); |
| 41 | pipe.out("on").has("number", number); |
| 42 | FramedVertexIterable<IPortObject> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IPortObject.class); |
| 43 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public IDeviceObject newDevice(GraphDBConnection conn) { |
| 48 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 49 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
| 50 | return obj; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void removeDevice(GraphDBConnection conn, IDeviceObject dev) { |
| 55 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 56 | fg.removeVertex(dev.asVertex()); |
| 57 | } |
| 58 | |
| 59 | } |