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; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 4 | import com.tinkerpop.blueprints.Vertex; |
| 5 | import com.tinkerpop.frames.FramedGraph; |
| 6 | import com.tinkerpop.frames.FramedVertexIterable; |
| 7 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 8 | |
| 9 | import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 10 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 11 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
| 13 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.util.FlowEntryId; |
| 15 | import net.floodlightcontroller.util.FlowId; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 16 | |
| 17 | public class GraphDBUtils implements IDBUtils { |
| 18 | |
| 19 | @Override |
| 20 | public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) { |
| 21 | // TODO Auto-generated method stub |
| 22 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 23 | |
| 24 | return fg.getVertices("dpid",dpid).iterator().hasNext() ? |
| 25 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
| 26 | |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) { |
| 31 | // TODO Auto-generated method stub |
| 32 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 33 | return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr, |
| 34 | IDeviceObject.class).iterator().next() : null; |
| 35 | |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) { |
| 40 | ISwitchObject sw = searchSwitch(conn, dpid); |
| 41 | GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>(); |
| 42 | pipe.start(sw.asVertex()); |
| 43 | pipe.out("on").has("number", number); |
| 44 | FramedVertexIterable<IPortObject> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IPortObject.class); |
| 45 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public IDeviceObject newDevice(GraphDBConnection conn) { |
| 50 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 51 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
| 52 | return obj; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void removeDevice(GraphDBConnection conn, IDeviceObject dev) { |
| 57 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 58 | fg.removeVertex(dev.asVertex()); |
| 59 | } |
| 60 | |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 61 | @Override |
| 62 | public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) { |
| 63 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 64 | return fg.getVertices("type","device",IDeviceObject.class); |
| 65 | } |
| 66 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 67 | @Override |
| 68 | public IFlowPath searchFlowPath(GraphDBConnection conn, |
| 69 | FlowId flowId) { |
| 70 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 71 | |
| 72 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ? |
| 73 | fg.getVertices("flow_id", flowId.toString(), |
| 74 | IFlowPath.class).iterator().next() : null; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public IFlowPath newFlowPath(GraphDBConnection conn) { |
| 79 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 80 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
| 81 | return flowPath; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void removeFlowPath(GraphDBConnection conn, |
| 86 | IFlowPath flowPath) { |
| 87 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 88 | fg.removeVertex(flowPath.asVertex()); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn, |
| 93 | IFlowEntry flowEntry) { |
| 94 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 95 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 96 | pipe.start(flowEntry.asVertex()); |
| 97 | pipe.out("flow"); |
| 98 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IFlowPath.class); |
| 99 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 100 | } |
| 101 | |
| 102 | @Override |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 103 | public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) { |
| 104 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 105 | |
| 106 | return fg.getVertices("type", "flow", IFlowPath.class); |
| 107 | } |
| 108 | |
| 109 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 110 | public IFlowEntry searchFlowEntry(GraphDBConnection conn, |
| 111 | FlowEntryId flowEntryId) { |
| 112 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 113 | |
| 114 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 115 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 116 | IFlowEntry.class).iterator().next() : null; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public IFlowEntry newFlowEntry(GraphDBConnection conn) { |
| 121 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 122 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
| 123 | return flowEntry; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void removeFlowEntry(GraphDBConnection conn, |
| 128 | IFlowEntry flowEntry) { |
| 129 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 130 | fg.removeVertex(flowEntry.asVertex()); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) { |
| 135 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 136 | |
| 137 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 138 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 139 | } |