Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.onrc.onos.util; |
| 2 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 6 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 7 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 8 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 11 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame^] | 12 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 13 | import net.onrc.onos.ofcontroller.util.FlowId; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 14 | |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 15 | import com.thinkaurelius.titan.core.TitanGraph; |
| 16 | import com.tinkerpop.blueprints.Vertex; |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 17 | import com.tinkerpop.frames.FramedGraph; |
Pankaj Berde | 5d50641 | 2013-04-23 15:03:02 -0700 | [diff] [blame] | 18 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 19 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 20 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 21 | public class GraphDBUtils implements IDBUtils { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 22 | |
| 23 | @Override |
| 24 | public ISwitchObject newSwitch(GraphDBConnection conn) { |
| 25 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 26 | ISwitchObject obj = fg.addVertex(null,ISwitchObject.class); |
| 27 | return obj; |
| 28 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 29 | |
| 30 | @Override |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 31 | public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) { |
| 32 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 33 | fg.removeVertex(sw.asVertex()); |
| 34 | } |
| 35 | |
| 36 | @Override |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 37 | public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) { |
| 38 | // TODO Auto-generated method stub |
| 39 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 40 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 41 | return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ? |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 42 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) { |
| 48 | // TODO Auto-generated method stub |
| 49 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 50 | return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr, |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 51 | IDeviceObject.class).iterator().next() : null; |
| 52 | |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) { |
| 57 | ISwitchObject sw = searchSwitch(conn, dpid); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 58 | if (sw != null) { |
Pankaj Berde | c165a52 | 2013-06-10 21:28:11 -0700 | [diff] [blame] | 59 | |
| 60 | IPortObject port = null; |
| 61 | |
| 62 | // Requires Frames 2.3.0 |
| 63 | |
| 64 | try { |
| 65 | port = sw.getPort(number); |
| 66 | } catch (Exception e) { |
| 67 | // TODO Auto-generated catch block |
| 68 | e.printStackTrace(); |
| 69 | } |
| 70 | |
| 71 | return port; |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 72 | } |
Pankaj Berde | c165a52 | 2013-06-10 21:28:11 -0700 | [diff] [blame] | 73 | |
| 74 | // if (sw != null) { |
| 75 | // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>(); |
| 76 | // pipe.start(sw.asVertex()); |
| 77 | // pipe.out("on").has("number", number); |
| 78 | // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class); |
| 79 | // return r != null && r.iterator().hasNext() ? r.iterator().next() : null; |
| 80 | // } |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 81 | return null; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 85 | public IPortObject newPort(GraphDBConnection conn) { |
| 86 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 87 | IPortObject obj = fg.addVertex(null,IPortObject.class); |
| 88 | return obj; |
| 89 | } |
| 90 | |
| 91 | @Override |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 92 | public IDeviceObject newDevice(GraphDBConnection conn) { |
| 93 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 94 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
| 95 | return obj; |
| 96 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public void removePort(GraphDBConnection conn, IPortObject port) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 100 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 101 | // EventGraph<TitanGraph> eg = conn.getEventGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 102 | if (fg != null) fg.removeVertex(port.asVertex()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 103 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public void removeDevice(GraphDBConnection conn, IDeviceObject dev) { |
| 107 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 108 | if (fg != null) fg.removeVertex(dev.asVertex()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) { |
| 113 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 114 | return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null; |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 117 | @Override |
| 118 | public IFlowPath searchFlowPath(GraphDBConnection conn, |
| 119 | FlowId flowId) { |
| 120 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 121 | |
| 122 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ? |
| 123 | fg.getVertices("flow_id", flowId.toString(), |
| 124 | IFlowPath.class).iterator().next() : null; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public IFlowPath newFlowPath(GraphDBConnection conn) { |
| 129 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 130 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
| 131 | return flowPath; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void removeFlowPath(GraphDBConnection conn, |
| 136 | IFlowPath flowPath) { |
| 137 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 138 | fg.removeVertex(flowPath.asVertex()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn, |
| 143 | IFlowEntry flowEntry) { |
| 144 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 145 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 146 | pipe.start(flowEntry.asVertex()); |
| 147 | pipe.out("flow"); |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 148 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 149 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 150 | } |
| 151 | |
| 152 | @Override |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 153 | public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 154 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 155 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 156 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 157 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 158 | |
| 159 | for (IFlowPath fp: flowPaths) { |
| 160 | if (fp.getFlowId() != null) { |
| 161 | nonNullFlows.add(fp); |
| 162 | } |
| 163 | } |
| 164 | return nonNullFlows; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 168 | public IFlowEntry searchFlowEntry(GraphDBConnection conn, |
| 169 | FlowEntryId flowEntryId) { |
| 170 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 171 | |
| 172 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 173 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 174 | IFlowEntry.class).iterator().next() : null; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public IFlowEntry newFlowEntry(GraphDBConnection conn) { |
| 179 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 180 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
| 181 | return flowEntry; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void removeFlowEntry(GraphDBConnection conn, |
| 186 | IFlowEntry flowEntry) { |
| 187 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 188 | fg.removeVertex(flowEntry.asVertex()); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) { |
| 193 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 194 | |
| 195 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 196 | } |
Pankaj Berde | d1c3859 | 2013-04-10 22:46:40 -0700 | [diff] [blame] | 197 | |
| 198 | @Override |
| 199 | public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries(GraphDBConnection conn) { |
| 200 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 201 | //TODO: Should use an enum for flow_switch_state |
| 202 | return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class); |
| 203 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 204 | |
| 205 | @Override |
| 206 | public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) { |
| 207 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 208 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 209 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
| 210 | |
| 211 | for (ISwitchObject sw: switches) { |
| 212 | if(sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 213 | activeSwitches.add(sw); |
| 214 | } |
| 215 | } |
| 216 | return activeSwitches; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) { |
| 221 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 222 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 223 | return switches; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) { |
| 228 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 229 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 230 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
| 231 | |
| 232 | for (ISwitchObject sw: switches) { |
| 233 | if(sw.getState().equals(SwitchState.INACTIVE.toString())) { |
| 234 | inactiveSwitches.add(sw); |
| 235 | } |
| 236 | } |
| 237 | return inactiveSwitches; |
| 238 | } |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 239 | |
| 240 | @Override |
| 241 | public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) { |
Pankaj Berde | 61ce908 | 2013-04-09 19:38:28 -0700 | [diff] [blame] | 242 | |
| 243 | ISwitchObject sw = searchSwitch(conn, dpid); |
| 244 | if ((sw != null) && |
| 245 | sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 246 | return sw; |
| 247 | } |
| 248 | return null; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 249 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 250 | } |