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