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