aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseConfig.java
blob: bd774b99b8096fa81135fee13624c692344a1125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.store.consistent.impl;

import com.typesafe.config.ConfigValueFactory;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.cluster.internal.coordinator.CoordinatedResourceConfig;
import net.kuujo.copycat.protocol.Consistency;
import net.kuujo.copycat.resource.ResourceConfig;
import net.kuujo.copycat.state.StateLogConfig;
import net.kuujo.copycat.util.internal.Assert;

import java.util.Map;

/**
 * Database configuration.
 *
 */
public class DatabaseConfig extends ResourceConfig<DatabaseConfig> {
  private static final String DATABASE_CONSISTENCY = "consistency";

  private static final String DEFAULT_CONFIGURATION = "database-defaults";
  private static final String CONFIGURATION = "database";

  private String name;

  public DatabaseConfig() {
    super(CONFIGURATION, DEFAULT_CONFIGURATION);
  }

  public DatabaseConfig(Map<String, Object> config) {
    super(config, CONFIGURATION, DEFAULT_CONFIGURATION);
  }

  public DatabaseConfig(String resource) {
    super(resource, CONFIGURATION, DEFAULT_CONFIGURATION);
  }

  protected DatabaseConfig(DatabaseConfig config) {
    super(config);
  }

  @Override
  public DatabaseConfig copy() {
    return new DatabaseConfig(this);
  }

  /**
   * Sets the database read consistency.
   *
   * @param consistency The database read consistency.
   * @throws java.lang.NullPointerException If the consistency is {@code null}
   */
  public void setConsistency(String consistency) {
    this.config = config.withValue(DATABASE_CONSISTENCY,
            ConfigValueFactory.fromAnyRef(
                    Consistency.parse(Assert.isNotNull(consistency, "consistency")).toString()));
  }

  /**
   * Sets the database read consistency.
   *
   * @param consistency The database read consistency.
   * @throws java.lang.NullPointerException If the consistency is {@code null}
   */
  public void setConsistency(Consistency consistency) {
    this.config = config.withValue(DATABASE_CONSISTENCY,
            ConfigValueFactory.fromAnyRef(
                    Assert.isNotNull(consistency, "consistency").toString()));
  }

  /**
   * Returns the database read consistency.
   *
   * @return The database read consistency.
   */
  public Consistency getConsistency() {
    return Consistency.parse(config.getString(DATABASE_CONSISTENCY));
  }

  /**
   * Sets the database read consistency, returning the configuration for method chaining.
   *
   * @param consistency The database read consistency.
   * @return The database configuration.
   * @throws java.lang.NullPointerException If the consistency is {@code null}
   */
  public DatabaseConfig withConsistency(String consistency) {
    setConsistency(consistency);
    return this;
  }

  /**
   * Sets the database read consistency, returning the configuration for method chaining.
   *
   * @param consistency The database read consistency.
   * @return The database configuration.
   * @throws java.lang.NullPointerException If the consistency is {@code null}
   */
  public DatabaseConfig withConsistency(Consistency consistency) {
    setConsistency(consistency);
    return this;
  }

  /**
   * Returns the database name.
   *
   * @return The database name
   */
  public String getName() {
      return name;
  }

  /**
   * Sets the database name, returning the configuration for method chaining.
   *
   * @param name The database name
   * @return The database configuration
   * @throws java.lang.NullPointerException If the name is {@code null}
   */
  public DatabaseConfig withName(String name) {
      setName(Assert.isNotNull(name, "name"));
      return this;
  }

  /**
   * Sets the database name.
   *
   * @param name The database name
   * @throws java.lang.NullPointerException If the name is {@code null}
   */
  public void setName(String name) {
      this.name = Assert.isNotNull(name, "name");
  }

  @Override
  public CoordinatedResourceConfig resolve(ClusterConfig cluster) {
    return new StateLogConfig(toMap())
      .resolve(cluster)
      .withResourceType(DefaultDatabase.class);
  }

}