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; |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 24 | private FramedGraph<RamCloudGraph> fg; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 25 | private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class); |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 26 | |
yoshi | 8d5a139 | 2013-12-06 17:19:19 -0800 | [diff] [blame] | 27 | //private static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 28 | |
| 29 | public RamCloudDBConnection(final String dbConfigFile) { |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 30 | //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
| 31 | //System.out.println("coordinatorURL "+ coordinatorURL); |
yoshi | d3025a9 | 2013-11-28 12:32:12 -0800 | [diff] [blame] | 32 | //graph = new RamCloudGraph(coordinatorURL); |
yoshi | 8d5a139 | 2013-12-06 17:19:19 -0800 | [diff] [blame] | 33 | //graph = RamCloudThreadLocal.get(); |
yoshi | 89eacab | 2013-12-09 17:29:08 -0800 | [diff] [blame] | 34 | //System.out.println("ThreadId = " + Thread.currentThread().getId() + " graph = " + graph); |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 35 | graph = new RamCloudGraph("fast+udp:host=10.0.0.144,port=12246"); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 36 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 37 | 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); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 57 | } |
| 58 | if (!s.contains("ipv4_address")) { |
| 59 | graph.createKeyIndex("ipv4_address", Vertex.class); |
| 60 | } |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 61 | fg = new FramedGraph<RamCloudGraph>(graph); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 66 | if (isValid()) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 67 | return fg; |
| 68 | } else { |
| 69 | log.error("new FramedGraph failed"); |
| 70 | return null; |
| 71 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 76 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 81 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 86 | try { |
| 87 | graph.commit(); |
| 88 | } catch (Exception e) { |
| 89 | log.error("{}", e.toString()); |
| 90 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 95 | try { |
| 96 | graph.rollback(); |
| 97 | } catch (Exception e) { |
| 98 | log.error("{}", e.toString()); |
| 99 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 104 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 105 | } |
| 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 | } |