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