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