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.opendaylight.controller.netconf.client.NetconfClient;
-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 java.util.regex.Pattern;
+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 containing latest
+ * Responsible for listening for notifications from netconf (via JMX) containing latest
* committed configuration that should be persisted, and also for loading last
* configuration.
*/
@ThreadSafe
-public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
+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 NetconfClient netconfClient;
- private final Persister persisterAggregator;
- private final Pattern ignoredMissingCapabilityRegex;
+ private final NotificationListener listener;
- public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, NetconfClient netconfClient,
- Persister persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
- this.mBeanServerConnection = mBeanServerConnection;
- this.netconfClient = netconfClient;
- this.persisterAggregator = persisterAggregator;
- this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
+
+ public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
+ this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
}
- public void init() {
- registerAsJMXListener();
+ public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
+ this.mBeanServerConnection = mBeanServerConnection;
+ this.listener = notificationListener;
+ registerAsJMXListener(mBeanServerConnection, listener);
}
- private void registerAsJMXListener() {
- logger.trace("Called registerAsJMXListener");
+ private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
+ LOG.trace("Called registerAsJMXListener");
try {
- mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, this, null, null);
+ 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 void handleNotification(Notification notification, Object handback) {
- if (notification instanceof NetconfJMXNotification == false)
+ public synchronized void close() {
+ // unregister from JMX
+ final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
+ try {
+ if (mBeanServerConnection.isRegistered(on)) {
+ mBeanServerConnection.removeNotificationListener(on, listener);
+ }
+ } catch (final Exception e) {
+ LOG.warn("Unable to unregister {} as listener for {}", listener, on, e);
+ }
+ }
+}
+
+class ConfigPersisterNotificationListener implements NotificationListener {
+ private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
+
+ private final Persister persisterAggregator;
+
+ ConfigPersisterNotificationListener(final Persister persisterAggregator) {
+ this.persisterAggregator = persisterAggregator;
+ }
+
+ @Override
+ 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 (Throwable e) {
+ } catch (final Exception e) {
// log exceptions from notification Handler here since
// notificationBroadcastSupport logs only DEBUG level
- logger.warn("Exception occured during notification handling: ", e);
+ LOG.warn("Failed to handle notification {}", notification, e);
throw e;
}
- } else
+ } else {
throw new IllegalStateException("Unknown config registry notification type " + notification);
+ }
}
private void handleAfterCommitNotification(final CommitJMXNotification notification) {
try {
persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
- notification.getCapabilities(), ignoredMissingCapabilityRegex));
- logger.trace("Configuration persisted successfully");
- } catch (IOException e) {
+ notification.getCapabilities()));
+ LOG.trace("Configuration persisted successfully");
+ } catch (final IOException e) {
throw new RuntimeException("Unable to persist configuration snapshot", e);
}
}
-
- @Override
- public synchronized void close() {
- // unregister from JMX
- ObjectName on = DefaultCommitOperationMXBean.objectName;
- try {
- if (mBeanServerConnection.isRegistered(on)) {
- mBeanServerConnection.removeNotificationListener(on, this);
- }
- } catch (Exception e) {
- logger.warn("Unable to unregister {} as listener for {}", this, on, e);
- }
- }
-
- public NetconfClient getNetconfClient() {
- return netconfClient;
- }
-
}