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 | |
| 7 | import com.thinkaurelius.titan.core.TitanGraph; |
| 8 | import com.tinkerpop.blueprints.Vertex; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
| 11 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.Iterator; |
| 14 | import java.util.List; |
| 15 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects; |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 19 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 22 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 23 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 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 | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 34 | @Override |
| 35 | public ISwitchObject searchActiveSwitch(String dpid) { |
| 36 | ISwitchObject sw = searchSwitch(dpid); |
| 37 | if ((sw != null) |
| 38 | && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) { |
| 39 | return sw; |
| 40 | } |
| 41 | return null; |
| 42 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 43 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 44 | @Override |
| 45 | public ISwitchObject newSwitch(final String dpid) { |
| 46 | ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class); |
| 47 | if (obj != null) { |
| 48 | obj.setType("switch"); |
| 49 | obj.setDPID(dpid); |
| 50 | } |
| 51 | return obj; |
| 52 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 53 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 54 | @Override |
| 55 | public Iterable<ISwitchObject> getAllSwitches() { |
| 56 | Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class); |
| 57 | return switches; |
| 58 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 59 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 60 | @Override |
| 61 | public Iterable<ISwitchObject> getInactiveSwitches() { |
| 62 | Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class); |
| 63 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 64 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 65 | for (ISwitchObject sw : switches) { |
| 66 | if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) { |
| 67 | inactiveSwitches.add(sw); |
| 68 | } |
| 69 | } |
| 70 | return inactiveSwitches; |
| 71 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 72 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 73 | @Override |
| 74 | public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() { |
| 75 | //TODO: Should use an enum for flow_switch_state |
| 76 | return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 77 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 78 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 79 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 80 | @Override |
| 81 | public void removeSwitch(ISwitchObject sw) { |
| 82 | conn.getFramedGraph().removeVertex(sw.asVertex()); |
| 83 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 84 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 85 | @Override |
| 86 | public IPortObject newPort(String dpid, Short portNum) { |
| 87 | IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class); |
| 88 | if (obj != null) { |
| 89 | obj.setType("port"); |
| 90 | String id = dpid + portNum.toString(); |
| 91 | obj.setPortId(id); |
| 92 | obj.setNumber(portNum); |
| 93 | } |
| 94 | return obj; |
| 95 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 96 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 97 | /** |
| 98 | * Create a port having specified port number. |
| 99 | * |
| 100 | * @param portNumber port number |
| 101 | */ |
| 102 | @Deprecated |
| 103 | public IPortObject newPort(Short portNumber) { |
| 104 | IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class); |
| 105 | if (obj != null) { |
| 106 | obj.setType("port"); |
| 107 | obj.setNumber(portNumber); |
| 108 | } |
| 109 | return obj; |
| 110 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 111 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 112 | @Override |
| 113 | public IPortObject searchPort(String dpid, Short number) { |
| 114 | String id = dpid + number.toString(); |
| 115 | return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("port_id", id).iterator().hasNext()) |
| 116 | ? (IPortObject) conn.getFramedGraph().getVertices("port_id", id, IPortObject.class).iterator().next() : null; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 117 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 118 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 119 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 120 | @Override |
| 121 | public void removePort(IPortObject port) { |
| 122 | if (conn.getFramedGraph() != null) { |
| 123 | conn.getFramedGraph().removeVertex(port.asVertex()); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public IDeviceObject newDevice() { |
| 129 | IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class); |
| 130 | if (obj != null) { |
| 131 | obj.setType("device"); |
| 132 | } |
| 133 | return obj; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Iterable<IDeviceObject> getDevices() { |
| 138 | return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null; |
| 139 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 140 | |
| 141 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 142 | @Override |
| 143 | public void removeDevice(IDeviceObject dev) { |
| 144 | if (conn.getFramedGraph() != null) { |
| 145 | conn.getFramedGraph().removeVertex(dev.asVertex()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Create and return a flow path object. |
| 151 | */ |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 152 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 153 | @Override |
| 154 | public IFlowPath newFlowPath() { |
| 155 | IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class); |
| 156 | if (flowPath != null) { |
| 157 | flowPath.setType("flow"); |
| 158 | } |
| 159 | return flowPath; |
| 160 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 161 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 162 | @Override |
| 163 | public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) { |
| 164 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 165 | pipe.start(flowEntry.asVertex()); |
| 166 | pipe.out("flow"); |
| 167 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
| 168 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 169 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 170 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 171 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 172 | /** |
| 173 | * Search and get a switch object with DPID. |
| 174 | * |
| 175 | * @param dpid DPID of the switch |
| 176 | */ |
| 177 | @Override |
| 178 | public ISwitchObject searchSwitch(final String dpid) { |
| 179 | return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext()) |
| 180 | ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null; |
| 181 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 182 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 183 | @Override |
| 184 | public Iterable<ISwitchObject> getActiveSwitches() { |
| 185 | Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class); |
| 186 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 187 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 188 | for (ISwitchObject sw : switches) { |
| 189 | if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) { |
| 190 | activeSwitches.add(sw); |
| 191 | } |
| 192 | } |
| 193 | return activeSwitches; |
| 194 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 195 | |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 196 | @Override |
| 197 | public IDeviceObject searchDevice(String macAddr) { |
| 198 | return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dl_addr", macAddr).iterator().hasNext()) |
| 199 | ? (IDeviceObject) conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) { |
| 204 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() |
| 205 | ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(), |
| 206 | IFlowPath.class).iterator().next() : null; |
| 207 | } |
| 208 | |
| 209 | protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) { |
| 210 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
| 211 | |
| 212 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 213 | |
| 214 | for (IFlowPath fp : flowPaths) { |
| 215 | if (fp.getFlowId() != null) { |
| 216 | nonNullFlows.add(fp); |
| 217 | } |
| 218 | } |
| 219 | return nonNullFlows; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public IFlowEntry newFlowEntry() { |
| 224 | IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class); |
| 225 | if (flowEntry != null) { |
| 226 | flowEntry.setType("flow_entry"); |
| 227 | } |
| 228 | return flowEntry; |
| 229 | } |
| 230 | |
| 231 | |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 232 | public IIpv4Address newIpv4Address() { |
| 233 | return newVertex("ipv4Address", IIpv4Address.class); |
| 234 | } |
| 235 | private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) { |
| 236 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 237 | T newVertex = fg.addVertex(null, vertexType); |
| 238 | if (newVertex != null) { |
| 239 | newVertex.setType(type); |
| 240 | } |
| 241 | return newVertex; |
| 242 | } |
yoshi | c455c01 | 2013-11-27 10:35:50 -0800 | [diff] [blame^] | 243 | |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 244 | public IIpv4Address searchIpv4Address(int intIpv4Address) { |
| 245 | return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | public IIpv4Address ensureIpv4Address(int intIpv4Address) { |
| 250 | IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address); |
| 251 | if (ipv4Vertex == null) { |
| 252 | ipv4Vertex = newIpv4Address(); |
| 253 | ipv4Vertex.setIpv4Address(intIpv4Address); |
| 254 | } |
| 255 | return ipv4Vertex; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) { |
| 260 | if (conn.getFramedGraph() != null) { |
| 261 | Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator(); |
| 262 | if (it.hasNext()) { |
| 263 | return it.next(); |
| 264 | } |
| 265 | } |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | public void removeIpv4Address(IIpv4Address ipv4Address) { |
| 270 | conn.getFramedGraph().removeVertex(ipv4Address.asVertex()); |
| 271 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 272 | |
yoshi | 7594aef | 2013-11-27 09:27:07 -0800 | [diff] [blame] | 273 | |
| 274 | @Override |
| 275 | public IDBConnection getDBConnection() { |
| 276 | return conn; |
| 277 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 278 | } |