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 ConfigPersisterNotificationListener listener;
40 public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection,
41 Persister persisterAggregator) {
42 this.mBeanServerConnection = mBeanServerConnection;
43 listener = new ConfigPersisterNotificationListener(persisterAggregator);
44 registerAsJMXListener(mBeanServerConnection, listener);
48 private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
49 logger.trace("Called registerAsJMXListener");
51 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
52 } catch (InstanceNotFoundException | IOException e) {
53 throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
58 public synchronized void close() {
59 // unregister from JMX
60 ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
62 if (mBeanServerConnection.isRegistered(on)) {
63 mBeanServerConnection.removeNotificationListener(on, listener);
65 } catch (Exception e) {
66 logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
71 class ConfigPersisterNotificationListener implements NotificationListener {
72 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
74 private final Persister persisterAggregator;
76 ConfigPersisterNotificationListener(Persister persisterAggregator) {
77 this.persisterAggregator = persisterAggregator;
81 public void handleNotification(Notification notification, Object handback) {
82 if (!(notification instanceof NetconfJMXNotification))
85 // Socket should not be closed at this point
86 // Activator unregisters this as JMX listener before close is called
88 logger.trace("Received notification {}", notification);
89 if (notification instanceof CommitJMXNotification) {
91 handleAfterCommitNotification((CommitJMXNotification) notification);
92 } catch (Exception e) {
93 // log exceptions from notification Handler here since
94 // notificationBroadcastSupport logs only DEBUG level
95 logger.warn("Failed to handle notification {}", notification, e);
99 throw new IllegalStateException("Unknown config registry notification type " + notification);
103 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
105 persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
106 notification.getCapabilities()));
107 logger.trace("Configuration persisted successfully");
108 } catch (IOException e) {
109 throw new RuntimeException("Unable to persist configuration snapshot", e);