yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | package net.onrc.onos.graph; |
| 2 | |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 3 | import com.tinkerpop.blueprints.Vertex; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 4 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph; |
| 5 | import com.tinkerpop.frames.FramedGraph; |
| 6 | import java.io.File; |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 7 | import java.util.Set; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 8 | import org.apache.commons.configuration.Configuration; |
| 9 | import org.apache.commons.configuration.ConfigurationException; |
| 10 | import org.apache.commons.configuration.PropertiesConfiguration; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 13 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 14 | public class RamCloudDBConnection extends DBConnection { |
| 15 | private RamCloudGraph graph; |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 16 | private FramedGraph<RamCloudGraph> fg; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 17 | private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class); |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 18 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 19 | public RamCloudDBConnection(final String dbConfigFile) { |
yoshi | ffad7dd | 2014-01-21 14:04:07 -0800 | [diff] [blame] | 20 | final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
| 21 | graph = new RamCloudGraph(coordinatorURL); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 22 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 23 | 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); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 43 | } |
| 44 | if (!s.contains("ipv4_address")) { |
| 45 | graph.createKeyIndex("ipv4_address", Vertex.class); |
| 46 | } |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 47 | fg = new FramedGraph<RamCloudGraph>(graph); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 52 | if (isValid()) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 53 | return fg; |
| 54 | } else { |
| 55 | log.error("new FramedGraph failed"); |
| 56 | return null; |
| 57 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 62 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 67 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 72 | try { |
| 73 | graph.commit(); |
| 74 | } catch (Exception e) { |
| 75 | log.error("{}", e.toString()); |
| 76 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 81 | try { |
| 82 | graph.rollback(); |
| 83 | } catch (Exception e) { |
| 84 | log.error("{}", e.toString()); |
| 85 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 90 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 91 | } |
| 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 | |
yoshi | ffad7dd | 2014-01-21 14:04:07 -0800 | [diff] [blame] | 109 | 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 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 118 | } |