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