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