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