yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.graph; |
| 6 | |
| 7 | import com.thinkaurelius.titan.core.TitanGraph; |
| 8 | import com.tinkerpop.blueprints.Vertex; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
| 11 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 12 | import java.util.ArrayList; |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 13 | import java.util.Iterator; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 14 | import java.util.List; |
| 15 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.*; |
| 16 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 17 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 18 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 19 | |
| 20 | /** |
| 21 | * |
| 22 | * @author nickkaranatsios |
| 23 | */ |
| 24 | public class TitanDBOperation extends DBOperation { |
| 25 | |
| 26 | /** |
| 27 | * Search and get a switch object with DPID. |
| 28 | * |
| 29 | * @param dpid DPID of the switch |
| 30 | */ |
| 31 | @Override |
| 32 | public ISwitchObject searchSwitch(String dpid) { |
| 33 | final FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 34 | |
| 35 | return searchSwitch(dpid, fg); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public IPortObject newPort(String dpid, Short portNum) { |
| 40 | return newPort(dpid, portNum); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public Iterable<ISwitchObject> getActiveSwitches() { |
| 45 | final FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 46 | |
| 47 | return getActiveSwitches(fg); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public IPortObject searchPort(String dpid, Short number) { |
| 52 | final FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 53 | return searchPort(dpid, number, fg); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | @Override |
| 58 | public void removePort(IPortObject port) { |
| 59 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 60 | if (fg != null) { |
| 61 | fg.removeVertex(port.asVertex()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | @Override |
| 67 | public IDeviceObject searchDevice(String macAddr) { |
| 68 | // TODO Auto-generated method stub |
| 69 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 70 | return searchDevice(macAddr, fg); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Iterable<IDeviceObject> getDevices() { |
| 75 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 76 | return fg != null ? fg.getVertices("type", "device", IDeviceObject.class) : null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void removeDevice(IDeviceObject dev) { |
| 81 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 82 | if (fg != null) { |
| 83 | fg.removeVertex(dev.asVertex()); |
| 84 | } |
| 85 | } |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Create and return a flow path object. |
| 89 | */ |
| 90 | public IFlowPath newFlowPath() { |
| 91 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 92 | IFlowPath flowPath = fg.addVertex(null, IFlowPath.class); |
| 93 | if (flowPath != null) flowPath.setType("flow"); |
| 94 | return flowPath; |
| 95 | } |
| 96 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 97 | @Override |
| 98 | public IFlowPath searchFlowPath(FlowId flowId) { |
| 99 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 100 | return searchFlowPath(flowId, fg); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | @Override |
| 105 | public Iterable<IFlowPath> getAllFlowPaths() { |
| 106 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 107 | return getAllFlowPaths(fg); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void removeFlowPath(IFlowPath flowPath) { |
| 112 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 113 | fg.removeVertex(flowPath.asVertex()); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public IFlowEntry newFlowEntry() { |
| 118 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 119 | return newFlowEntry(fg); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) { |
| 124 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 125 | |
| 126 | return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() |
| 127 | ? fg.getVertices("flow_entry_id", flowEntryId.toString(), |
| 128 | IFlowEntry.class).iterator().next() : null; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Iterable<IFlowEntry> getAllFlowEntries() { |
| 133 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 134 | |
| 135 | return fg.getVertices("type", "flow_entry", IFlowEntry.class); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void removeFlowEntry(IFlowEntry flowEntry) { |
| 140 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 141 | fg.removeVertex(flowEntry.asVertex()); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public IDBConnection getDBConnection() { |
yoshi | a0839b5 | 2013-11-25 16:42:10 -0800 | [diff] [blame] | 146 | System.out.println("TitangetDBConnection"); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 147 | return conn; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void commit() { |
| 152 | conn.commit(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void rollback() { |
| 157 | conn.rollback(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void close() { |
| 162 | conn.close(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Create a port having specified port number. |
| 167 | * |
| 168 | * @param portNumber port number |
| 169 | */ |
| 170 | @Deprecated |
| 171 | public IPortObject newPort(Short portNumber) { |
| 172 | FramedGraph<TitanGraph> fg = conn.getFramedGraph(); |
| 173 | IPortObject obj = fg.addVertex(null, IPortObject.class); |
| 174 | if (obj != null) { |
| 175 | obj.setType("port"); |
| 176 | obj.setNumber(portNumber); |
| 177 | } |
| 178 | return obj; |
| 179 | } |
| 180 | } |