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