Bug 9288 - DistributedDataStoreFactory in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContextPropertiesUpdater.java
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextPropertiesUpdater.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextPropertiesUpdater.java
new file mode 100644 (file)
index 0000000..7dc01fc
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore;
+
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Update DatastoreContext settings on invoke update method.
+ *
+ */
+public class DatastoreContextPropertiesUpdater implements AutoCloseable {
+
+    public interface Listener {
+        void onDatastoreContextUpdated(DatastoreContextFactory contextFactory);
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextPropertiesUpdater.class);
+
+    private final DatastoreContextIntrospector introspector;
+    private Listener listener;
+
+    /**
+     * Base init of updater for DatastoreContext settings with base properties.
+     *
+     * @param introspector
+     *            - introspection on DatastoreContext
+     * @param props
+     *            - base properties
+     */
+    public DatastoreContextPropertiesUpdater(final DatastoreContextIntrospector introspector,
+            final Map<String, Object> props) {
+        this.introspector = introspector;
+        update(props);
+    }
+
+    public void setListener(final Listener listener) {
+        this.listener = listener;
+    }
+
+    public void update(final Map<String, Object> properties) {
+        LOG.debug("Overlaying settings: {}", properties);
+
+        if (introspector.update(properties) && listener != null) {
+            listener.onDatastoreContextUpdated(introspector.newContextFactory());
+        }
+    }
+
+    @Override
+    public void close() {
+        listener = null;
+    }
+}