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