blob: 7841d3ec667a28578b927fdb4a9cf56773b445a9 [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() {
yoshie665e822013-11-26 19:51:16 -080033 if (isValid()) {
34 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
35 return fg;
36 } else {
37 log.error("new FramedGraph failed");
38 return null;
39 }
yoshi0451f282013-11-22 15:48:55 -080040 }
41
42 @Override
43 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080044 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080045 }
46
47 @Override
48 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080049 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080050 }
51
52 @Override
53 public void commit() {
yoshie665e822013-11-26 19:51:16 -080054 try {
55 graph.commit();
56 } catch (Exception e) {
57 log.error("{}", e.toString());
58 }
yoshi0451f282013-11-22 15:48:55 -080059 }
60
61 @Override
62 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080063 try {
64 graph.rollback();
65 } catch (Exception e) {
66 log.error("{}", e.toString());
67 }
yoshi0451f282013-11-22 15:48:55 -080068 }
69
70 @Override
71 public void close() {
yoshie665e822013-11-26 19:51:16 -080072 commit();
yoshi0451f282013-11-22 15:48:55 -080073 }
74
75 private static final Configuration getConfiguration(final File dirOrFile) {
76 if (dirOrFile == null) {
77 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
78 }
79
80 if (!dirOrFile.isFile()) {
81 throw new IllegalArgumentException("Location of configuration must be a file");
82 }
83
84 try {
85 return new PropertiesConfiguration(dirOrFile);
86 } catch (ConfigurationException e) {
87 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
88 }
89 }
90
91 private String open(final Configuration configuration) {
92 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
93 if (coordinatorURL == null) {
94 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
95 }
96 return coordinatorURL;
97 }
98}