blob: 9f3b9a015f228943056c4c1ca21d843c85bcbee6 [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);
yoshid3025a92013-11-28 12:32:12 -080028 //graph = new RamCloudGraph(coordinatorURL);
29 graph = new RamCloudGraph("fast+udp:host=10.128.100.36,port=12246");
yoshi0451f282013-11-22 15:48:55 -080030 }
31
32 @Override
33 public FramedGraph getFramedGraph() {
yoshi9c429532013-11-27 01:12:17 -080034 System.out.println("RamCloud getFramedGraph");
yoshie665e822013-11-26 19:51:16 -080035 if (isValid()) {
36 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
37 return fg;
38 } else {
39 log.error("new FramedGraph failed");
40 return null;
41 }
yoshi0451f282013-11-22 15:48:55 -080042 }
43
44 @Override
45 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080046 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080047 }
48
49 @Override
50 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080051 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080052 }
53
54 @Override
55 public void commit() {
yoshie665e822013-11-26 19:51:16 -080056 try {
57 graph.commit();
58 } catch (Exception e) {
59 log.error("{}", e.toString());
60 }
yoshi0451f282013-11-22 15:48:55 -080061 }
62
63 @Override
64 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080065 try {
66 graph.rollback();
67 } catch (Exception e) {
68 log.error("{}", e.toString());
69 }
yoshi0451f282013-11-22 15:48:55 -080070 }
71
72 @Override
73 public void close() {
yoshie665e822013-11-26 19:51:16 -080074 commit();
yoshi0451f282013-11-22 15:48:55 -080075 }
76
77 private static final Configuration getConfiguration(final File dirOrFile) {
78 if (dirOrFile == null) {
79 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
80 }
81
82 if (!dirOrFile.isFile()) {
83 throw new IllegalArgumentException("Location of configuration must be a file");
84 }
85
86 try {
87 return new PropertiesConfiguration(dirOrFile);
88 } catch (ConfigurationException e) {
89 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
90 }
91 }
92
93 private String open(final Configuration configuration) {
94 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
95 if (coordinatorURL == null) {
96 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
97 }
98 return coordinatorURL;
99 }
100}