blob: ba48103288b24cb520bc6d913b69ca343316ab26 [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 com.thinkaurelius.titan.core.TitanGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -08007import com.tinkerpop.blueprints.Vertex;
8import com.tinkerpop.frames.FramedGraph;
9import com.tinkerpop.frames.FramedVertexIterable;
10import com.tinkerpop.gremlin.java.GremlinPipeline;
11
12import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080013import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
14import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berdeda809572013-02-22 15:31:20 -080015import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
16import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde15193092013-03-21 17:30:14 -070017import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080018import net.floodlightcontroller.util.FlowEntryId;
19import net.floodlightcontroller.util.FlowId;
Pankaj Berdeda809572013-02-22 15:31:20 -080020
21public class GraphDBUtils implements IDBUtils {
Pankaj Berde15193092013-03-21 17:30:14 -070022
23 @Override
24 public ISwitchObject newSwitch(GraphDBConnection conn) {
25 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
26 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
27 return obj;
28 }
Pankaj Berdeda809572013-02-22 15:31:20 -080029
30 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070031 public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
32 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
33 fg.removeVertex(sw.asVertex());
34 }
35
36 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080037 public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
38 // TODO Auto-generated method stub
39 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
40
41 return fg.getVertices("dpid",dpid).iterator().hasNext() ?
42 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
43
44 }
45
46 @Override
47 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
48 // TODO Auto-generated method stub
49 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
50 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
51 IDeviceObject.class).iterator().next() : null;
52
53 }
54
55 @Override
56 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
57 ISwitchObject sw = searchSwitch(conn, dpid);
58 GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
59 pipe.start(sw.asVertex());
60 pipe.out("on").has("number", number);
61 FramedVertexIterable<IPortObject> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IPortObject.class);
62 return r.iterator().hasNext() ? r.iterator().next() : null;
63 }
64
65 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070066 public IPortObject newPort(GraphDBConnection conn) {
67 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
68 IPortObject obj = fg.addVertex(null,IPortObject.class);
69 return obj;
70 }
71
72 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080073 public IDeviceObject newDevice(GraphDBConnection conn) {
74 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
75 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
76 return obj;
77 }
Pankaj Berde15193092013-03-21 17:30:14 -070078
79 @Override
80 public void removePort(GraphDBConnection conn, IPortObject port) {
81 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
82 fg.removeVertex(port.asVertex());
83 }
Pankaj Berdeda809572013-02-22 15:31:20 -080084
85 @Override
86 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
87 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
88 fg.removeVertex(dev.asVertex());
89 }
90
Pankaj Berdeac1a8c32013-02-26 17:45:57 -080091 @Override
92 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
93 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
94 return fg.getVertices("type","device",IDeviceObject.class);
95 }
96
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080097 @Override
98 public IFlowPath searchFlowPath(GraphDBConnection conn,
99 FlowId flowId) {
100 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
101
102 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
103 fg.getVertices("flow_id", flowId.toString(),
104 IFlowPath.class).iterator().next() : null;
105 }
106
107 @Override
108 public IFlowPath newFlowPath(GraphDBConnection conn) {
109 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
110 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
111 return flowPath;
112 }
113
114 @Override
115 public void removeFlowPath(GraphDBConnection conn,
116 IFlowPath flowPath) {
117 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
118 fg.removeVertex(flowPath.asVertex());
119 }
120
121 @Override
122 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
123 IFlowEntry flowEntry) {
124 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
125 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
126 pipe.start(flowEntry.asVertex());
127 pipe.out("flow");
128 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IFlowPath.class);
129 return r.iterator().hasNext() ? r.iterator().next() : null;
130 }
131
132 @Override
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800133 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
134 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
135
136 return fg.getVertices("type", "flow", IFlowPath.class);
137 }
138
139 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800140 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
141 FlowEntryId flowEntryId) {
142 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
143
144 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
145 fg.getVertices("flow_entry_id", flowEntryId.toString(),
146 IFlowEntry.class).iterator().next() : null;
147 }
148
149 @Override
150 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
151 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
152 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
153 return flowEntry;
154 }
155
156 @Override
157 public void removeFlowEntry(GraphDBConnection conn,
158 IFlowEntry flowEntry) {
159 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
160 fg.removeVertex(flowEntry.asVertex());
161 }
162
163 @Override
164 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
165 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
166
167 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
168 }
Pankaj Berde15193092013-03-21 17:30:14 -0700169
170 @Override
171 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
172 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
173 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
174 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
175
176 for (ISwitchObject sw: switches) {
177 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
178 activeSwitches.add(sw);
179 }
180 }
181 return activeSwitches;
182 }
183
184 @Override
185 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
186 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
187 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
188 return switches;
189 }
190
191 @Override
192 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
193 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
194 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
195 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
196
197 for (ISwitchObject sw: switches) {
198 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
199 inactiveSwitches.add(sw);
200 }
201 }
202 return inactiveSwitches;
203 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800204}