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