blob: 17067b466bac7b135f9ea8e3d1e9ab725ec26b05 [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;
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000024 private FramedGraph<RamCloudGraph> fg;
yoshie665e822013-11-26 19:51:16 -080025 private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class);
yoshi6184f952013-12-04 12:03:15 -080026
yoshi8d5a1392013-12-06 17:19:19 -080027 //private static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>();
yoshi0451f282013-11-22 15:48:55 -080028
29 public RamCloudDBConnection(final String dbConfigFile) {
yoshi6184f952013-12-04 12:03:15 -080030 //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
31 //System.out.println("coordinatorURL "+ coordinatorURL);
yoshid3025a92013-11-28 12:32:12 -080032 //graph = new RamCloudGraph(coordinatorURL);
yoshi8d5a1392013-12-06 17:19:19 -080033 //graph = RamCloudThreadLocal.get();
yoshi89eacab2013-12-09 17:29:08 -080034 //System.out.println("ThreadId = " + Thread.currentThread().getId() + " graph = " + graph);
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000035 graph = new RamCloudGraph("fast+udp:host=10.0.0.144,port=12246");
yoshi73e55342013-12-09 10:01:32 -080036 Set<String> s = graph.getIndexedKeys(Vertex.class);
yoshi73e55342013-12-09 10:01:32 -080037 if (!s.contains("dpid")) {
38 graph.createKeyIndex("dpid", Vertex.class);
39 }
40 if (!s.contains("port_id")) {
41 graph.createKeyIndex("port_id", Vertex.class);
42 }
43 if (!s.contains("type")) {
44 graph.createKeyIndex("type", Vertex.class);
45 }
46 if (!s.contains("dl_addr")) {
47 graph.createKeyIndex("dl_addr", Vertex.class);
48 }
49 if (!s.contains("flow_id")) {
50 graph.createKeyIndex("flow_id", Vertex.class);
51 }
52 if (!s.contains("flow_entry_id")) {
53 graph.createKeyIndex("flow_entry_id", Vertex.class);
54 }
55 if (!s.contains("switch_state")) {
56 graph.createKeyIndex("switch_state", Vertex.class);
yoshid76fa1f2013-12-19 14:20:34 -080057 }
58 if (!s.contains("ipv4_address")) {
59 graph.createKeyIndex("ipv4_address", Vertex.class);
60 }
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000061 fg = new FramedGraph<RamCloudGraph>(graph);
yoshi0451f282013-11-22 15:48:55 -080062 }
63
64 @Override
65 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080066 if (isValid()) {
yoshie665e822013-11-26 19:51:16 -080067 return fg;
68 } else {
69 log.error("new FramedGraph failed");
70 return null;
71 }
yoshi0451f282013-11-22 15:48:55 -080072 }
73
74 @Override
75 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080076 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080077 }
78
79 @Override
80 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080081 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080082 }
83
84 @Override
85 public void commit() {
yoshie665e822013-11-26 19:51:16 -080086 try {
87 graph.commit();
88 } catch (Exception e) {
89 log.error("{}", e.toString());
90 }
yoshi0451f282013-11-22 15:48:55 -080091 }
92
93 @Override
94 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080095 try {
96 graph.rollback();
97 } catch (Exception e) {
98 log.error("{}", e.toString());
99 }
yoshi0451f282013-11-22 15:48:55 -0800100 }
101
102 @Override
103 public void close() {
yoshie665e822013-11-26 19:51:16 -0800104 commit();
yoshi0451f282013-11-22 15:48:55 -0800105 }
106
107 private static final Configuration getConfiguration(final File dirOrFile) {
108 if (dirOrFile == null) {
109 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
110 }
111
112 if (!dirOrFile.isFile()) {
113 throw new IllegalArgumentException("Location of configuration must be a file");
114 }
115
116 try {
117 return new PropertiesConfiguration(dirOrFile);
118 } catch (ConfigurationException e) {
119 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
120 }
121 }
122
123 private String open(final Configuration configuration) {
124 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
125 if (coordinatorURL == null) {
126 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
127 }
128 return coordinatorURL;
129 }
130}