X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-persister-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fpersist%2Fimpl%2FConfigPersisterNotificationHandler.java;h=fe7b3da770fb0b6358f6895e1174c78236320248;hp=1616857949d91c0678e7740ae2b277bfc12b25a0;hb=662f3629fea2904f8c14526ef412ef1650961e83;hpb=cb98269f3ccd77787672053054910696e4eea470 diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java index 1616857949..fe7b3da770 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java @@ -8,21 +8,20 @@ package org.opendaylight.controller.netconf.persist.impl; -import org.opendaylight.controller.config.persist.api.Persister; -import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification; -import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean; -import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import java.io.Closeable; +import java.io.IOException; import javax.annotation.concurrent.ThreadSafe; import javax.management.InstanceNotFoundException; import javax.management.MBeanServerConnection; import javax.management.Notification; import javax.management.NotificationListener; import javax.management.ObjectName; -import java.io.Closeable; -import java.io.IOException; +import org.opendaylight.controller.config.persist.api.Persister; +import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification; +import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean; +import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Responsible for listening for notifications from netconf (via JMX) containing latest @@ -32,68 +31,70 @@ import java.io.IOException; @ThreadSafe public class ConfigPersisterNotificationHandler implements Closeable { - private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class); private final MBeanServerConnection mBeanServerConnection; - private final ConfigPersisterNotificationListener listener; + private final NotificationListener listener; - public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, - Persister persisterAggregator) { + public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) { + this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator)); + } + + public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) { this.mBeanServerConnection = mBeanServerConnection; - listener = new ConfigPersisterNotificationListener(persisterAggregator); + this.listener = notificationListener; registerAsJMXListener(mBeanServerConnection, listener); - } - private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) { - logger.trace("Called registerAsJMXListener"); + private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) { + LOG.trace("Called registerAsJMXListener"); try { mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null); } catch (InstanceNotFoundException | IOException e) { - throw new RuntimeException("Cannot register as JMX listener to netconf", e); + throw new IllegalStateException("Cannot register as JMX listener to netconf", e); } } @Override public synchronized void close() { // unregister from JMX - ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME; + final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME; try { if (mBeanServerConnection.isRegistered(on)) { mBeanServerConnection.removeNotificationListener(on, listener); } - } catch (Exception e) { - logger.warn("Unable to unregister {} as listener for {}", listener, on, e); + } catch (final Exception e) { + LOG.warn("Unable to unregister {} as listener for {}", listener, on, e); } } } class ConfigPersisterNotificationListener implements NotificationListener { - private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class); + private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class); private final Persister persisterAggregator; - ConfigPersisterNotificationListener(Persister persisterAggregator) { + ConfigPersisterNotificationListener(final Persister persisterAggregator) { this.persisterAggregator = persisterAggregator; } @Override - public void handleNotification(Notification notification, Object handback) { - if (notification instanceof NetconfJMXNotification == false) { + public void handleNotification(final Notification notification, final Object handback) { + if (!(notification instanceof NetconfJMXNotification)) { return; } // Socket should not be closed at this point // Activator unregisters this as JMX listener before close is called - logger.trace("Received notification {}", notification); + LOG.trace("Received notification {}", notification); if (notification instanceof CommitJMXNotification) { try { handleAfterCommitNotification((CommitJMXNotification) notification); - } catch (Exception e) { + } catch (final Exception e) { // log exceptions from notification Handler here since // notificationBroadcastSupport logs only DEBUG level - logger.warn("Failed to handle notification {}", notification, e); + LOG.warn("Failed to handle notification {}", notification, e); throw e; } } else { @@ -105,8 +106,8 @@ class ConfigPersisterNotificationListener implements NotificationListener { try { persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(), notification.getCapabilities())); - logger.trace("Configuration persisted successfully"); - } catch (IOException e) { + LOG.trace("Configuration persisted successfully"); + } catch (final IOException e) { throw new RuntimeException("Unable to persist configuration snapshot", e); } }