Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.onrc.onos.util; |
| 2 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 7 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 8 | import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 9 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
| 10 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 11 | import net.floodlightcontroller.core.ISwitchStorage.SwitchState; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.util.FlowEntryId; |
| 13 | import net.floodlightcontroller.util.FlowId; |
Toshio Koide | a41950a | 2013-06-13 13:09:19 -0700 | [diff] [blame] | 14 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 15 | |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 16 | import com.thinkaurelius.titan.core.TitanGraph; |
| 17 | import com.tinkerpop.blueprints.Vertex; |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 18 | import com.tinkerpop.frames.FramedGraph; |
Pankaj Berde | 5d50641 | 2013-04-23 15:03:02 -0700 | [diff] [blame] | 19 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 20 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 21 | |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 22 | public class GraphDBOperation implements IDBOperation { |
| 23 | private GraphDBConnection conn; |
| 24 | |
| 25 | public GraphDBOperation(GraphDBConnection dbConnection) { |
| 26 | this.conn = dbConnection; |
| 27 | } |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 28 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 29 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 30 | public ISwitchObject newSwitch(String dpid) { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 31 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 32 | ISwitchObject obj = fg.addVertex(null,ISwitchObject.class); |
Toshio Koide | 12004e6 | 2013-06-12 18:17:53 -0700 | [diff] [blame] | 33 | if (obj != null) { |
| 34 | obj.setType("switch"); |
| 35 | obj.setDPID(dpid); |
| 36 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 37 | return obj; |
| 38 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 39 | |
| 40 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 41 | public ISwitchObject searchSwitch(String dpid) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 42 | // TODO Auto-generated method stub |
| 43 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 44 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 45 | return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ? |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 46 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 47 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 51 | public ISwitchObject searchActiveSwitch(String dpid) { |
| 52 | |
| 53 | ISwitchObject sw = searchSwitch(dpid); |
| 54 | if ((sw != null) && |
| 55 | sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 56 | return sw; |
| 57 | } |
| 58 | return null; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 62 | public Iterable<ISwitchObject> getAllSwitches() { |
| 63 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 64 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 65 | return switches; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Iterable<ISwitchObject> getActiveSwitches() { |
| 70 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 71 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 72 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
| 73 | |
| 74 | for (ISwitchObject sw: switches) { |
| 75 | if(sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 76 | activeSwitches.add(sw); |
Pankaj Berde | c165a52 | 2013-06-10 21:28:11 -0700 | [diff] [blame] | 77 | } |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 78 | } |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 79 | return activeSwitches; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Iterable<ISwitchObject> getInactiveSwitches() { |
| 84 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 85 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 86 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
| 87 | |
| 88 | for (ISwitchObject sw: switches) { |
| 89 | if(sw.getState().equals(SwitchState.INACTIVE.toString())) { |
| 90 | inactiveSwitches.add(sw); |
| 91 | } |
| 92 | } |
| 93 | return inactiveSwitches; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() { |
| 98 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 99 | //TODO: Should use an enum for flow_switch_state |
| 100 | return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeSwitch(ISwitchObject sw) { |
| 105 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 106 | fg.removeVertex(sw.asVertex()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
Toshio Koide | 12004e6 | 2013-06-12 18:17:53 -0700 | [diff] [blame] | 110 | public IPortObject newPort(Short portNumber) { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 111 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 112 | IPortObject obj = fg.addVertex(null,IPortObject.class); |
Toshio Koide | 12004e6 | 2013-06-12 18:17:53 -0700 | [diff] [blame] | 113 | if (obj != null) { |
| 114 | obj.setType("port"); |
| 115 | obj.setNumber(portNumber); |
| 116 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 117 | return obj; |
| 118 | } |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 119 | |
| 120 | @Override |
| 121 | public IPortObject searchPort(String dpid, short number) { |
| 122 | ISwitchObject sw = searchSwitch(dpid); |
| 123 | if (sw != null) { |
| 124 | |
| 125 | IPortObject port = null; |
| 126 | |
| 127 | // Requires Frames 2.3.0 |
| 128 | |
| 129 | try { |
| 130 | port = sw.getPort(number); |
| 131 | } catch (Exception e) { |
| 132 | // TODO Auto-generated catch block |
| 133 | e.printStackTrace(); |
| 134 | } |
| 135 | |
| 136 | return port; |
| 137 | } |
| 138 | |
| 139 | // if (sw != null) { |
| 140 | // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>(); |
| 141 | // pipe.start(sw.asVertex()); |
| 142 | // pipe.out("on").has("number", number); |
| 143 | // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class); |
| 144 | // return r != null && r.iterator().hasNext() ? r.iterator().next() : null; |
| 145 | // } |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void removePort(IPortObject port) { |
| 151 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 152 | // EventGraph<TitanGraph> eg = conn.getEventGraph(); |
| 153 | if (fg != null) fg.removeVertex(port.asVertex()); |
| 154 | } |
| 155 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 156 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 157 | public IDeviceObject newDevice() { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 158 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 159 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
Toshio Koide | d7b7e10 | 2013-06-12 17:13:25 -0700 | [diff] [blame] | 160 | if (obj != null) obj.setType("device"); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 161 | return obj; |
| 162 | } |
| 163 | |
| 164 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 165 | public IDeviceObject searchDevice(String macAddr) { |
| 166 | // TODO Auto-generated method stub |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 167 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 168 | return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr, |
| 169 | IDeviceObject.class).iterator().next() : null; |
| 170 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 173 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 174 | public Iterable<IDeviceObject> getDevices() { |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 175 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 176 | return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null; |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 179 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 180 | public void removeDevice(IDeviceObject dev) { |
| 181 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 182 | if (fg != null) fg.removeVertex(dev.asVertex()); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 186 | public IFlowPath newFlowPath() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 187 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 188 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
Toshio Koide | d7b7e10 | 2013-06-12 17:13:25 -0700 | [diff] [blame] | 189 | if (flowPath != null) flowPath.setType("flow"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 190 | return flowPath; |
| 191 | } |
| 192 | |
| 193 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 194 | public IFlowPath searchFlowPath(FlowId flowId) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 195 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 196 | |
| 197 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ? |
| 198 | fg.getVertices("flow_id", flowId.toString(), |
| 199 | IFlowPath.class).iterator().next() : null; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 203 | public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 204 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 205 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 206 | pipe.start(flowEntry.asVertex()); |
| 207 | pipe.out("flow"); |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 208 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 209 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 210 | } |
| 211 | |
| 212 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 213 | public Iterable<IFlowPath> getAllFlowPaths() { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 214 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 215 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 216 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 217 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 218 | |
| 219 | for (IFlowPath fp: flowPaths) { |
| 220 | if (fp.getFlowId() != null) { |
| 221 | nonNullFlows.add(fp); |
| 222 | } |
| 223 | } |
| 224 | return nonNullFlows; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 228 | public void removeFlowPath(IFlowPath flowPath) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 229 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 230 | fg.removeVertex(flowPath.asVertex()); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 234 | public IFlowEntry newFlowEntry() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 235 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 236 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
Toshio Koide | d7b7e10 | 2013-06-12 17:13:25 -0700 | [diff] [blame] | 237 | if (flowEntry != null) flowEntry.setType("flow_entry"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 238 | return flowEntry; |
| 239 | } |
| 240 | |
| 241 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 242 | public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 243 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 244 | |
| 245 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 246 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 247 | IFlowEntry.class).iterator().next() : null; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 251 | public Iterable<IFlowEntry> getAllFlowEntries() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 252 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 253 | |
| 254 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 255 | } |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 256 | |
Pankaj Berde | d1c3859 | 2013-04-10 22:46:40 -0700 | [diff] [blame] | 257 | @Override |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 258 | public void removeFlowEntry(IFlowEntry flowEntry) { |
Pankaj Berde | d1c3859 | 2013-04-10 22:46:40 -0700 | [diff] [blame] | 259 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Toshio Koide | 5340380 | 2013-06-13 10:08:06 -0700 | [diff] [blame] | 260 | fg.removeVertex(flowEntry.asVertex()); |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 261 | } |
Toshio Koide | a41950a | 2013-06-13 13:09:19 -0700 | [diff] [blame] | 262 | |
| 263 | public IDBConnection getDBConnection() { |
| 264 | return conn; |
| 265 | } |
| 266 | |
| 267 | public void commit() { |
| 268 | conn.endTx(Transaction.COMMIT); |
| 269 | } |
| 270 | |
| 271 | public void rollback() { |
| 272 | conn.endTx(Transaction.ROLLBACK); |
| 273 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 274 | } |