blob: 547da4410bd8dd0a4ebfed1a2a19ef8805919ed8 [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
yoshi0451f282013-11-22 15:48:55 -080027 public RamCloudDBConnection(final String dbConfigFile) {
yoshiffad7dd2014-01-21 14:04:07 -080028 final String coordinatorURL = open(getConfiguration(new File(dbConfigFile)));
29 graph = new RamCloudGraph(coordinatorURL);
yoshi73e55342013-12-09 10:01:32 -080030 Set<String> s = graph.getIndexedKeys(Vertex.class);
yoshi73e55342013-12-09 10:01:32 -080031 if (!s.contains("dpid")) {
32 graph.createKeyIndex("dpid", Vertex.class);
33 }
34 if (!s.contains("port_id")) {
35 graph.createKeyIndex("port_id", Vertex.class);
36 }
37 if (!s.contains("type")) {
38 graph.createKeyIndex("type", Vertex.class);
39 }
40 if (!s.contains("dl_addr")) {
41 graph.createKeyIndex("dl_addr", Vertex.class);
42 }
43 if (!s.contains("flow_id")) {
44 graph.createKeyIndex("flow_id", Vertex.class);
45 }
46 if (!s.contains("flow_entry_id")) {
47 graph.createKeyIndex("flow_entry_id", Vertex.class);
48 }
49 if (!s.contains("switch_state")) {
50 graph.createKeyIndex("switch_state", Vertex.class);
yoshid76fa1f2013-12-19 14:20:34 -080051 }
52 if (!s.contains("ipv4_address")) {
53 graph.createKeyIndex("ipv4_address", Vertex.class);
54 }
Masayoshi Kobayashibd57bd32013-12-20 04:21:46 +000055 fg = new FramedGraph<RamCloudGraph>(graph);
yoshi0451f282013-11-22 15:48:55 -080056 }
57
58 @Override
59 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080060 if (isValid()) {
yoshie665e822013-11-26 19:51:16 -080061 return fg;
62 } else {
63 log.error("new FramedGraph failed");
64 return null;
65 }
yoshi0451f282013-11-22 15:48:55 -080066 }
67
68 @Override
69 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080070 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080071 }
72
73 @Override
74 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080075 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080076 }
77
78 @Override
79 public void commit() {
yoshie665e822013-11-26 19:51:16 -080080 try {
81 graph.commit();
82 } catch (Exception e) {
83 log.error("{}", e.toString());
84 }
yoshi0451f282013-11-22 15:48:55 -080085 }
86
87 @Override
88 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080089 try {
90 graph.rollback();
91 } catch (Exception e) {
92 log.error("{}", e.toString());
93 }
yoshi0451f282013-11-22 15:48:55 -080094 }
95
96 @Override
97 public void close() {
yoshie665e822013-11-26 19:51:16 -080098 commit();
yoshi0451f282013-11-22 15:48:55 -080099 }
100
101 private static final Configuration getConfiguration(final File dirOrFile) {
102 if (dirOrFile == null) {
103 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
104 }
105
106 if (!dirOrFile.isFile()) {
107 throw new IllegalArgumentException("Location of configuration must be a file");
108 }
109
110 try {
111 return new PropertiesConfiguration(dirOrFile);
112 } catch (ConfigurationException e) {
113 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
114 }
115 }
116
yoshiffad7dd2014-01-21 14:04:07 -0800117 private String open(final Configuration configuration) {
118 final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp", null);
119 final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort", null);
120 final String coordinatorURL = coordinatorIp + "," + coordinatorPort;
121 if (coordinatorURL == null) {
122 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
123 }
124 return coordinatorURL;
125 }
yoshi0451f282013-11-22 15:48:55 -0800126}