Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 1 | package org.onosproject.store.consistent.impl; |
| 2 | |
| 3 | import com.typesafe.config.ConfigValueFactory; |
| 4 | import net.kuujo.copycat.cluster.ClusterConfig; |
| 5 | import net.kuujo.copycat.cluster.internal.coordinator.CoordinatedResourceConfig; |
| 6 | import net.kuujo.copycat.protocol.Consistency; |
| 7 | import net.kuujo.copycat.resource.ResourceConfig; |
| 8 | import net.kuujo.copycat.state.StateLogConfig; |
| 9 | import net.kuujo.copycat.util.internal.Assert; |
| 10 | |
| 11 | import java.util.Map; |
| 12 | |
| 13 | /** |
| 14 | * Database configuration. |
| 15 | * |
| 16 | */ |
| 17 | public class DatabaseConfig extends ResourceConfig<DatabaseConfig> { |
| 18 | private static final String DATABASE_CONSISTENCY = "consistency"; |
| 19 | |
| 20 | private static final String DEFAULT_CONFIGURATION = "database-defaults"; |
| 21 | private static final String CONFIGURATION = "database"; |
| 22 | |
| 23 | public DatabaseConfig() { |
| 24 | super(CONFIGURATION, DEFAULT_CONFIGURATION); |
| 25 | } |
| 26 | |
| 27 | public DatabaseConfig(Map<String, Object> config) { |
| 28 | super(config, CONFIGURATION, DEFAULT_CONFIGURATION); |
| 29 | } |
| 30 | |
| 31 | public DatabaseConfig(String resource) { |
| 32 | super(resource, CONFIGURATION, DEFAULT_CONFIGURATION); |
| 33 | } |
| 34 | |
| 35 | protected DatabaseConfig(DatabaseConfig config) { |
| 36 | super(config); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public DatabaseConfig copy() { |
| 41 | return new DatabaseConfig(this); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Sets the database read consistency. |
| 46 | * |
| 47 | * @param consistency The database read consistency. |
| 48 | * @throws java.lang.NullPointerException If the consistency is {@code null} |
| 49 | */ |
| 50 | public void setConsistency(String consistency) { |
| 51 | this.config = config.withValue(DATABASE_CONSISTENCY, |
| 52 | ConfigValueFactory.fromAnyRef( |
| 53 | Consistency.parse(Assert.isNotNull(consistency, "consistency")).toString())); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the database read consistency. |
| 58 | * |
| 59 | * @param consistency The database read consistency. |
| 60 | * @throws java.lang.NullPointerException If the consistency is {@code null} |
| 61 | */ |
| 62 | public void setConsistency(Consistency consistency) { |
| 63 | this.config = config.withValue(DATABASE_CONSISTENCY, |
| 64 | ConfigValueFactory.fromAnyRef( |
| 65 | Assert.isNotNull(consistency, "consistency").toString())); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the database read consistency. |
| 70 | * |
| 71 | * @return The database read consistency. |
| 72 | */ |
| 73 | public Consistency getConsistency() { |
| 74 | return Consistency.parse(config.getString(DATABASE_CONSISTENCY)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets the database read consistency, returning the configuration for method chaining. |
| 79 | * |
| 80 | * @param consistency The database read consistency. |
| 81 | * @return The database configuration. |
| 82 | * @throws java.lang.NullPointerException If the consistency is {@code null} |
| 83 | */ |
| 84 | public DatabaseConfig withConsistency(String consistency) { |
| 85 | setConsistency(consistency); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the database read consistency, returning the configuration for method chaining. |
| 91 | * |
| 92 | * @param consistency The database read consistency. |
| 93 | * @return The database configuration. |
| 94 | * @throws java.lang.NullPointerException If the consistency is {@code null} |
| 95 | */ |
| 96 | public DatabaseConfig withConsistency(Consistency consistency) { |
| 97 | setConsistency(consistency); |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public CoordinatedResourceConfig resolve(ClusterConfig cluster) { |
| 103 | return new StateLogConfig(toMap()) |
| 104 | .resolve(cluster) |
| 105 | .withResourceType(DefaultDatabase.class); |
| 106 | } |
| 107 | |
| 108 | } |