blob: eae323f0cc7c16e2b7cdcd04707d988a9cf9dfe9 [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
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 }
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
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 }
96
97 public IPortObject searchPort(String dpid, Short number, final FramedGraph fg) {
98 String id = dpid + number.toString();
99 return (fg != null && fg.getVertices("port_id", id).iterator().hasNext())
100 ? (IPortObject) fg.getVertices("port_id", id, IPortObject.class).iterator().next() : null;
101
102 }
103
104 @Override
105 public IDeviceObject newDevice() {
106 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
107 if (obj != null) {
108 obj.setType("device");
109 }
110 return obj;
111 }
112
113 @Override
114 public IFlowPath newFlowPath() {
115 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
116 if (flowPath != null) {
117 flowPath.setType("flow");
118 }
119 return flowPath;
120 }
121
122 @Override
123 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
124 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
125 pipe.start(flowEntry.asVertex());
126 pipe.out("flow");
127 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
128 return r.iterator().hasNext() ? r.iterator().next() : null;
129 }
130
131
132
133 protected ISwitchObject searchSwitch(final String dpid, final FramedGraph fg) {
134 return (fg != null && fg.getVertices("dpid", dpid).iterator().hasNext())
135 ? (ISwitchObject) (fg.getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
136 }
137
138 protected Iterable<ISwitchObject> getActiveSwitches(final FramedGraph fg) {
139 Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
140 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
141
142 for (ISwitchObject sw : switches) {
143 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
144 activeSwitches.add(sw);
145 }
146 }
147 return activeSwitches;
148 }
149
150 protected Iterable<ISwitchObject> getAllSwitches(final FramedGraph fg) {
151 Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class);
152 return switches;
153 }
154
155 protected IDeviceObject searchDevice(String macAddr, final FramedGraph fg) {
156 return (fg != null && fg.getVertices("dl_addr", macAddr).iterator().hasNext())
157 ? (IDeviceObject) fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
158
159 }
160
161 protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) {
162 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext()
163 ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(),
164 IFlowPath.class).iterator().next() : null;
165 }
166
167 protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) {
168 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
169
170 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
171
172 for (IFlowPath fp : flowPaths) {
173 if (fp.getFlowId() != null) {
174 nonNullFlows.add(fp);
175 }
176 }
177 return nonNullFlows;
178 }
179
180 protected IFlowEntry newFlowEntry(final FramedGraph fg) {
181 IFlowEntry flowEntry = (IFlowEntry) fg.addVertex(null, IFlowEntry.class);
182 if (flowEntry != null) {
183 flowEntry.setType("flow_entry");
184 }
185 return flowEntry;
186 }
yoshitomob292c622013-11-23 14:35:58 -0800187
188
189 public IIpv4Address newIpv4Address() {
190 return newVertex("ipv4Address", IIpv4Address.class);
191 }
192 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
193 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
194 T newVertex = fg.addVertex(null, vertexType);
195 if (newVertex != null) {
196 newVertex.setType(type);
197 }
198 return newVertex;
199 }
200
201 public IIpv4Address searchIpv4Address(int intIpv4Address) {
202 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
203 }
204
205
206 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
207 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
208 if (ipv4Vertex == null) {
209 ipv4Vertex = newIpv4Address();
210 ipv4Vertex.setIpv4Address(intIpv4Address);
211 }
212 return ipv4Vertex;
213 }
214
215
216 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
217 if (conn.getFramedGraph() != null) {
218 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
219 if (it.hasNext()) {
220 return it.next();
221 }
222 }
223 return null;
224 }
225
226 public void removeIpv4Address(IIpv4Address ipv4Address) {
227 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
228 }
yoshi0451f282013-11-22 15:48:55 -0800229
230}