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 | |
| 7 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph; |
| 8 | import com.tinkerpop.frames.FramedGraph; |
| 9 | import java.io.File; |
| 10 | import org.apache.commons.configuration.Configuration; |
| 11 | import org.apache.commons.configuration.ConfigurationException; |
| 12 | import org.apache.commons.configuration.PropertiesConfiguration; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * |
| 18 | * @author nickkaranatsios |
| 19 | */ |
| 20 | public class RamCloudDBConnection extends DBConnection { |
| 21 | private RamCloudGraph graph; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 22 | private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 23 | |
| 24 | public RamCloudDBConnection(final String dbConfigFile) { |
| 25 | System.out.println("dbconfigfile is + "+ dbConfigFile); |
| 26 | final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
yoshi | c935828 | 2013-11-26 16:45:06 -0800 | [diff] [blame] | 27 | System.out.println("coordinatorURL "+ coordinatorURL); |
yoshi | d3025a9 | 2013-11-28 12:32:12 -0800 | [diff] [blame^] | 28 | //graph = new RamCloudGraph(coordinatorURL); |
| 29 | graph = new RamCloudGraph("fast+udp:host=10.128.100.36,port=12246"); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public FramedGraph getFramedGraph() { |
yoshi | 9c42953 | 2013-11-27 01:12:17 -0800 | [diff] [blame] | 34 | System.out.println("RamCloud getFramedGraph"); |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 35 | if (isValid()) { |
| 36 | FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph); |
| 37 | return fg; |
| 38 | } else { |
| 39 | log.error("new FramedGraph failed"); |
| 40 | return null; |
| 41 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 46 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 51 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 56 | try { |
| 57 | graph.commit(); |
| 58 | } catch (Exception e) { |
| 59 | log.error("{}", e.toString()); |
| 60 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 65 | try { |
| 66 | graph.rollback(); |
| 67 | } catch (Exception e) { |
| 68 | log.error("{}", e.toString()); |
| 69 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 74 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private static final Configuration getConfiguration(final File dirOrFile) { |
| 78 | if (dirOrFile == null) { |
| 79 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 80 | } |
| 81 | |
| 82 | if (!dirOrFile.isFile()) { |
| 83 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | return new PropertiesConfiguration(dirOrFile); |
| 88 | } catch (ConfigurationException e) { |
| 89 | throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private String open(final Configuration configuration) { |
| 94 | final String coordinatorURL = configuration.getString("ramcloud.coordinator", null); |
| 95 | if (coordinatorURL == null) { |
| 96 | throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting"); |
| 97 | } |
| 98 | return coordinatorURL; |
| 99 | } |
| 100 | } |