Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 1 | package net.onrc.onos.graph; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 6 | import org.openflow.protocol.OFPhysicalPort; |
| 7 | |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 8 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 13 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
| 14 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 15 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 16 | |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 17 | import com.google.common.base.Stopwatch; |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 18 | import com.thinkaurelius.titan.core.TitanGraph; |
| 19 | import com.tinkerpop.blueprints.Vertex; |
| 20 | import com.tinkerpop.frames.FramedGraph; |
| 21 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
| 22 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 23 | |
| 24 | public class GraphDBOperation implements IDBOperation { |
| 25 | private GraphDBConnection conn; |
| 26 | |
| 27 | /** |
| 28 | * Create a GraphDBOperation instance from specified GraphDBConnection's instance. |
| 29 | * @param dbConnection an instance of GraphDBConnection |
| 30 | */ |
| 31 | public GraphDBOperation(GraphDBConnection dbConnection) { |
| 32 | this.conn = dbConnection; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Create a GraphDBOperation instance from database configuration path. |
| 37 | * @param dbConfPath a path for database configuration file. |
| 38 | */ |
| 39 | public GraphDBOperation(final String dbConfPath) { |
| 40 | this.conn = GraphDBConnection.getInstance(dbConfPath); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Create a new switch and return the created switch object. |
| 45 | * @param dpid DPID of the switch |
| 46 | */ |
| 47 | public ISwitchObject newSwitch(String dpid) { |
| 48 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 49 | ISwitchObject obj = fg.addVertex(null,ISwitchObject.class); |
| 50 | if (obj != null) { |
| 51 | obj.setType("switch"); |
| 52 | obj.setDPID(dpid); |
| 53 | } |
| 54 | return obj; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Search and get a switch object with DPID. |
| 59 | * @param dpid DPID of the switch |
| 60 | */ |
| 61 | public ISwitchObject searchSwitch(String dpid) { |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 62 | |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 63 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 64 | |
| 65 | return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ? |
| 66 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Search and get an active switch object with DPID. |
| 72 | * @param dpid DPID of the switch |
| 73 | */ |
| 74 | public ISwitchObject searchActiveSwitch(String dpid) { |
| 75 | |
| 76 | ISwitchObject sw = searchSwitch(dpid); |
| 77 | if ((sw != null) && |
| 78 | sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 79 | return sw; |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get all switch objects. |
| 86 | */ |
| 87 | public Iterable<ISwitchObject> getAllSwitches() { |
| 88 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 89 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 90 | return switches; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get all active switch objects. |
| 95 | */ |
| 96 | public Iterable<ISwitchObject> getActiveSwitches() { |
| 97 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 98 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 99 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
| 100 | |
| 101 | for (ISwitchObject sw: switches) { |
| 102 | if(sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 103 | activeSwitches.add(sw); |
| 104 | } |
| 105 | } |
| 106 | return activeSwitches; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get all inactive switch objects. |
| 111 | */ |
| 112 | public Iterable<ISwitchObject> getInactiveSwitches() { |
| 113 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 114 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 115 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
| 116 | |
| 117 | for (ISwitchObject sw: switches) { |
| 118 | if(sw.getState().equals(SwitchState.INACTIVE.toString())) { |
| 119 | inactiveSwitches.add(sw); |
| 120 | } |
| 121 | } |
| 122 | return inactiveSwitches; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get all flow entries' objects where their switches are not updated. |
| 127 | */ |
| 128 | public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() { |
| 129 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 130 | //TODO: Should use an enum for flow_switch_state |
| 131 | return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Remove specified switch. |
| 136 | * @param sw switch object to remove |
| 137 | */ |
| 138 | public void removeSwitch(ISwitchObject sw) { |
| 139 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 140 | fg.removeVertex(sw.asVertex()); |
| 141 | } |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 142 | |
| 143 | @Override |
| 144 | public IPortObject newPort(String dpid, Short portNumber) { |
| 145 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 146 | IPortObject obj = fg.addVertex(null,IPortObject.class); |
| 147 | if (obj != null) { |
| 148 | obj.setType("port"); |
| 149 | String id = dpid + portNumber.toString(); |
| 150 | obj.setPortId(id); |
| 151 | obj.setNumber(portNumber); |
| 152 | } |
| 153 | return obj; |
| 154 | |
| 155 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Create a port having specified port number. |
| 159 | * @param portNumber port number |
| 160 | */ |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 161 | @Deprecated |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 162 | public IPortObject newPort(Short portNumber) { |
| 163 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 164 | IPortObject obj = fg.addVertex(null,IPortObject.class); |
| 165 | if (obj != null) { |
| 166 | obj.setType("port"); |
| 167 | obj.setNumber(portNumber); |
| 168 | } |
| 169 | return obj; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Search and get a port object of specified switch and port number. |
| 174 | * @param dpid DPID of a switch |
| 175 | * @param number port number of the switch's port |
| 176 | */ |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 177 | public IPortObject searchPort(String dpid, Short number) { |
| 178 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 179 | String id = dpid + number.toString(); |
| 180 | return (fg != null && fg.getVertices("port_id",id).iterator().hasNext()) ? |
| 181 | fg.getVertices("port_id",id,IPortObject.class).iterator().next() : null; |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Remove the specified switch port. |
| 186 | * @param port switch port object to remove |
| 187 | */ |
| 188 | public void removePort(IPortObject port) { |
| 189 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 190 | // EventGraph<TitanGraph> eg = conn.getEventGraph(); |
| 191 | if (fg != null) fg.removeVertex(port.asVertex()); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Create and return a device object. |
| 196 | */ |
| 197 | public IDeviceObject newDevice() { |
| 198 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 199 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
| 200 | if (obj != null) obj.setType("device"); |
| 201 | return obj; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Search and get a device object having specified MAC address. |
| 206 | * @param macAddr MAC address to search and get |
| 207 | */ |
| 208 | public IDeviceObject searchDevice(String macAddr) { |
| 209 | // TODO Auto-generated method stub |
| 210 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 211 | return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ? |
| 212 | fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator().next() : null; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get all devices. |
| 217 | */ |
| 218 | public Iterable<IDeviceObject> getDevices() { |
| 219 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 220 | return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Remove the specified device. |
| 225 | * @param dev a device object to remove |
| 226 | */ |
| 227 | public void removeDevice(IDeviceObject dev) { |
| 228 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 229 | if (fg != null) fg.removeVertex(dev.asVertex()); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Create and return a flow path object. |
| 234 | */ |
| 235 | public IFlowPath newFlowPath() { |
| 236 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 237 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
| 238 | if (flowPath != null) flowPath.setType("flow"); |
| 239 | return flowPath; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Search and get a flow path object with specified flow ID. |
| 244 | * @param flowId flow ID to search |
| 245 | */ |
| 246 | public IFlowPath searchFlowPath(FlowId flowId) { |
| 247 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 248 | |
| 249 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ? |
| 250 | fg.getVertices("flow_id", flowId.toString(), |
| 251 | IFlowPath.class).iterator().next() : null; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get a flow path object with a flow entry. |
| 256 | * @param flowEntry flow entry object |
| 257 | */ |
| 258 | public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) { |
| 259 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 260 | pipe.start(flowEntry.asVertex()); |
| 261 | pipe.out("flow"); |
| 262 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
| 263 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get all flow path objects. |
| 268 | */ |
| 269 | public Iterable<IFlowPath> getAllFlowPaths() { |
| 270 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 271 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
| 272 | |
| 273 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 274 | |
| 275 | for (IFlowPath fp: flowPaths) { |
| 276 | if (fp.getFlowId() != null) { |
| 277 | nonNullFlows.add(fp); |
| 278 | } |
| 279 | } |
| 280 | return nonNullFlows; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Remove the specified flow path. |
| 285 | * @param flowPath flow path object to remove |
| 286 | */ |
| 287 | public void removeFlowPath(IFlowPath flowPath) { |
| 288 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 289 | fg.removeVertex(flowPath.asVertex()); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Create and return a flow entry object. |
| 294 | */ |
| 295 | public IFlowEntry newFlowEntry() { |
| 296 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 297 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
| 298 | if (flowEntry != null) flowEntry.setType("flow_entry"); |
| 299 | return flowEntry; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Search and get a flow entry object with flow entry ID. |
| 304 | * @param flowEntryId flow entry ID to search |
| 305 | */ |
| 306 | public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) { |
| 307 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 308 | |
| 309 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 310 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 311 | IFlowEntry.class).iterator().next() : null; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Get all flow entry objects. |
| 316 | */ |
| 317 | public Iterable<IFlowEntry> getAllFlowEntries() { |
| 318 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 319 | |
| 320 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Remove the specified flow entry. |
| 325 | * @param flowEntry flow entry object to remove |
| 326 | */ |
| 327 | public void removeFlowEntry(IFlowEntry flowEntry) { |
| 328 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 329 | fg.removeVertex(flowEntry.asVertex()); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get the instance of GraphDBConnection assigned to this class. |
| 334 | */ |
| 335 | public IDBConnection getDBConnection() { |
| 336 | return conn; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Commit changes for the graph. |
| 341 | */ |
| 342 | public void commit() { |
| 343 | conn.commit(); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Rollback changes for the graph. |
| 348 | */ |
| 349 | public void rollback() { |
| 350 | conn.rollback(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Close the connection of the assigned GraphDBConnection. |
| 355 | */ |
| 356 | public void close() { |
| 357 | conn.close(); |
| 358 | } |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 359 | |
| 360 | |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 361 | } |