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 java.io.Closeable;
12 import java.io.IOException;
13 import javax.annotation.concurrent.ThreadSafe;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.MBeanServerConnection;
16 import javax.management.Notification;
17 import javax.management.NotificationListener;
18 import javax.management.ObjectName;
19 import org.opendaylight.controller.config.persist.api.Persister;
20 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
21 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
22 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Responsible for listening for notifications from netconf (via JMX) containing latest
28 * committed configuration that should be persisted, and also for loading last
32 public class ConfigPersisterNotificationHandler implements Closeable {
34 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
35 private final MBeanServerConnection mBeanServerConnection;
36 private final NotificationListener listener;
39 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
40 this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
43 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
44 this.mBeanServerConnection = mBeanServerConnection;
45 this.listener = notificationListener;
46 registerAsJMXListener(mBeanServerConnection, listener);
49 private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
50 LOG.trace("Called registerAsJMXListener");
52 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
53 } catch (InstanceNotFoundException | IOException e) {
54 throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
59 public synchronized void close() {
60 // unregister from JMX
61 final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
63 if (mBeanServerConnection.isRegistered(on)) {
64 mBeanServerConnection.removeNotificationListener(on, listener);
66 } catch (final Exception e) {
67 LOG.warn("Unable to unregister {} as listener for {}", listener, on, e);
72 class ConfigPersisterNotificationListener implements NotificationListener {
73 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
75 private final Persister persisterAggregator;
77 ConfigPersisterNotificationListener(final Persister persisterAggregator) {
78 this.persisterAggregator = persisterAggregator;
82 public void handleNotification(final Notification notification, final Object handback) {
83 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 LOG.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 LOG.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 LOG.trace("Configuration persisted successfully");
110 } catch (final IOException e) {
111 throw new RuntimeException("Unable to persist configuration snapshot", e);