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 | 5fb2763 | 2013-04-05 08:56:12 -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; |
Pankaj Berde | 73ea7e5 | 2013-04-04 14:52:50 -0700 | [diff] [blame] | 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 | 73ea7e5 | 2013-04-04 14:52:50 -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 |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 147 | public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) { |
| 148 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 149 | |
| 150 | return fg.getVertices("type", "flow", IFlowPath.class); |
| 151 | } |
| 152 | |
| 153 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 154 | public IFlowEntry searchFlowEntry(GraphDBConnection conn, |
| 155 | FlowEntryId flowEntryId) { |
| 156 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 157 | |
| 158 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ? |
| 159 | fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 160 | IFlowEntry.class).iterator().next() : null; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public IFlowEntry newFlowEntry(GraphDBConnection conn) { |
| 165 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 166 | IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class); |
| 167 | return flowEntry; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void removeFlowEntry(GraphDBConnection conn, |
| 172 | IFlowEntry flowEntry) { |
| 173 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 174 | fg.removeVertex(flowEntry.asVertex()); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) { |
| 179 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 180 | |
| 181 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 182 | } |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 183 | |
| 184 | @Override |
| 185 | public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) { |
| 186 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 187 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 188 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
| 189 | |
| 190 | for (ISwitchObject sw: switches) { |
| 191 | if(sw.getState().equals(SwitchState.ACTIVE.toString())) { |
| 192 | activeSwitches.add(sw); |
| 193 | } |
| 194 | } |
| 195 | return activeSwitches; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) { |
| 200 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 201 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 202 | return switches; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) { |
| 207 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 208 | Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class); |
| 209 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
| 210 | |
| 211 | for (ISwitchObject sw: switches) { |
| 212 | if(sw.getState().equals(SwitchState.INACTIVE.toString())) { |
| 213 | inactiveSwitches.add(sw); |
| 214 | } |
| 215 | } |
| 216 | return inactiveSwitches; |
| 217 | } |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) { |
| 221 | // TODO Auto-generated method stub |
| 222 | return null; |
| 223 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 224 | } |