blob: 4728e61c13b312c87d4348c6327c393185240fc9 [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
yoshi73e55342013-12-09 10:01:32 -08007import com.tinkerpop.blueprints.Vertex;
yoshi0451f282013-11-22 15:48:55 -08008import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph;
9import com.tinkerpop.frames.FramedGraph;
10import java.io.File;
yoshi73e55342013-12-09 10:01:32 -080011import java.util.Set;
yoshi0451f282013-11-22 15:48:55 -080012import org.apache.commons.configuration.Configuration;
13import org.apache.commons.configuration.ConfigurationException;
14import org.apache.commons.configuration.PropertiesConfiguration;
yoshie665e822013-11-26 19:51:16 -080015import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
yoshi0451f282013-11-22 15:48:55 -080017
18/**
19 *
20 * @author nickkaranatsios
21 */
22public class RamCloudDBConnection extends DBConnection {
23 private RamCloudGraph graph;
yoshie665e822013-11-26 19:51:16 -080024 private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class);
yoshi6184f952013-12-04 12:03:15 -080025
yoshi8d5a1392013-12-06 17:19:19 -080026 //private static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>();
yoshi0451f282013-11-22 15:48:55 -080027
28 public RamCloudDBConnection(final String dbConfigFile) {
yoshi6184f952013-12-04 12:03:15 -080029 //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
30 //System.out.println("coordinatorURL "+ coordinatorURL);
yoshid3025a92013-11-28 12:32:12 -080031 //graph = new RamCloudGraph(coordinatorURL);
yoshi8d5a1392013-12-06 17:19:19 -080032 //graph = RamCloudThreadLocal.get();
yoshi89eacab2013-12-09 17:29:08 -080033 //System.out.println("ThreadId = " + Thread.currentThread().getId() + " graph = " + graph);
yoshi8d5a1392013-12-06 17:19:19 -080034 graph = new RamCloudGraph("fast+udp:host=10.128.4.104,port=12246");
yoshi73e55342013-12-09 10:01:32 -080035 Set<String> s = graph.getIndexedKeys(Vertex.class);
yoshi73e55342013-12-09 10:01:32 -080036 if (!s.contains("dpid")) {
37 graph.createKeyIndex("dpid", Vertex.class);
38 }
39 if (!s.contains("port_id")) {
40 graph.createKeyIndex("port_id", Vertex.class);
41 }
42 if (!s.contains("type")) {
43 graph.createKeyIndex("type", Vertex.class);
44 }
45 if (!s.contains("dl_addr")) {
46 graph.createKeyIndex("dl_addr", Vertex.class);
47 }
48 if (!s.contains("flow_id")) {
49 graph.createKeyIndex("flow_id", Vertex.class);
50 }
51 if (!s.contains("flow_entry_id")) {
52 graph.createKeyIndex("flow_entry_id", Vertex.class);
53 }
54 if (!s.contains("switch_state")) {
55 graph.createKeyIndex("switch_state", Vertex.class);
56 }
yoshi0451f282013-11-22 15:48:55 -080057 }
58
59 @Override
60 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080061 if (isValid()) {
62 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
63 return fg;
64 } else {
65 log.error("new FramedGraph failed");
66 return null;
67 }
yoshi0451f282013-11-22 15:48:55 -080068 }
69
70 @Override
71 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080072 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080073 }
74
75 @Override
76 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080077 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080078 }
79
80 @Override
81 public void commit() {
yoshie665e822013-11-26 19:51:16 -080082 try {
83 graph.commit();
84 } catch (Exception e) {
85 log.error("{}", e.toString());
86 }
yoshi0451f282013-11-22 15:48:55 -080087 }
88
89 @Override
90 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080091 try {
92 graph.rollback();
93 } catch (Exception e) {
94 log.error("{}", e.toString());
95 }
yoshi0451f282013-11-22 15:48:55 -080096 }
97
98 @Override
99 public void close() {
yoshie665e822013-11-26 19:51:16 -0800100 commit();
yoshi0451f282013-11-22 15:48:55 -0800101 }
102
103 private static final Configuration getConfiguration(final File dirOrFile) {
104 if (dirOrFile == null) {
105 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
106 }
107
108 if (!dirOrFile.isFile()) {
109 throw new IllegalArgumentException("Location of configuration must be a file");
110 }
111
112 try {
113 return new PropertiesConfiguration(dirOrFile);
114 } catch (ConfigurationException e) {
115 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
116 }
117 }
118
119 private String open(final Configuration configuration) {
120 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
121 if (coordinatorURL == null) {
122 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
123 }
124 return coordinatorURL;
125 }
126}