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