blob: 2410ed70bddc614809064e5cb5f910306cb3a2b1 [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
yoshi0bc07212013-11-27 02:23:51 -080044 public ISwitchObject newSwitch(final String dpid, final FramedGraph fg) {
45 ISwitchObject obj = (ISwitchObject) fg.addVertex(null, ISwitchObject.class);
yoshi0451f282013-11-22 15:48:55 -080046 if (obj != null) {
47 obj.setType("switch");
48 obj.setDPID(dpid);
49 }
50 return obj;
51 }
52
53 @Override
54 public Iterable<ISwitchObject> getAllSwitches() {
55 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
56 return switches;
57 }
58
59 @Override
60 public Iterable<ISwitchObject> getInactiveSwitches() {
61 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
62 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
63
64 for (ISwitchObject sw : switches) {
65 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
66 inactiveSwitches.add(sw);
67 }
68 }
69 return inactiveSwitches;
70 }
71
72 @Override
73 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
74 //TODO: Should use an enum for flow_switch_state
75 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
76
77 }
78
79 @Override
80 public void removeSwitch(ISwitchObject sw) {
81 conn.getFramedGraph().removeVertex(sw.asVertex());
82 }
83
yoshi0bc07212013-11-27 02:23:51 -080084 public IPortObject newPort(String dpid, Short portNum, final FramedGraph fg) {
85 IPortObject obj = (IPortObject) fg.addVertex(null, IPortObject.class);
yoshi0451f282013-11-22 15:48:55 -080086 if (obj != null) {
87 obj.setType("port");
88 String id = dpid + portNum.toString();
89 obj.setPortId(id);
90 obj.setNumber(portNum);
91 }
92 return obj;
93 }
yoshi8bc311d2013-11-27 01:53:40 -080094
95 /**
96 * Create a port having specified port number.
97 *
98 * @param portNumber port number
99 */
100 @Deprecated
101 public IPortObject newPort(Short portNumber) {
102 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
103 if (obj != null) {
104 obj.setType("port");
105 obj.setNumber(portNumber);
106 }
107 return obj;
108 }
yoshi0451f282013-11-22 15:48:55 -0800109
110 public IPortObject searchPort(String dpid, Short number, final FramedGraph fg) {
111 String id = dpid + number.toString();
112 return (fg != null && fg.getVertices("port_id", id).iterator().hasNext())
113 ? (IPortObject) fg.getVertices("port_id", id, IPortObject.class).iterator().next() : null;
114
115 }
116
yoshi52806992013-11-27 02:15:47 -0800117 public IDeviceObject newDevice(final FramedGraph fg) {
118 IDeviceObject obj = (IDeviceObject) fg.addVertex(null, IDeviceObject.class);
yoshi0451f282013-11-22 15:48:55 -0800119 if (obj != null) {
120 obj.setType("device");
121 }
122 return obj;
123 }
124
yoshi8bc311d2013-11-27 01:53:40 -0800125 /**
126 * Create and return a flow path object.
127 */
128
yoshi0451f282013-11-22 15:48:55 -0800129 @Override
130 public IFlowPath newFlowPath() {
131 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
132 if (flowPath != null) {
133 flowPath.setType("flow");
134 }
135 return flowPath;
136 }
137
138 @Override
139 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
140 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
141 pipe.start(flowEntry.asVertex());
142 pipe.out("flow");
143 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
144 return r.iterator().hasNext() ? r.iterator().next() : null;
145 }
146
147
148
149 protected ISwitchObject searchSwitch(final String dpid, final FramedGraph fg) {
150 return (fg != null && fg.getVertices("dpid", dpid).iterator().hasNext())
151 ? (ISwitchObject) (fg.getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
152 }
153
154 protected Iterable<ISwitchObject> getActiveSwitches(final FramedGraph fg) {
155 Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
156 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
157
158 for (ISwitchObject sw : switches) {
159 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
160 activeSwitches.add(sw);
161 }
162 }
163 return activeSwitches;
164 }
165
166 protected Iterable<ISwitchObject> getAllSwitches(final FramedGraph fg) {
167 Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
168 return switches;
169 }
170
171 protected IDeviceObject searchDevice(String macAddr, final FramedGraph fg) {
172 return (fg != null && fg.getVertices("dl_addr", macAddr).iterator().hasNext())
173 ? (IDeviceObject) fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
174
175 }
176
177 protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) {
178 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext()
179 ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(),
180 IFlowPath.class).iterator().next() : null;
181 }
182
183 protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) {
184 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
185
186 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
187
188 for (IFlowPath fp : flowPaths) {
189 if (fp.getFlowId() != null) {
190 nonNullFlows.add(fp);
191 }
192 }
193 return nonNullFlows;
194 }
195
yoshi8bc311d2013-11-27 01:53:40 -0800196 @Override
197 public IFlowEntry newFlowEntry() {
198 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -0800199 if (flowEntry != null) {
200 flowEntry.setType("flow_entry");
201 }
202 return flowEntry;
203 }
yoshitomob292c622013-11-23 14:35:58 -0800204
205
206 public IIpv4Address newIpv4Address() {
207 return newVertex("ipv4Address", IIpv4Address.class);
208 }
209 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
210 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
211 T newVertex = fg.addVertex(null, vertexType);
212 if (newVertex != null) {
213 newVertex.setType(type);
214 }
215 return newVertex;
216 }
217
218 public IIpv4Address searchIpv4Address(int intIpv4Address) {
219 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
220 }
221
222
223 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
224 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
225 if (ipv4Vertex == null) {
226 ipv4Vertex = newIpv4Address();
227 ipv4Vertex.setIpv4Address(intIpv4Address);
228 }
229 return ipv4Vertex;
230 }
231
232
233 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
234 if (conn.getFramedGraph() != null) {
235 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
236 if (it.hasNext()) {
237 return it.next();
238 }
239 }
240 return null;
241 }
242
243 public void removeIpv4Address(IIpv4Address ipv4Address) {
244 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
245 }
yoshi0451f282013-11-22 15:48:55 -0800246
247}