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 | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 27 | public RamCloudDBConnection(final String dbConfigFile) { |
yoshi | ffad7dd | 2014-01-21 14:04:07 -0800 | [diff] [blame] | 28 | final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
| 29 | graph = new RamCloudGraph(coordinatorURL); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 30 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
yoshi | 73e5534 | 2013-12-09 10:01:32 -0800 | [diff] [blame] | 31 | 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); |
yoshi | d76fa1f | 2013-12-19 14:20:34 -0800 | [diff] [blame] | 51 | } |
| 52 | if (!s.contains("ipv4_address")) { |
| 53 | graph.createKeyIndex("ipv4_address", Vertex.class); |
| 54 | } |
Masayoshi Kobayashi | bd57bd3 | 2013-12-20 04:21:46 +0000 | [diff] [blame] | 55 | fg = new FramedGraph<RamCloudGraph>(graph); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 60 | if (isValid()) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 61 | return fg; |
| 62 | } else { |
| 63 | log.error("new FramedGraph failed"); |
| 64 | return null; |
| 65 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 70 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 75 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 80 | try { |
| 81 | graph.commit(); |
| 82 | } catch (Exception e) { |
| 83 | log.error("{}", e.toString()); |
| 84 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 89 | try { |
| 90 | graph.rollback(); |
| 91 | } catch (Exception e) { |
| 92 | log.error("{}", e.toString()); |
| 93 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 98 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 99 | } |
| 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 | |
yoshi | ffad7dd | 2014-01-21 14:04:07 -0800 | [diff] [blame] | 117 | 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 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 126 | } |