yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.graph; |
| 6 | |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 7 | import com.tinkerpop.blueprints.Vertex; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 8 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | import java.io.File; |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 11 | import java.util.Set; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 12 | import org.apache.commons.configuration.Configuration; |
| 13 | import org.apache.commons.configuration.ConfigurationException; |
| 14 | import org.apache.commons.configuration.PropertiesConfiguration; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * |
| 20 | * @author nickkaranatsios |
| 21 | */ |
| 22 | public class RamCloudDBConnection extends DBConnection { |
| 23 | private RamCloudGraph graph; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 24 | private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class); |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 25 | |
yoshi | 8d5a139 | 2013-12-06 17:19:19 -0800 | [diff] [blame] | 26 | //private static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 27 | |
| 28 | public RamCloudDBConnection(final String dbConfigFile) { |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 29 | //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
| 30 | //System.out.println("coordinatorURL "+ coordinatorURL); |
yoshi | d3025a9 | 2013-11-28 12:32:12 -0800 | [diff] [blame] | 31 | //graph = new RamCloudGraph(coordinatorURL); |
yoshi | 8d5a139 | 2013-12-06 17:19:19 -0800 | [diff] [blame] | 32 | //graph = RamCloudThreadLocal.get(); |
yoshi | 89eacab | 2013-12-09 17:29:08 -0800 | [diff] [blame] | 33 | //System.out.println("ThreadId = " + Thread.currentThread().getId() + " graph = " + graph); |
yoshi | 8d5a139 | 2013-12-06 17:19:19 -0800 | [diff] [blame] | 34 | graph = new RamCloudGraph("fast+udp:host=10.128.4.104,port=12246"); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 35 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 36 | 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 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 61 | 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 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 72 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 77 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 82 | try { |
| 83 | graph.commit(); |
| 84 | } catch (Exception e) { |
| 85 | log.error("{}", e.toString()); |
| 86 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 91 | try { |
| 92 | graph.rollback(); |
| 93 | } catch (Exception e) { |
| 94 | log.error("{}", e.toString()); |
| 95 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 100 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 101 | } |
| 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 | } |