Unit tests for ClientBackedDataStore class
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContextConfigAdminOverlay.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import java.io.IOException;
11 import java.util.Dictionary;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.ServiceReference;
14 import org.osgi.framework.ServiceRegistration;
15 import org.osgi.service.cm.Configuration;
16 import org.osgi.service.cm.ConfigurationAdmin;
17 import org.osgi.service.cm.ConfigurationEvent;
18 import org.osgi.service.cm.ConfigurationListener;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Class that overlays DatastoreContext settings with settings obtained from an OSGi Config Admin
24  * service.
25  *
26  * @author Thomas Pantelis
27  */
28 public class DatastoreContextConfigAdminOverlay implements AutoCloseable {
29     public static final String CONFIG_ID = "org.opendaylight.controller.cluster.datastore";
30
31     public interface Listener {
32         void onDatastoreContextUpdated(DatastoreContextFactory contextFactory);
33     }
34
35     private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextConfigAdminOverlay.class);
36
37     private final DatastoreContextIntrospector introspector;
38     private final BundleContext bundleContext;
39     private ServiceRegistration<?> configListenerServiceRef;
40     private Listener listener;
41
42     public DatastoreContextConfigAdminOverlay(DatastoreContextIntrospector introspector,
43             BundleContext bundleContext) {
44         this.introspector = introspector;
45         this.bundleContext = bundleContext;
46
47         ServiceReference<ConfigurationAdmin> configAdminServiceReference =
48                 bundleContext.getServiceReference(ConfigurationAdmin.class);
49         if (configAdminServiceReference == null) {
50             LOG.warn("No ConfigurationAdmin service found");
51         } else {
52             overlaySettings(configAdminServiceReference);
53
54             configListenerServiceRef = bundleContext.registerService(ConfigurationListener.class.getName(),
55                     new DatastoreConfigurationListener(), null);
56         }
57     }
58
59     public void setListener(Listener listener) {
60         this.listener = listener;
61     }
62
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
65         try {
66             ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
67
68             Configuration config = configAdmin.getConfiguration(CONFIG_ID);
69             if (config != null) {
70                 Dictionary<String, Object> properties = config.getProperties();
71
72                 LOG.debug("Overlaying settings: {}", properties);
73
74                 if (introspector.update(properties)) {
75                     if (listener != null) {
76                         listener.onDatastoreContextUpdated(introspector.newContextFactory());
77                     }
78                 }
79             } else {
80                 LOG.debug("No Configuration found for {}", CONFIG_ID);
81             }
82         } catch (IOException e) {
83             LOG.error("Error obtaining Configuration for pid {}", CONFIG_ID, e);
84         } catch (IllegalStateException e) {
85             // Ignore - indicates the bundleContext has been closed.
86         } finally {
87             try {
88                 bundleContext.ungetService(configAdminServiceReference);
89             } catch (Exception e) {
90                 LOG.debug("Error from ungetService", e);
91             }
92         }
93     }
94
95     @Override
96     public void close() {
97         listener = null;
98
99         if (configListenerServiceRef != null) {
100             configListenerServiceRef.unregister();
101         }
102     }
103
104     private class DatastoreConfigurationListener implements ConfigurationListener {
105         @Override
106         public void configurationEvent(ConfigurationEvent event) {
107             if (CONFIG_ID.equals(event.getPid()) && event.getType() == ConfigurationEvent.CM_UPDATED) {
108                 LOG.debug("configurationEvent: config {} was updated", CONFIG_ID);
109                 overlaySettings(event.getReference());
110             }
111         }
112     }
113 }