blob: 2029d80d3611c00f259dcab171e4fa51d2a50fcd [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
Pankaj Berde15193092013-03-21 17:30:14 -07003import java.util.ArrayList;
4import java.util.List;
5
Pankaj Berdeda809572013-02-22 15:31:20 -08006import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08007import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
8import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berdeda809572013-02-22 15:31:20 -08009import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde15193092013-03-21 17:30:14 -070011import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080012import net.floodlightcontroller.util.FlowEntryId;
13import net.floodlightcontroller.util.FlowId;
Pankaj Berdeda809572013-02-22 15:31:20 -080014
Pankaj Berde8f036112013-03-28 22:58:47 -070015import com.thinkaurelius.titan.core.TitanGraph;
16import com.tinkerpop.blueprints.Vertex;
17import com.tinkerpop.blueprints.util.wrappers.event.EventGraph;
18import com.tinkerpop.frames.FramedGraph;
19import com.tinkerpop.frames.FramedVertexIterable;
20import com.tinkerpop.gremlin.java.GremlinPipeline;
21
Pankaj Berdeda809572013-02-22 15:31:20 -080022public class GraphDBUtils implements IDBUtils {
Pankaj Berde15193092013-03-21 17:30:14 -070023
24 @Override
25 public ISwitchObject newSwitch(GraphDBConnection conn) {
26 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
27 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
28 return obj;
29 }
Pankaj Berdeda809572013-02-22 15:31:20 -080030
31 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070032 public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
33 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
34 fg.removeVertex(sw.asVertex());
35 }
36
37 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080038 public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
39 // TODO Auto-generated method stub
40 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
41
42 return fg.getVertices("dpid",dpid).iterator().hasNext() ?
43 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
44
45 }
46
47 @Override
48 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
49 // TODO Auto-generated method stub
50 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
51 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
52 IDeviceObject.class).iterator().next() : null;
53
54 }
55
56 @Override
57 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
58 ISwitchObject sw = searchSwitch(conn, dpid);
59 GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
60 pipe.start(sw.asVertex());
61 pipe.out("on").has("number", number);
Pankaj Berde8f036112013-03-28 22:58:47 -070062 FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080063 return r.iterator().hasNext() ? r.iterator().next() : null;
64 }
65
66 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070067 public IPortObject newPort(GraphDBConnection conn) {
68 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
69 IPortObject obj = fg.addVertex(null,IPortObject.class);
70 return obj;
71 }
72
73 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080074 public IDeviceObject newDevice(GraphDBConnection conn) {
75 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
76 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
77 return obj;
78 }
Pankaj Berde15193092013-03-21 17:30:14 -070079
80 @Override
81 public void removePort(GraphDBConnection conn, IPortObject port) {
Pankaj Berde8f036112013-03-28 22:58:47 -070082// FramedGraph<TitanGraph> fg = conn.getFramedGraph();
83 EventGraph<TitanGraph> eg = conn.getEventGraph();
84 eg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -070085 }
Pankaj Berdeda809572013-02-22 15:31:20 -080086
87 @Override
88 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
89 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
90 fg.removeVertex(dev.asVertex());
91 }
92
Pankaj Berdeac1a8c32013-02-26 17:45:57 -080093 @Override
94 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
95 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
96 return fg.getVertices("type","device",IDeviceObject.class);
97 }
98
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080099 @Override
100 public IFlowPath searchFlowPath(GraphDBConnection conn,
101 FlowId flowId) {
102 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
103
104 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
105 fg.getVertices("flow_id", flowId.toString(),
106 IFlowPath.class).iterator().next() : null;
107 }
108
109 @Override
110 public IFlowPath newFlowPath(GraphDBConnection conn) {
111 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
112 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
113 return flowPath;
114 }
115
116 @Override
117 public void removeFlowPath(GraphDBConnection conn,
118 IFlowPath flowPath) {
119 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
120 fg.removeVertex(flowPath.asVertex());
121 }
122
123 @Override
124 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
125 IFlowEntry flowEntry) {
126 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
127 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
128 pipe.start(flowEntry.asVertex());
129 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700130 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800131 return r.iterator().hasNext() ? r.iterator().next() : null;
132 }
133
134 @Override
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800135 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
136 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
137
138 return fg.getVertices("type", "flow", IFlowPath.class);
139 }
140
141 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800142 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
143 FlowEntryId flowEntryId) {
144 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
145
146 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
147 fg.getVertices("flow_entry_id", flowEntryId.toString(),
148 IFlowEntry.class).iterator().next() : null;
149 }
150
151 @Override
152 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
153 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
154 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
155 return flowEntry;
156 }
157
158 @Override
159 public void removeFlowEntry(GraphDBConnection conn,
160 IFlowEntry flowEntry) {
161 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
162 fg.removeVertex(flowEntry.asVertex());
163 }
164
165 @Override
166 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
167 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
168
169 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
170 }
Pankaj Berde15193092013-03-21 17:30:14 -0700171
172 @Override
173 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
174 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
175 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
176 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
177
178 for (ISwitchObject sw: switches) {
179 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
180 activeSwitches.add(sw);
181 }
182 }
183 return activeSwitches;
184 }
185
186 @Override
187 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
188 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
189 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
190 return switches;
191 }
192
193 @Override
194 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
195 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
196 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
197 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
198
199 for (ISwitchObject sw: switches) {
200 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
201 inactiveSwitches.add(sw);
202 }
203 }
204 return inactiveSwitches;
205 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800206}