ff3e36acfaf9f93dc5b8f510d676f3380577d44a
[onosfw.git] /
1 /*
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
17 package org.onosproject.store.consistent.impl;
18
19
20 import java.util.function.Consumer;
21
22 import net.kuujo.copycat.cluster.ClusterConfig;
23 import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
24 import net.kuujo.copycat.cluster.internal.coordinator.CoordinatorConfig;
25 import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
26 import net.kuujo.copycat.resource.Resource;
27
28 /**
29  * Database.
30  */
31 public interface Database extends DatabaseProxy<String, byte[]>, Resource<Database> {
32
33   /**
34    * Creates a new database with the default cluster configuration.<p>
35    *
36    * The database will be constructed with the default cluster configuration. The default cluster configuration
37    * searches for two resources on the classpath - {@code cluster} and {cluster-defaults} - in that order. Configuration
38    * options specified in {@code cluster.conf} will override those in {cluster-defaults.conf}.<p>
39    *
40    * Additionally, the database will be constructed with an database configuration that searches the classpath for
41    * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
42    * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
43    * as the map resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
44    * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
45    *
46    * @param name The database name.
47    * @return The database.
48    */
49   static Database create(String name) {
50     return create(name, new ClusterConfig(), new DatabaseConfig());
51   }
52
53   /**
54    * Creates a new database.<p>
55    *
56    * The database will be constructed with an database configuration that searches the classpath for
57    * three configuration files - {@code {name}}, {@code database}, {@code database-defaults}, {@code resource}, and
58    * {@code resource-defaults} - in that order. The first resource is a configuration resource with the same name
59    * as the database resource. If the resource is namespaced - e.g. `databases.my-database.conf` - then resource
60    * configurations will be loaded according to namespaces as well; for example, `databases.conf`.
61    *
62    * @param name The database name.
63    * @param cluster The cluster configuration.
64    * @return The database.
65    */
66   static Database create(String name, ClusterConfig cluster) {
67     return create(name, cluster, new DatabaseConfig());
68   }
69
70   /**
71    * Creates a new database.
72    *
73    * @param name The database name.
74    * @param cluster The cluster configuration.
75    * @param config The database configuration.
76
77    * @return The database.
78    */
79   static Database create(String name, ClusterConfig cluster, DatabaseConfig config) {
80     ClusterCoordinator coordinator =
81             new DefaultClusterCoordinator(new CoordinatorConfig().withName(name).withClusterConfig(cluster));
82     return coordinator.<Database>getResource(name, config.resolve(cluster))
83       .addStartupTask(() -> coordinator.open().thenApply(v -> null))
84       .addShutdownTask(coordinator::close);
85   }
86
87   /**
88    * Tells whether the database supports change notifications.
89    * @return true if notifications are supported; false otherwise
90    */
91   default boolean hasChangeNotificationSupport() {
92       return true;
93   }
94
95   /**
96    * Registers a new consumer of StateMachineUpdates.
97    * @param consumer consumer to register
98    */
99   void registerConsumer(Consumer<StateMachineUpdate> consumer);
100
101   /**
102    * Unregisters a consumer of StateMachineUpdates.
103    * @param consumer consumer to unregister
104    */
105   void unregisterConsumer(Consumer<StateMachineUpdate> consumer);
106 }