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