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;
26 import java.util.regex.Pattern;
29 * Responsible for listening for notifications from netconf (via JMX) containing latest
30 * committed configuration that should be persisted, and also for loading last
34 public class ConfigPersisterNotificationHandler implements Closeable {
36 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
37 private final MBeanServerConnection mBeanServerConnection;
38 private final ConfigPersisterNotificationListener listener;
41 public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection,
42 Persister persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
43 this.mBeanServerConnection = mBeanServerConnection;
44 listener = new ConfigPersisterNotificationListener(persisterAggregator, ignoredMissingCapabilityRegex);
45 registerAsJMXListener(mBeanServerConnection, listener);
49 private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
50 logger.trace("Called registerAsJMXListener");
52 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
53 } catch (InstanceNotFoundException | IOException e) {
54 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
59 public synchronized void close() {
60 // unregister from JMX
61 ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
63 if (mBeanServerConnection.isRegistered(on)) {
64 mBeanServerConnection.removeNotificationListener(on, listener);
66 } catch (Exception e) {
67 logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
72 class ConfigPersisterNotificationListener implements NotificationListener {
73 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
75 private final Persister persisterAggregator;
76 private final Pattern ignoredMissingCapabilityRegex;
78 ConfigPersisterNotificationListener(Persister persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
79 this.persisterAggregator = persisterAggregator;
80 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
84 public void handleNotification(Notification notification, Object handback) {
85 if (notification instanceof NetconfJMXNotification == false)
88 // Socket should not be closed at this point
89 // Activator unregisters this as JMX listener before close is called
91 logger.trace("Received notification {}", notification);
92 if (notification instanceof CommitJMXNotification) {
94 handleAfterCommitNotification((CommitJMXNotification) notification);
95 } catch (Throwable e) {
96 // log exceptions from notification Handler here since
97 // notificationBroadcastSupport logs only DEBUG level
98 logger.warn("Exception occured during notification handling: ", e);
102 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(), ignoredMissingCapabilityRegex));
109 logger.trace("Configuration persisted successfully");
110 } catch (IOException e) {
111 throw new RuntimeException("Unable to persist configuration snapshot", e);