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