blob: 5dd5aaa546c91a6c48b0da9b1e56cd2c349c5c32 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.graph;
6
7import com.thinkaurelius.titan.core.TitanGraph;
8import com.tinkerpop.blueprints.Vertex;
9import com.tinkerpop.frames.FramedGraph;
10import com.tinkerpop.frames.structures.FramedVertexIterable;
11import com.tinkerpop.gremlin.java.GremlinPipeline;
12import java.util.ArrayList;
13import java.util.Iterator;
14import java.util.List;
15import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
yoshitomob292c622013-11-23 14:35:58 -080016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject;
yoshi0451f282013-11-22 15:48:55 -080017import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
19import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
yoshitomob292c622013-11-23 14:35:58 -080020import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
yoshi0451f282013-11-22 15:48:55 -080021import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
22import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
23import net.onrc.onos.ofcontroller.core.ISwitchStorage;
24import net.onrc.onos.ofcontroller.util.FlowId;
25
26/**
27 *
28 * @author nickkaranatsios
29 */
30public abstract class DBOperation implements IDBOperation {
31
yoshic455c012013-11-27 10:35:50 -080032 protected DBConnection conn;
yoshi0451f282013-11-22 15:48:55 -080033
yoshic455c012013-11-27 10:35:50 -080034 @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 }
yoshi0451f282013-11-22 15:48:55 -080043
yoshic455c012013-11-27 10:35:50 -080044 @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 }
yoshi0451f282013-11-22 15:48:55 -080053
yoshic455c012013-11-27 10:35:50 -080054 @Override
55 public Iterable<ISwitchObject> getAllSwitches() {
56 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
57 return switches;
58 }
yoshi0451f282013-11-22 15:48:55 -080059
yoshic455c012013-11-27 10:35:50 -080060 @Override
61 public Iterable<ISwitchObject> getInactiveSwitches() {
62 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
63 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080064
yoshic455c012013-11-27 10:35:50 -080065 for (ISwitchObject sw : switches) {
66 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
67 inactiveSwitches.add(sw);
68 }
69 }
70 return inactiveSwitches;
71 }
yoshi0451f282013-11-22 15:48:55 -080072
yoshic455c012013-11-27 10:35:50 -080073 @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);
yoshi0451f282013-11-22 15:48:55 -080077
yoshic455c012013-11-27 10:35:50 -080078 }
yoshi0451f282013-11-22 15:48:55 -080079
yoshic455c012013-11-27 10:35:50 -080080 @Override
81 public void removeSwitch(ISwitchObject sw) {
82 conn.getFramedGraph().removeVertex(sw.asVertex());
83 }
yoshi0451f282013-11-22 15:48:55 -080084
yoshic455c012013-11-27 10:35:50 -080085 @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 }
yoshi0451f282013-11-22 15:48:55 -080096
yoshic455c012013-11-27 10:35:50 -080097 /**
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 }
yoshi0451f282013-11-22 15:48:55 -0800111
yoshic455c012013-11-27 10:35:50 -0800112 @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;
yoshi0451f282013-11-22 15:48:55 -0800117
yoshic455c012013-11-27 10:35:50 -0800118 }
yoshi0451f282013-11-22 15:48:55 -0800119
yoshic455c012013-11-27 10:35:50 -0800120 @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 }
yoshi0451f282013-11-22 15:48:55 -0800140
141
yoshic455c012013-11-27 10:35:50 -0800142 @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 */
yoshi0451f282013-11-22 15:48:55 -0800152
yoshic455c012013-11-27 10:35:50 -0800153 @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 }
yoshi0451f282013-11-22 15:48:55 -0800161
yoshic455c012013-11-27 10:35:50 -0800162 @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 }
yoshi0451f282013-11-22 15:48:55 -0800170
yoshi0451f282013-11-22 15:48:55 -0800171
yoshic455c012013-11-27 10:35:50 -0800172 /**
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 }
yoshi0451f282013-11-22 15:48:55 -0800182
yoshic455c012013-11-27 10:35:50 -0800183 @Override
184 public Iterable<ISwitchObject> getActiveSwitches() {
185 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
186 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800187
yoshic455c012013-11-27 10:35:50 -0800188 for (ISwitchObject sw : switches) {
189 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
190 activeSwitches.add(sw);
191 }
192 }
193 return activeSwitches;
194 }
yoshi0451f282013-11-22 15:48:55 -0800195
yoshic455c012013-11-27 10:35:50 -0800196 @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
yoshitomob292c622013-11-23 14:35:58 -0800232 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 }
yoshic455c012013-11-27 10:35:50 -0800243
yoshitomob292c622013-11-23 14:35:58 -0800244 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 }
yoshi0451f282013-11-22 15:48:55 -0800272
yoshi7594aef2013-11-27 09:27:07 -0800273
274 @Override
275 public IDBConnection getDBConnection() {
276 return conn;
277 }
yoshi0451f282013-11-22 15:48:55 -0800278}