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.netconf.api.jmx.CommitJMXNotification;
12 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
13 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
14 import org.opendaylight.controller.netconf.client.NetconfClient;
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 containing latest
30 * committed configuration that should be persisted, and also for loading last
34 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
36 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
37 private final MBeanServerConnection mBeanServerConnection;
38 private final NetconfClient netconfClient;
39 private final PersisterAggregator persisterAggregator;
40 private final Pattern ignoredMissingCapabilityRegex;
42 public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, NetconfClient netconfClient,
43 PersisterAggregator persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
44 this.mBeanServerConnection = mBeanServerConnection;
45 this.netconfClient = netconfClient;
46 this.persisterAggregator = persisterAggregator;
47 this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
51 registerAsJMXListener();
54 private void registerAsJMXListener() {
55 logger.trace("Called registerAsJMXListener");
57 mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, this, null, null);
58 } catch (InstanceNotFoundException | IOException e) {
59 throw new RuntimeException("Cannot register as JMX listener to netconf", e);
64 public void handleNotification(Notification notification, Object handback) {
65 if (notification instanceof NetconfJMXNotification == false)
68 // Socket should not be closed at this point
69 // Activator unregisters this as JMX listener before close is called
71 logger.info("Received notification {}", notification);
72 if (notification instanceof CommitJMXNotification) {
74 handleAfterCommitNotification((CommitJMXNotification) notification);
75 } catch (Exception e) {
76 // TODO: notificationBroadcast support logs only DEBUG
77 logger.warn("Exception occured during notification handling: ", e);
81 throw new IllegalStateException("Unknown config registry notification type " + notification);
84 private void handleAfterCommitNotification(final CommitJMXNotification notification) {
86 persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
87 notification.getCapabilities(), ignoredMissingCapabilityRegex));
88 logger.info("Configuration persisted successfully");
89 } catch (IOException e) {
90 throw new RuntimeException("Unable to persist configuration snapshot", e);
95 public synchronized void close() {
96 // unregister from JMX
97 ObjectName on = DefaultCommitOperationMXBean.objectName;
99 if (mBeanServerConnection.isRegistered(on)) {
100 mBeanServerConnection.removeNotificationListener(on, this);
102 } catch (Exception e) {
103 logger.warn("Unable to unregister {} as listener for {}", this, on, e);
107 public NetconfClient getNetconfClient() {
108 return netconfClient;