blob: 9b89bede923cb34043c412b730de65ad2a6f47c6 [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
32 protected DBConnection conn;
33
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 }
43
yoshi7594aef2013-11-27 09:27:07 -080044 @Override
45 public ISwitchObject newSwitch(final String dpid) {
46 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
yoshi0451f282013-11-22 15:48:55 -080047 if (obj != null) {
48 obj.setType("switch");
49 obj.setDPID(dpid);
50 }
51 return obj;
52 }
53
54 @Override
55 public Iterable<ISwitchObject> getAllSwitches() {
56 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
57 return switches;
58 }
59
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>();
64
65 for (ISwitchObject sw : switches) {
66 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
67 inactiveSwitches.add(sw);
68 }
69 }
70 return inactiveSwitches;
71 }
72
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);
77
78 }
79
80 @Override
81 public void removeSwitch(ISwitchObject sw) {
82 conn.getFramedGraph().removeVertex(sw.asVertex());
83 }
84
yoshi7594aef2013-11-27 09:27:07 -080085 @Override
86 public IPortObject newPort(String dpid, Short portNum) {
87 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
yoshi0451f282013-11-22 15:48:55 -080088 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 }
yoshi8bc311d2013-11-27 01:53:40 -080096
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 }
yoshi0451f282013-11-22 15:48:55 -0800111
yoshi7594aef2013-11-27 09:27:07 -0800112 @Override
113 public IPortObject searchPort(String dpid, Short number) {
yoshi0451f282013-11-22 15:48:55 -0800114 String id = dpid + number.toString();
yoshi7594aef2013-11-27 09:27:07 -0800115 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
118 }
119
yoshi7594aef2013-11-27 09:27:07 -0800120 @Override
121 public IDeviceObject newDevice() {
122 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
yoshi0451f282013-11-22 15:48:55 -0800123 if (obj != null) {
124 obj.setType("device");
125 }
126 return obj;
127 }
128
yoshi8bc311d2013-11-27 01:53:40 -0800129 /**
130 * Create and return a flow path object.
131 */
132
yoshi0451f282013-11-22 15:48:55 -0800133 @Override
134 public IFlowPath newFlowPath() {
135 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
136 if (flowPath != null) {
137 flowPath.setType("flow");
138 }
139 return flowPath;
140 }
141
142 @Override
143 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
144 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
145 pipe.start(flowEntry.asVertex());
146 pipe.out("flow");
147 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
148 return r.iterator().hasNext() ? r.iterator().next() : null;
149 }
150
151
yoshi7594aef2013-11-27 09:27:07 -0800152 /**
153 * Search and get a switch object with DPID.
154 *
155 * @param dpid DPID of the switch
156 */
157 @Override
158 public ISwitchObject searchSwitch(final String dpid) {
159 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext())
160 ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
yoshi0451f282013-11-22 15:48:55 -0800161 }
162
yoshi7594aef2013-11-27 09:27:07 -0800163 @Override
164 public Iterable<ISwitchObject> getActiveSwitches() {
165 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
yoshi0451f282013-11-22 15:48:55 -0800166 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
167
168 for (ISwitchObject sw : switches) {
169 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
170 activeSwitches.add(sw);
171 }
172 }
173 return activeSwitches;
174 }
175
176 protected Iterable<ISwitchObject> getAllSwitches(final FramedGraph fg) {
177 Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
178 return switches;
179 }
180
181 protected IDeviceObject searchDevice(String macAddr, final FramedGraph fg) {
182 return (fg != null && fg.getVertices("dl_addr", macAddr).iterator().hasNext())
183 ? (IDeviceObject) fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
184
185 }
186
187 protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) {
188 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext()
189 ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(),
190 IFlowPath.class).iterator().next() : null;
191 }
192
193 protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) {
194 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
195
196 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
197
198 for (IFlowPath fp : flowPaths) {
199 if (fp.getFlowId() != null) {
200 nonNullFlows.add(fp);
201 }
202 }
203 return nonNullFlows;
204 }
205
yoshi8bc311d2013-11-27 01:53:40 -0800206 @Override
207 public IFlowEntry newFlowEntry() {
208 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -0800209 if (flowEntry != null) {
210 flowEntry.setType("flow_entry");
211 }
212 return flowEntry;
213 }
yoshitomob292c622013-11-23 14:35:58 -0800214
215
216 public IIpv4Address newIpv4Address() {
217 return newVertex("ipv4Address", IIpv4Address.class);
218 }
219 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
220 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
221 T newVertex = fg.addVertex(null, vertexType);
222 if (newVertex != null) {
223 newVertex.setType(type);
224 }
225 return newVertex;
226 }
227
228 public IIpv4Address searchIpv4Address(int intIpv4Address) {
229 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
230 }
231
232
233 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
234 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
235 if (ipv4Vertex == null) {
236 ipv4Vertex = newIpv4Address();
237 ipv4Vertex.setIpv4Address(intIpv4Address);
238 }
239 return ipv4Vertex;
240 }
241
242
243 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
244 if (conn.getFramedGraph() != null) {
245 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
246 if (it.hasNext()) {
247 return it.next();
248 }
249 }
250 return null;
251 }
252
253 public void removeIpv4Address(IIpv4Address ipv4Address) {
254 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
255 }
yoshi0451f282013-11-22 15:48:55 -0800256
yoshi7594aef2013-11-27 09:27:07 -0800257
258 @Override
259 public IDBConnection getDBConnection() {
260 return conn;
261 }
yoshi0451f282013-11-22 15:48:55 -0800262}