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); |
| 32 | return obj; |
| 33 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 34 | |
| 35 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 36 | public void removeSwitch(ISwitchObject sw) { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 37 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 38 | fg.removeVertex(sw.asVertex()); |
| 39 | } |
| 40 | |
| 41 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 42 | public ISwitchObject searchSwitch(String dpid) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 43 | // TODO Auto-generated method stub |
| 44 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 45 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 46 | return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ? |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 47 | fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 52 | public IDeviceObject searchDevice(String macAddr) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 53 | // TODO Auto-generated method stub |
| 54 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 55 | 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] | 56 | IDeviceObject.class).iterator().next() : null; |
| 57 | |
| 58 | } |
| 59 | |
| 60 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 61 | public IPortObject searchPort(String dpid, short number) { |
| 62 | ISwitchObject sw = searchSwitch(dpid); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 63 | if (sw != null) { |
Pankaj Berde | c165a52 | 2013-06-10 21:28:11 -0700 | [diff] [blame] | 64 | |
| 65 | IPortObject port = null; |
| 66 | |
| 67 | // Requires Frames 2.3.0 |
| 68 | |
| 69 | try { |
| 70 | port = sw.getPort(number); |
| 71 | } catch (Exception e) { |
| 72 | // TODO Auto-generated catch block |
| 73 | e.printStackTrace(); |
| 74 | } |
| 75 | |
| 76 | return port; |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 77 | } |
Pankaj Berde | c165a52 | 2013-06-10 21:28:11 -0700 | [diff] [blame] | 78 | |
| 79 | // if (sw != null) { |
| 80 | // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>(); |
| 81 | // pipe.start(sw.asVertex()); |
| 82 | // pipe.out("on").has("number", number); |
| 83 | // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class); |
| 84 | // return r != null && r.iterator().hasNext() ? r.iterator().next() : null; |
| 85 | // } |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 86 | return null; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 90 | public IPortObject newPort() { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 91 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 92 | IPortObject obj = fg.addVertex(null,IPortObject.class); |
| 93 | return obj; |
| 94 | } |
| 95 | |
| 96 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 97 | public IDeviceObject newDevice() { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 98 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 99 | IDeviceObject obj = fg.addVertex(null,IDeviceObject.class); |
| 100 | return obj; |
| 101 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 102 | |
| 103 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 104 | public void removePort(IPortObject port) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 105 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 106 | // EventGraph<TitanGraph> eg = conn.getEventGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 107 | if (fg != null) fg.removeVertex(port.asVertex()); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 108 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 109 | |
| 110 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 111 | public void removeDevice(IDeviceObject dev) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 112 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 113 | if (fg != null) fg.removeVertex(dev.asVertex()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 116 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 117 | public Iterable<IDeviceObject> getDevices() { |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 118 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 119 | return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null; |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 122 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 123 | public IFlowPath searchFlowPath(FlowId flowId) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 124 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 125 | |
| 126 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ? |
| 127 | fg.getVertices("flow_id", flowId.toString(), |
| 128 | IFlowPath.class).iterator().next() : null; |
| 129 | } |
| 130 | |
| 131 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 132 | public IFlowPath newFlowPath() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 133 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 134 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
| 135 | return flowPath; |
| 136 | } |
| 137 | |
| 138 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 139 | public void removeFlowPath(IFlowPath flowPath) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 140 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 141 | fg.removeVertex(flowPath.asVertex()); |
| 142 | } |
| 143 | |
| 144 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 145 | public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 146 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 147 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 148 | pipe.start(flowEntry.asVertex()); |
| 149 | pipe.out("flow"); |
Pankaj Berde | 8f03611 | 2013-03-28 22:58:47 -0700 | [diff] [blame] | 150 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 151 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 152 | } |
| 153 | |
| 154 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 155 | public Iterable<IFlowPath> getAllFlowPaths() { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 156 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 157 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 158 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 159 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 160 | |
| 161 | for (IFlowPath fp: flowPaths) { |
| 162 | if (fp.getFlowId() != null) { |
| 163 | nonNullFlows.add(fp); |
| 164 | } |
| 165 | } |
| 166 | return nonNullFlows; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 170 | public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 171 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 172 | |
| 173 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 174 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 175 | IFlowEntry.class).iterator().next() : null; |
| 176 | } |
| 177 | |
| 178 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 179 | public IFlowEntry newFlowEntry() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 180 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 181 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
| 182 | return flowEntry; |
| 183 | } |
| 184 | |
| 185 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 186 | public void removeFlowEntry(IFlowEntry flowEntry) { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 187 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 188 | fg.removeVertex(flowEntry.asVertex()); |
| 189 | } |
| 190 | |
| 191 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 192 | public Iterable<IFlowEntry> getAllFlowEntries() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 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 |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 199 | public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() { |
Pankaj Berde | d1c3859 | 2013-04-10 22:46:40 -0700 | [diff] [blame] | 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 |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 206 | public Iterable<ISwitchObject> getActiveSwitches() { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 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 |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 220 | public Iterable<ISwitchObject> getAllSwitches() { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 221 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 222 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 223 | return switches; |
| 224 | } |
| 225 | |
| 226 | @Override |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 227 | public Iterable<ISwitchObject> getInactiveSwitches() { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 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 |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 241 | public ISwitchObject searchActiveSwitch(String dpid) { |
Pankaj Berde | 61ce908 | 2013-04-09 19:38:28 -0700 | [diff] [blame] | 242 | |
Toshio Koide | eb88ff6 | 2013-06-12 16:46:40 -0700 | [diff] [blame] | 243 | ISwitchObject sw = searchSwitch(dpid); |
Pankaj Berde | 61ce908 | 2013-04-09 19:38:28 -0700 | [diff] [blame] | 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 | } |