blob: 3fcbfe94383cad8a5befff4228b3fc0569c7dce3 [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 com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph;
8import com.tinkerpop.frames.FramedGraph;
9import java.io.File;
10import org.apache.commons.configuration.Configuration;
11import org.apache.commons.configuration.ConfigurationException;
12import org.apache.commons.configuration.PropertiesConfiguration;
yoshie665e822013-11-26 19:51:16 -080013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
yoshi0451f282013-11-22 15:48:55 -080015
16/**
17 *
18 * @author nickkaranatsios
19 */
20public class RamCloudDBConnection extends DBConnection {
21 private RamCloudGraph graph;
yoshie665e822013-11-26 19:51:16 -080022 private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class);
yoshi6184f952013-12-04 12:03:15 -080023
24 public static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>();
yoshi0451f282013-11-22 15:48:55 -080025
26 public RamCloudDBConnection(final String dbConfigFile) {
yoshi6184f952013-12-04 12:03:15 -080027 //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
28 //System.out.println("coordinatorURL "+ coordinatorURL);
yoshid3025a92013-11-28 12:32:12 -080029 //graph = new RamCloudGraph(coordinatorURL);
yoshi6184f952013-12-04 12:03:15 -080030 graph = RamCloudThreadLocal.get();
31 System.out.println("ThreadId = " + Thread.currentThread().getId() + "graph = " + graph);
32 if (graph == null) {
33 graph = new RamCloudGraph("fast+udp:host=10.128.100.36,port=12246");
34 RamCloudThreadLocal.set(graph);
35 }
yoshi0451f282013-11-22 15:48:55 -080036 }
37
38 @Override
39 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080040 if (isValid()) {
41 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
42 return fg;
43 } else {
44 log.error("new FramedGraph failed");
45 return null;
46 }
yoshi0451f282013-11-22 15:48:55 -080047 }
48
49 @Override
50 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080051 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080052 }
53
54 @Override
55 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080056 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080057 }
58
59 @Override
60 public void commit() {
yoshie665e822013-11-26 19:51:16 -080061 try {
62 graph.commit();
63 } catch (Exception e) {
64 log.error("{}", e.toString());
65 }
yoshi0451f282013-11-22 15:48:55 -080066 }
67
68 @Override
69 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080070 try {
71 graph.rollback();
72 } catch (Exception e) {
73 log.error("{}", e.toString());
74 }
yoshi0451f282013-11-22 15:48:55 -080075 }
76
77 @Override
78 public void close() {
yoshie665e822013-11-26 19:51:16 -080079 commit();
yoshi0451f282013-11-22 15:48:55 -080080 }
81
82 private static final Configuration getConfiguration(final File dirOrFile) {
83 if (dirOrFile == null) {
84 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
85 }
86
87 if (!dirOrFile.isFile()) {
88 throw new IllegalArgumentException("Location of configuration must be a file");
89 }
90
91 try {
92 return new PropertiesConfiguration(dirOrFile);
93 } catch (ConfigurationException e) {
94 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
95 }
96 }
97
98 private String open(final Configuration configuration) {
99 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
100 if (coordinatorURL == null) {
101 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
102 }
103 return coordinatorURL;
104 }
105}