blob: 097cfa04bd277ca9593604aaa6ec9ea89c9fe2e2 [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
3import com.thinkaurelius.titan.core.TitanGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -08004import com.tinkerpop.blueprints.Vertex;
5import com.tinkerpop.frames.FramedGraph;
6import com.tinkerpop.frames.FramedVertexIterable;
7import com.tinkerpop.gremlin.java.GremlinPipeline;
8
9import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080010import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
11import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berdeda809572013-02-22 15:31:20 -080012import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
13import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080014import net.floodlightcontroller.util.FlowEntryId;
15import net.floodlightcontroller.util.FlowId;
Pankaj Berdeda809572013-02-22 15:31:20 -080016
17public 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 Berdeac1a8c32013-02-26 17:45:57 -080061 @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 Radoslavovb6f53542013-03-01 16:02:14 -080067 @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 Radoslavov706df052013-03-06 10:49:07 -0800103 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 Radoslavovb6f53542013-03-01 16:02:14 -0800110 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 Berdeda809572013-02-22 15:31:20 -0800139}