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