blob: b0ca23bdb533667d4dc674e27b9f85dd4f0b9746 [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;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070018import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph;
Pankaj Berde8f036112013-03-28 22:58:47 -070019import com.tinkerpop.frames.FramedGraph;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070020import com.tinkerpop.frames.structures.FramedVertexIterable;
Pankaj Berde8f036112013-03-28 22:58:47 -070021import com.tinkerpop.gremlin.java.GremlinPipeline;
22
Pankaj Berdeda809572013-02-22 15:31:20 -080023public class GraphDBUtils implements IDBUtils {
Pankaj Berde15193092013-03-21 17:30:14 -070024
25 @Override
26 public ISwitchObject newSwitch(GraphDBConnection conn) {
27 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
28 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
29 return obj;
30 }
Pankaj Berdeda809572013-02-22 15:31:20 -080031
32 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070033 public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
34 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
35 fg.removeVertex(sw.asVertex());
36 }
37
38 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080039 public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
40 // TODO Auto-generated method stub
41 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
42
43 return fg.getVertices("dpid",dpid).iterator().hasNext() ?
44 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
45
46 }
47
48 @Override
49 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
50 // TODO Auto-generated method stub
51 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
52 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
53 IDeviceObject.class).iterator().next() : null;
54
55 }
56
57 @Override
58 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
59 ISwitchObject sw = searchSwitch(conn, dpid);
Pankaj Berde73ea7e52013-04-04 14:52:50 -070060 return sw != null ? sw.getPort(number): null;
61 /* GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
Pankaj Berdeda809572013-02-22 15:31:20 -080062 pipe.start(sw.asVertex());
63 pipe.out("on").has("number", number);
Pankaj Berde8f036112013-03-28 22:58:47 -070064 FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
Pankaj Berde73ea7e52013-04-04 14:52:50 -070065 return r.iterator().hasNext() ? r.iterator().next() : null; */
Pankaj Berdeda809572013-02-22 15:31:20 -080066 }
67
68 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070069 public IPortObject newPort(GraphDBConnection conn) {
70 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
71 IPortObject obj = fg.addVertex(null,IPortObject.class);
72 return obj;
73 }
74
75 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080076 public IDeviceObject newDevice(GraphDBConnection conn) {
77 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
78 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
79 return obj;
80 }
Pankaj Berde15193092013-03-21 17:30:14 -070081
82 @Override
83 public void removePort(GraphDBConnection conn, IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -070084 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
85// EventGraph<TitanGraph> eg = conn.getEventGraph();
86 fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -070087 }
Pankaj Berdeda809572013-02-22 15:31:20 -080088
89 @Override
90 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
91 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
92 fg.removeVertex(dev.asVertex());
93 }
94
Pankaj Berdeac1a8c32013-02-26 17:45:57 -080095 @Override
96 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
97 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
98 return fg.getVertices("type","device",IDeviceObject.class);
99 }
100
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800101 @Override
102 public IFlowPath searchFlowPath(GraphDBConnection conn,
103 FlowId flowId) {
104 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
105
106 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
107 fg.getVertices("flow_id", flowId.toString(),
108 IFlowPath.class).iterator().next() : null;
109 }
110
111 @Override
112 public IFlowPath newFlowPath(GraphDBConnection conn) {
113 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
114 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
115 return flowPath;
116 }
117
118 @Override
119 public void removeFlowPath(GraphDBConnection conn,
120 IFlowPath flowPath) {
121 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
122 fg.removeVertex(flowPath.asVertex());
123 }
124
125 @Override
126 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
127 IFlowEntry flowEntry) {
128 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
129 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
130 pipe.start(flowEntry.asVertex());
131 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700132 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800133 return r.iterator().hasNext() ? r.iterator().next() : null;
134 }
135
136 @Override
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800137 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
138 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
139
140 return fg.getVertices("type", "flow", IFlowPath.class);
141 }
142
143 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800144 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
145 FlowEntryId flowEntryId) {
146 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
147
148 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
149 fg.getVertices("flow_entry_id", flowEntryId.toString(),
150 IFlowEntry.class).iterator().next() : null;
151 }
152
153 @Override
154 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
155 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
156 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
157 return flowEntry;
158 }
159
160 @Override
161 public void removeFlowEntry(GraphDBConnection conn,
162 IFlowEntry flowEntry) {
163 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
164 fg.removeVertex(flowEntry.asVertex());
165 }
166
167 @Override
168 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
169 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
170
171 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
172 }
Pankaj Berde15193092013-03-21 17:30:14 -0700173
174 @Override
175 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
176 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
177 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
178 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
179
180 for (ISwitchObject sw: switches) {
181 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
182 activeSwitches.add(sw);
183 }
184 }
185 return activeSwitches;
186 }
187
188 @Override
189 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
190 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
191 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
192 return switches;
193 }
194
195 @Override
196 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
197 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
198 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
199 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
200
201 for (ISwitchObject sw: switches) {
202 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
203 inactiveSwitches.add(sw);
204 }
205 }
206 return inactiveSwitches;
207 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700208
209 @Override
210 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
211 // TODO Auto-generated method stub
212 return null;
213 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800214}