blob: 3f8b235b2e3f920580bf606238d758f47e5f70a5 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Madan Jampani94c23532015-02-05 17:40:01 -080017package org.onosproject.store.consistent.impl;
18
19import com.typesafe.config.ConfigValueFactory;
20import net.kuujo.copycat.cluster.ClusterConfig;
21import net.kuujo.copycat.cluster.internal.coordinator.CoordinatedResourceConfig;
22import net.kuujo.copycat.protocol.Consistency;
23import net.kuujo.copycat.resource.ResourceConfig;
24import net.kuujo.copycat.state.StateLogConfig;
25import net.kuujo.copycat.util.internal.Assert;
26
27import java.util.Map;
28
29/**
30 * Database configuration.
31 *
32 */
33public class DatabaseConfig extends ResourceConfig<DatabaseConfig> {
34 private static final String DATABASE_CONSISTENCY = "consistency";
35
36 private static final String DEFAULT_CONFIGURATION = "database-defaults";
37 private static final String CONFIGURATION = "database";
38
39 public DatabaseConfig() {
40 super(CONFIGURATION, DEFAULT_CONFIGURATION);
41 }
42
43 public DatabaseConfig(Map<String, Object> config) {
44 super(config, CONFIGURATION, DEFAULT_CONFIGURATION);
45 }
46
47 public DatabaseConfig(String resource) {
48 super(resource, CONFIGURATION, DEFAULT_CONFIGURATION);
49 }
50
51 protected DatabaseConfig(DatabaseConfig config) {
52 super(config);
53 }
54
55 @Override
56 public DatabaseConfig copy() {
57 return new DatabaseConfig(this);
58 }
59
60 /**
61 * Sets the database read consistency.
62 *
63 * @param consistency The database read consistency.
64 * @throws java.lang.NullPointerException If the consistency is {@code null}
65 */
66 public void setConsistency(String consistency) {
67 this.config = config.withValue(DATABASE_CONSISTENCY,
68 ConfigValueFactory.fromAnyRef(
69 Consistency.parse(Assert.isNotNull(consistency, "consistency")).toString()));
70 }
71
72 /**
73 * Sets the database read consistency.
74 *
75 * @param consistency The database read consistency.
76 * @throws java.lang.NullPointerException If the consistency is {@code null}
77 */
78 public void setConsistency(Consistency consistency) {
79 this.config = config.withValue(DATABASE_CONSISTENCY,
80 ConfigValueFactory.fromAnyRef(
81 Assert.isNotNull(consistency, "consistency").toString()));
82 }
83
84 /**
85 * Returns the database read consistency.
86 *
87 * @return The database read consistency.
88 */
89 public Consistency getConsistency() {
90 return Consistency.parse(config.getString(DATABASE_CONSISTENCY));
91 }
92
93 /**
94 * Sets the database read consistency, returning the configuration for method chaining.
95 *
96 * @param consistency The database read consistency.
97 * @return The database configuration.
98 * @throws java.lang.NullPointerException If the consistency is {@code null}
99 */
100 public DatabaseConfig withConsistency(String consistency) {
101 setConsistency(consistency);
102 return this;
103 }
104
105 /**
106 * Sets the database read consistency, returning the configuration for method chaining.
107 *
108 * @param consistency The database read consistency.
109 * @return The database configuration.
110 * @throws java.lang.NullPointerException If the consistency is {@code null}
111 */
112 public DatabaseConfig withConsistency(Consistency consistency) {
113 setConsistency(consistency);
114 return this;
115 }
116
117 @Override
118 public CoordinatedResourceConfig resolve(ClusterConfig cluster) {
119 return new StateLogConfig(toMap())
120 .resolve(cluster)
121 .withResourceType(DefaultDatabase.class);
122 }
123
124}