blob: f23db921710fd45cbe2022b5f8985c71cb377d93 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001package net.onrc.onos.graph;
2
yoshi73e55342013-12-09 10:01:32 -08003import com.tinkerpop.blueprints.Vertex;
yoshi0451f282013-11-22 15:48:55 -08004import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph;
5import com.tinkerpop.frames.FramedGraph;
6import java.io.File;
yoshi73e55342013-12-09 10:01:32 -08007import java.util.Set;
yoshi0451f282013-11-22 15:48:55 -08008import org.apache.commons.configuration.Configuration;
9import org.apache.commons.configuration.ConfigurationException;
10import org.apache.commons.configuration.PropertiesConfiguration;
yoshie665e822013-11-26 19:51:16 -080011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
yoshi0451f282013-11-22 15:48:55 -080013
yoshi0451f282013-11-22 15:48:55 -080014public class RamCloudDBConnection extends DBConnection {
15 private RamCloudGraph graph;
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000016 private FramedGraph<RamCloudGraph> fg;
yoshie665e822013-11-26 19:51:16 -080017 private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class);
yoshi6184f952013-12-04 12:03:15 -080018
yoshi0451f282013-11-22 15:48:55 -080019 public RamCloudDBConnection(final String dbConfigFile) {
yoshiffad7dd2014-01-21 14:04:07 -080020 final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
21 graph = new RamCloudGraph(coordinatorURL);
yoshi73e55342013-12-09 10:01:32 -080022 Set<String> s = graph.getIndexedKeys(Vertex.class);
yoshi73e55342013-12-09 10:01:32 -080023 if (!s.contains("dpid")) {
24 graph.createKeyIndex("dpid", Vertex.class);
25 }
26 if (!s.contains("port_id")) {
27 graph.createKeyIndex("port_id", Vertex.class);
28 }
29 if (!s.contains("type")) {
30 graph.createKeyIndex("type", Vertex.class);
31 }
32 if (!s.contains("dl_addr")) {
33 graph.createKeyIndex("dl_addr", Vertex.class);
34 }
35 if (!s.contains("flow_id")) {
36 graph.createKeyIndex("flow_id", Vertex.class);
37 }
38 if (!s.contains("flow_entry_id")) {
39 graph.createKeyIndex("flow_entry_id", Vertex.class);
40 }
41 if (!s.contains("switch_state")) {
42 graph.createKeyIndex("switch_state", Vertex.class);
yoshid76fa1f2013-12-19 14:20:34 -080043 }
44 if (!s.contains("ipv4_address")) {
45 graph.createKeyIndex("ipv4_address", Vertex.class);
46 }
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000047 fg = new FramedGraph<RamCloudGraph>(graph);
yoshi0451f282013-11-22 15:48:55 -080048 }
49
50 @Override
51 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080052 if (isValid()) {
yoshie665e822013-11-26 19:51:16 -080053 return fg;
54 } else {
55 log.error("new FramedGraph failed");
56 return null;
57 }
yoshi0451f282013-11-22 15:48:55 -080058 }
59
60 @Override
61 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080062 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080063 }
64
65 @Override
66 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080067 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080068 }
69
70 @Override
71 public void commit() {
yoshie665e822013-11-26 19:51:16 -080072 try {
73 graph.commit();
74 } catch (Exception e) {
75 log.error("{}", e.toString());
76 }
yoshi0451f282013-11-22 15:48:55 -080077 }
78
79 @Override
80 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080081 try {
82 graph.rollback();
83 } catch (Exception e) {
84 log.error("{}", e.toString());
85 }
yoshi0451f282013-11-22 15:48:55 -080086 }
87
88 @Override
89 public void close() {
yoshie665e822013-11-26 19:51:16 -080090 commit();
yoshi0451f282013-11-22 15:48:55 -080091 }
92
93 private static final Configuration getConfiguration(final File dirOrFile) {
94 if (dirOrFile == null) {
95 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
96 }
97
98 if (!dirOrFile.isFile()) {
99 throw new IllegalArgumentException("Location of configuration must be a file");
100 }
101
102 try {
103 return new PropertiesConfiguration(dirOrFile);
104 } catch (ConfigurationException e) {
105 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
106 }
107 }
108
yoshiffad7dd2014-01-21 14:04:07 -0800109 private String open(final Configuration configuration) {
110 final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp", null);
111 final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort", null);
112 final String coordinatorURL = coordinatorIp + "," + coordinatorPort;
113 if (coordinatorURL == null) {
114 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
115 }
116 return coordinatorURL;
117 }
yoshi0451f282013-11-22 15:48:55 -0800118}