blob: 57dd31c3024f4ca24ad13781e5947457a079b09a [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 Jampanif4c88502016-01-21 12:35:36 -080017package org.onosproject.store.primitives.impl;
Madan Jampani94c23532015-02-05 17:40:01 -080018
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
Madan Jampanif1b8e172015-03-23 11:42:02 -070039 private String name;
40
Madan Jampani94c23532015-02-05 17:40:01 -080041 public DatabaseConfig() {
42 super(CONFIGURATION, DEFAULT_CONFIGURATION);
43 }
44
45 public DatabaseConfig(Map<String, Object> config) {
46 super(config, CONFIGURATION, DEFAULT_CONFIGURATION);
47 }
48
49 public DatabaseConfig(String resource) {
50 super(resource, CONFIGURATION, DEFAULT_CONFIGURATION);
51 }
52
53 protected DatabaseConfig(DatabaseConfig config) {
54 super(config);
55 }
56
57 @Override
58 public DatabaseConfig copy() {
59 return new DatabaseConfig(this);
60 }
61
62 /**
63 * Sets the database read consistency.
64 *
65 * @param consistency The database read consistency.
66 * @throws java.lang.NullPointerException If the consistency is {@code null}
67 */
68 public void setConsistency(String consistency) {
69 this.config = config.withValue(DATABASE_CONSISTENCY,
70 ConfigValueFactory.fromAnyRef(
71 Consistency.parse(Assert.isNotNull(consistency, "consistency")).toString()));
72 }
73
74 /**
75 * Sets the database read consistency.
76 *
77 * @param consistency The database read consistency.
78 * @throws java.lang.NullPointerException If the consistency is {@code null}
79 */
80 public void setConsistency(Consistency consistency) {
81 this.config = config.withValue(DATABASE_CONSISTENCY,
82 ConfigValueFactory.fromAnyRef(
83 Assert.isNotNull(consistency, "consistency").toString()));
84 }
85
86 /**
87 * Returns the database read consistency.
88 *
89 * @return The database read consistency.
90 */
91 public Consistency getConsistency() {
92 return Consistency.parse(config.getString(DATABASE_CONSISTENCY));
93 }
94
95 /**
96 * Sets the database read consistency, returning the configuration for method chaining.
97 *
98 * @param consistency The database read consistency.
99 * @return The database configuration.
100 * @throws java.lang.NullPointerException If the consistency is {@code null}
101 */
102 public DatabaseConfig withConsistency(String consistency) {
103 setConsistency(consistency);
104 return this;
105 }
106
107 /**
108 * Sets the database read consistency, returning the configuration for method chaining.
109 *
110 * @param consistency The database read consistency.
111 * @return The database configuration.
112 * @throws java.lang.NullPointerException If the consistency is {@code null}
113 */
114 public DatabaseConfig withConsistency(Consistency consistency) {
115 setConsistency(consistency);
116 return this;
117 }
118
Madan Jampanif1b8e172015-03-23 11:42:02 -0700119 /**
120 * Returns the database name.
121 *
122 * @return The database name
123 */
124 public String getName() {
125 return name;
126 }
127
128 /**
129 * Sets the database name, returning the configuration for method chaining.
130 *
131 * @param name The database name
132 * @return The database configuration
133 * @throws java.lang.NullPointerException If the name is {@code null}
134 */
135 public DatabaseConfig withName(String name) {
136 setName(Assert.isNotNull(name, "name"));
137 return this;
138 }
139
140 /**
141 * Sets the database name.
142 *
143 * @param name The database name
144 * @throws java.lang.NullPointerException If the name is {@code null}
145 */
146 public void setName(String name) {
147 this.name = Assert.isNotNull(name, "name");
148 }
149
Madan Jampani94c23532015-02-05 17:40:01 -0800150 @Override
151 public CoordinatedResourceConfig resolve(ClusterConfig cluster) {
152 return new StateLogConfig(toMap())
153 .resolve(cluster)
154 .withResourceType(DefaultDatabase.class);
155 }
156
157}