2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl;
11 import org.opendaylight.controller.config.persist.api.Persister;
12 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
13 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
14 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import javax.annotation.concurrent.ThreadSafe;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.MBeanServerConnection;
21 import javax.management.Notification;
22 import javax.management.NotificationListener;
23 import javax.management.ObjectName;
24 import java.io.Closeable;
25 import java.io.IOException;
28 * Responsible for listening for notifications from netconf (via JMX) containing latest
29 * committed configuration that should be persisted, and also for loading last
33 public class ConfigPersisterNotificationHandler implements Closeable {
35 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
36 private final MBeanServerConnection mBeanServerConnection;
37 private final NotificationListener listener;
40 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
41 this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
44 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
45 this.mBeanServerConnection = mBeanServerConnection;
46 this.listener = notificationListener;
47 registerAsJMXListener(mBeanServerConnection, listener);
50 private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
51 logger.trace("Called registerAsJMXListener");
53 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
54 } catch (InstanceNotFoundException | IOException e) {
55 throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
60 public synchronized void close() {
61 // unregister from JMX
62 final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
64 if (mBeanServerConnection.isRegistered(on)) {
65 mBeanServerConnection.removeNotificationListener(on, listener);
67 } catch (final Exception e) {
68 logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
73 class ConfigPersisterNotificationListener implements NotificationListener {
74 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
76 private final Persister persisterAggregator;
78 ConfigPersisterNotificationListener(final Persister persisterAggregator) {
79 this.persisterAggregator = persisterAggregator;
83 public void handleNotification(final Notification notification, final Object handback) {
84 if (!(notification instanceof NetconfJMXNotification))
87 // Socket should not be closed at this point
88 // Activator unregisters this as JMX listener before close is called
90 logger.trace("Received notification {}", notification);
91 if (notification instanceof CommitJMXNotification) {
93 handleAfterCommitNotification((CommitJMXNotification) notification);
94 } catch (final Exception e) {
95 // log exceptions from notification Handler here since
96 // notificationBroadcastSupport logs only DEBUG level
97 logger.warn("Failed to handle notification {}", notification, e);
101 throw new IllegalStateException("Unknown config registry notification type " + notification);
105 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
107 persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
108 notification.getCapabilities()));
109 logger.trace("Configuration persisted successfully");
110 } catch (final IOException e) {
111 throw new RuntimeException("Unable to persist configuration snapshot", e);