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