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