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