Use a static YangInstanceIdentifier in AbstractEntityOwnerChangeListener
[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 static 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     private void overlaySettings(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
64         try {
65             ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
66
67             Configuration config = configAdmin.getConfiguration(CONFIG_ID);
68             if(config != null) {
69                 Dictionary<String, Object> properties = config.getProperties();
70
71                 LOG.debug("Overlaying settings: {}", properties);
72
73                 if(introspector.update(properties)) {
74                     if(listener != null) {
75                         listener.onDatastoreContextUpdated(introspector.newContextFactory());
76                     }
77                 }
78             } else {
79                 LOG.debug("No Configuration found for {}", CONFIG_ID);
80             }
81         } catch (IOException e) {
82             LOG.error("Error obtaining Configuration for pid {}", CONFIG_ID, e);
83         } catch(IllegalStateException e) {
84             // Ignore - indicates the bundleContext has been closed.
85         } finally {
86             try {
87                 bundleContext.ungetService(configAdminServiceReference);
88             } catch (Exception e) {
89                 LOG.debug("Error from ungetService", e);
90             }
91         }
92     }
93
94     @Override
95     public void close() {
96         listener = null;
97
98         if(configListenerServiceRef != null) {
99             configListenerServiceRef.unregister();
100         }
101     }
102
103     private class DatastoreConfigurationListener implements ConfigurationListener {
104         @Override
105         public void configurationEvent(ConfigurationEvent event) {
106             if(CONFIG_ID.equals(event.getPid()) && event.getType() == ConfigurationEvent.CM_UPDATED) {
107                 LOG.debug("configurationEvent: config {} was updated", CONFIG_ID);
108                 overlaySettings(event.getReference());
109             }
110         }
111     }
112 }