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