Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 1 | package net.onrc.onos.graph; |
| 2 | |
| 3 | import java.util.Set; |
| 4 | |
| 5 | import org.slf4j.Logger; |
| 6 | import org.slf4j.LoggerFactory; |
| 7 | |
| 8 | import com.thinkaurelius.titan.core.TitanFactory; |
| 9 | import com.thinkaurelius.titan.core.TitanGraph; |
| 10 | import com.tinkerpop.blueprints.TransactionalGraph; |
| 11 | import com.tinkerpop.blueprints.Vertex; |
| 12 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
| 13 | import com.tinkerpop.frames.FramedGraph; |
| 14 | |
| 15 | public class GraphDBConnection implements IDBConnection { |
| 16 | public enum Transaction { |
| 17 | COMMIT, ROLLBACK |
| 18 | } |
| 19 | |
| 20 | public enum GenerateEvent { |
| 21 | TRUE, FALSE |
| 22 | } |
| 23 | |
| 24 | class TransactionHandle { |
| 25 | protected TransactionalGraph tr; |
| 26 | |
| 27 | public void create() { |
| 28 | tr = graph.newTransaction(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | protected static Logger log = LoggerFactory |
| 33 | .getLogger(GraphDBConnection.class); |
| 34 | private static GraphDBConnection singleton = new GraphDBConnection(); |
| 35 | private static TitanGraph graph; |
| 36 | private static EventTransactionalGraph<TitanGraph> eg; |
| 37 | private static String configFile; |
| 38 | |
| 39 | /* |
| 40 | * A private Constructor prevents any other class from instantiating. |
| 41 | */ |
| 42 | private GraphDBConnection() { |
| 43 | } |
| 44 | |
| 45 | /* Static 'instance' method */ |
| 46 | /** |
| 47 | * Get the instance of GraphDBConnection class. |
| 48 | * @param conf the path to the database configuration file. |
| 49 | * @return GraphDBConnection instance. |
| 50 | */ |
| 51 | public static synchronized GraphDBConnection getInstance(final String conf) { |
| 52 | if (GraphDBConnection.configFile == null |
| 53 | || GraphDBConnection.configFile.isEmpty()) { |
| 54 | GraphDBConnection.configFile = conf; |
| 55 | log.debug("GraphDBConnection::Setting Config File {}", |
| 56 | GraphDBConnection.configFile); |
| 57 | } |
| 58 | if (!GraphDBConnection.configFile.isEmpty() |
| 59 | && (graph == null || graph.isOpen() == Boolean.FALSE)) { |
| 60 | graph = TitanFactory.open(GraphDBConnection.configFile); |
| 61 | // FIXME: Creation on Indexes should be done only once |
| 62 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 63 | if (!s.contains("dpid")) { |
| 64 | graph.createKeyIndex("dpid", Vertex.class); |
| 65 | } |
Pankaj Berde | bbd3861 | 2013-06-22 05:59:12 -0700 | [diff] [blame] | 66 | if (!s.contains("port_id")) { |
| 67 | graph.createKeyIndex("port_id", Vertex.class); |
| 68 | } |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 69 | if (!s.contains("type")) { |
| 70 | graph.createKeyIndex("type", Vertex.class); |
| 71 | } |
Pavlin Radoslavov | 3aed468 | 2013-06-21 13:40:48 -0700 | [diff] [blame] | 72 | if (!s.contains("dl_addr")) { |
| 73 | graph.createKeyIndex("dl_addr", Vertex.class); |
Pankaj Berde | 85016ab | 2013-06-21 11:34:53 -0700 | [diff] [blame] | 74 | } |
| 75 | if (!s.contains("flow_id")) { |
| 76 | graph.createKeyIndex("flow_id", Vertex.class); |
| 77 | } |
| 78 | if (!s.contains("flow_entry_id")) { |
| 79 | graph.createKeyIndex("flow_entry_id", Vertex.class); |
| 80 | } |
| 81 | if (!s.contains("switch_state")) { |
| 82 | graph.createKeyIndex("switch_state", Vertex.class); |
| 83 | } |
| 84 | graph.commit(); |
| 85 | eg = new EventTransactionalGraph<TitanGraph>(graph); |
| 86 | } |
| 87 | return singleton; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get a FramedGraph instance of the graph. |
| 92 | */ |
| 93 | public FramedGraph<TitanGraph> getFramedGraph() { |
| 94 | if (isValid()) { |
| 95 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 96 | return fg; |
| 97 | } else { |
| 98 | log.error("new FramedGraph failed"); |
| 99 | return null; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get EventTransactionalGraph of the titan graph. |
| 105 | * @return EventTransactionalGraph of the titan graph |
| 106 | */ |
| 107 | protected EventTransactionalGraph<TitanGraph> getEventGraph() { |
| 108 | if (isValid()) { |
| 109 | return eg; |
| 110 | } else { |
| 111 | return null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add LocalGraphChangedLister for the graph. |
| 117 | */ |
| 118 | public void addEventListener(final LocalGraphChangedListener listener) { |
| 119 | EventTransactionalGraph<TitanGraph> eg = this.getEventGraph(); |
| 120 | eg.addListener(listener); |
| 121 | log.debug("Registered listener {}", listener.getClass()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return whether this connection is valid. |
| 126 | */ |
| 127 | public Boolean isValid() { |
| 128 | return (graph != null || graph.isOpen()); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Commit changes for the graph operations. |
| 133 | */ |
| 134 | public void commit() { |
| 135 | try { |
| 136 | graph.commit(); |
| 137 | } |
| 138 | catch (Exception e) { |
| 139 | log.error("{}", e.toString()); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Rollback changes for the graph operations. |
| 145 | */ |
| 146 | public void rollback() { |
| 147 | try { |
| 148 | graph.rollback(); |
| 149 | } |
| 150 | catch (Exception e) { |
| 151 | log.error("{}", e.toString()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Close this database connection. |
| 157 | */ |
| 158 | public void close() { |
| 159 | commit(); |
| 160 | } |
| 161 | } |