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