blob: 0709cc4bba8143c29ef52e0fb0bafa3baf9a2df5 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.graph;
6
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12/**
13 *
14 * @author nickkaranatsios
15 */
16public class GraphDBManager {
17 private static ThreadLocal<HashMap<String, DBConnection>> connections = new ThreadLocal<HashMap<String, DBConnection>>();
yoshi2db7ff42013-11-25 19:30:25 -080018 private static DBOperation operation = null;
19
yoshi0451f282013-11-22 15:48:55 -080020 static Map<String, DBConnection> getConnectionMap() {
21 if (connections.get() == null) {
22 connections.set(new HashMap<String, DBConnection>());
23 }
24 return connections.get();
25 }
26
27 public static DBOperation getDBOperation(final String dbStore, final String dbConfigFile) {
yoshif7424e42013-11-25 22:07:40 -080028 if (operation == null) {
yoshi2db7ff42013-11-25 19:30:25 -080029 if (dbStore.equals("ramcloud")) {
30 operation = new RamCloudDBOperation();
31 } else if (dbStore.equals("titan")) {
32 operation = new TitanDBOperation();
33 }
34 if (operation != null) {
35 operation.conn = GraphDBManager.getConnection(dbStore, dbConfigFile);
36 }
37 }
yoshi95ff5c22013-11-25 17:00:18 -080038 return operation;
yoshi0451f282013-11-22 15:48:55 -080039 }
40
41 public static DBConnection getConnection(final String dbStore, final String dbConfigFile) {
42 DBConnection conn = getConnectionMap().get(dbStore);
43 if (conn == null) {
44 if (dbStore.equals("ramcloud")) {
45 conn = new RamCloudDBConnection(dbConfigFile);
46 } else if (dbStore.equals("titan")) {
47 conn = new TitanDBConnection(dbConfigFile);
48 }
49
50 GraphDBManager.getConnectionMap().put(dbStore, conn);
51 } else {
52 GraphDBManager.getConnectionMap().get(dbStore);
53 }
54 return conn;
55 }
56
57 static List<DBConnection> getConnections() {
58 return new ArrayList<DBConnection>(getConnectionMap().values());
59 }
60}