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.opendaylight.controller.netconf.client.NetconfClient;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import javax.annotation.concurrent.ThreadSafe;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.MBeanServerConnection;
22 import javax.management.Notification;
23 import javax.management.NotificationListener;
24 import javax.management.ObjectName;
25 import java.io.Closeable;
26 import java.io.IOException;
27 import java.util.regex.Pattern;
30 * Responsible for listening for notifications from netconf containing latest
31 * committed configuration that should be persisted, and also for loading last
35 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
37 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
38 private final MBeanServerConnection mBeanServerConnection;
39 private final NetconfClient netconfClient;
40 private final Persister persisterAggregator;
41 private final Pattern ignoredMissingCapabilityRegex;
43 public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, NetconfClient netconfClient,
44 Persister persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
45 this.mBeanServerConnection = mBeanServerConnection;
46 this.netconfClient = netconfClient;
47 this.persisterAggregator = persisterAggregator;
48 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
52 registerAsJMXListener();
55 private void registerAsJMXListener() {
56 logger.trace("Called registerAsJMXListener");
58 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, this, null, null);
59 } catch (InstanceNotFoundException | IOException e) {
60 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
65 public void handleNotification(Notification notification, Object handback) {
66 if (notification instanceof NetconfJMXNotification == false)
69 // Socket should not be closed at this point
70 // Activator unregisters this as JMX listener before close is called
72 logger.info("Received notification {}", notification);
73 if (notification instanceof CommitJMXNotification) {
75 handleAfterCommitNotification((CommitJMXNotification) notification);
76 } catch (Throwable e) {
77 // log exceptions from notification Handler here since
78 // notificationBroadcastSupport logs only DEBUG level
79 logger.warn("Exception occured during notification handling: ", e);
83 throw new IllegalStateException("Unknown config registry notification type " + notification);
86 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
88 persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
89 notification.getCapabilities(), ignoredMissingCapabilityRegex));
90 logger.info("Configuration persisted successfully");
91 } catch (IOException e) {
92 throw new RuntimeException("Unable to persist configuration snapshot", e);
97 public synchronized void close() {
98 // unregister from JMX
99 ObjectName on = DefaultCommitOperationMXBean.objectName;
101 if (mBeanServerConnection.isRegistered(on)) {
102 mBeanServerConnection.removeNotificationListener(on, this);
104 } catch (Exception e) {
105 logger.warn("Unable to unregister {} as listener for {}", this, on, e);
109 public NetconfClient getNetconfClient() {
110 return netconfClient;