blob: 354070a07ca615f03d18ef8585de12fb405fb4e3 [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);
yoshid76fa1f2013-12-19 14:20:34 -080056 }
57 if (!s.contains("ipv4_address")) {
58 graph.createKeyIndex("ipv4_address", Vertex.class);
59 }
yoshi0451f282013-11-22 15:48:55 -080060 }
61
62 @Override
63 public FramedGraph getFramedGraph() {
yoshie665e822013-11-26 19:51:16 -080064 if (isValid()) {
65 FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph);
66 return fg;
67 } else {
68 log.error("new FramedGraph failed");
69 return null;
70 }
yoshi0451f282013-11-22 15:48:55 -080071 }
72
73 @Override
74 public void addEventListener(LocalGraphChangedListener listener) {
yoshie665e822013-11-26 19:51:16 -080075 //TO-DO
yoshi0451f282013-11-22 15:48:55 -080076 }
77
78 @Override
79 public Boolean isValid() {
yoshie665e822013-11-26 19:51:16 -080080 return (graph != null);
yoshi0451f282013-11-22 15:48:55 -080081 }
82
83 @Override
84 public void commit() {
yoshie665e822013-11-26 19:51:16 -080085 try {
86 graph.commit();
87 } catch (Exception e) {
88 log.error("{}", e.toString());
89 }
yoshi0451f282013-11-22 15:48:55 -080090 }
91
92 @Override
93 public void rollback() {
yoshie665e822013-11-26 19:51:16 -080094 try {
95 graph.rollback();
96 } catch (Exception e) {
97 log.error("{}", e.toString());
98 }
yoshi0451f282013-11-22 15:48:55 -080099 }
100
101 @Override
102 public void close() {
yoshie665e822013-11-26 19:51:16 -0800103 commit();
yoshi0451f282013-11-22 15:48:55 -0800104 }
105
106 private static final Configuration getConfiguration(final File dirOrFile) {
107 if (dirOrFile == null) {
108 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
109 }
110
111 if (!dirOrFile.isFile()) {
112 throw new IllegalArgumentException("Location of configuration must be a file");
113 }
114
115 try {
116 return new PropertiesConfiguration(dirOrFile);
117 } catch (ConfigurationException e) {
118 throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
119 }
120 }
121
122 private String open(final Configuration configuration) {
123 final String coordinatorURL = configuration.getString("ramcloud.coordinator", null);
124 if (coordinatorURL == null) {
125 throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting");
126 }
127 return coordinatorURL;
128 }
129}