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