blob: ac3b8b0b51f5c292c753b4e7d0bd96c5814da2e6 [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);
yoshi0451f282013-11-22 15:48:55 -080023
24 public RamCloudDBConnection(final String dbConfigFile) {
25 System.out.println("dbconfigfile is + "+ dbConfigFile);
26 final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
yoshic9358282013-11-26 16:45:06 -080027 System.out.println("coordinatorURL "+ coordinatorURL);
yoshi0451f282013-11-22 15:48:55 -080028 graph = new RamCloudGraph(coordinatorURL);
29 }
30
31 @Override
32 public FramedGraph getFramedGraph() {
yoshi9c429532013-11-27 01:12:17 -080033 System.out.println("RamCloud getFramedGraph");
yoshie665e822013-11-26 19:51:16 -080034 if (isValid()) {
35 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
36 return fg;
37 } else {
38 log.error("new FramedGraph failed");
39 return null;
40 }
yoshi0451f282013-11-22 15:48:55 -080041 }
42
43 @Override
44 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080045 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080046 }
47
48 @Override
49 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080050 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080051 }
52
53 @Override
54 public void commit() {
yoshie665e822013-11-26 19:51:16 -080055 try {
56 graph.commit();
57 } catch (Exception e) {
58 log.error("{}", e.toString());
59 }
yoshi0451f282013-11-22 15:48:55 -080060 }
61
62 @Override
63 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080064 try {
65 graph.rollback();
66 } catch (Exception e) {
67 log.error("{}", e.toString());
68 }
yoshi0451f282013-11-22 15:48:55 -080069 }
70
71 @Override
72 public void close() {
yoshie665e822013-11-26 19:51:16 -080073 commit();
yoshi0451f282013-11-22 15:48:55 -080074 }
75
76 private static final Configuration getConfiguration(final File dirOrFile) {
77 if (dirOrFile == null) {
78 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
79 }
80
81 if (!dirOrFile.isFile()) {
82 throw new IllegalArgumentException("Location of configuration must be a file");
83 }
84
85 try {
86 return new PropertiesConfiguration(dirOrFile);
87 } catch (ConfigurationException e) {
88 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
89 }
90 }
91
92 private String open(final Configuration configuration) {
93 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
94 if (coordinatorURL == null) {
95 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
96 }
97 return coordinatorURL;
98 }
99}