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.config.persist.impl;
11 import com.google.common.base.Optional;
12 import java.io.Closeable;
13 import java.io.IOException;
15 import javax.annotation.concurrent.ThreadSafe;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.MBeanServerConnection;
18 import javax.management.Notification;
19 import javax.management.NotificationListener;
20 import javax.management.ObjectName;
21 import org.opendaylight.controller.config.api.jmx.notifications.CommitJMXNotification;
22 import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
23 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
24 import org.opendaylight.controller.config.facade.xml.Datastore;
25 import org.opendaylight.controller.config.persist.api.Persister;
26 import org.opendaylight.controller.config.util.capability.Capability;
27 import org.opendaylight.controller.config.util.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Element;
33 * Responsible for listening for notifications from netconf (via JMX) containing latest
34 * committed configuration that should be persisted, and also for loading last
38 public class ConfigPersisterNotificationHandler implements Closeable {
40 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
41 private final MBeanServerConnection mBeanServerConnection;
42 private final NotificationListener listener;
45 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
46 this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator, facade));
49 public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
50 this.mBeanServerConnection = mBeanServerConnection;
51 this.listener = notificationListener;
52 registerAsJMXListener(mBeanServerConnection, listener);
55 private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
56 LOG.trace("Called registerAsJMXListener");
58 mBeanServerConnection.addNotificationListener(ConfigJMXNotification.OBJECT_NAME, listener, null, null);
59 } catch (InstanceNotFoundException | IOException e) {
60 throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
65 public synchronized void close() {
66 // unregister from JMX
67 final ObjectName on = ConfigJMXNotification.OBJECT_NAME;
69 if (mBeanServerConnection.isRegistered(on)) {
70 mBeanServerConnection.removeNotificationListener(on, listener);
72 } catch (final Exception e) {
73 LOG.warn("Unable to unregister {} as listener for {}", listener, on, e);
78 class ConfigPersisterNotificationListener implements NotificationListener {
79 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
81 private final Persister persisterAggregator;
82 private final ConfigSubsystemFacadeFactory facade;
84 ConfigPersisterNotificationListener(final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
85 this.persisterAggregator = persisterAggregator;
90 public void handleNotification(final Notification notification, final Object handback) {
91 if (!(notification instanceof ConfigJMXNotification)) {
95 // Socket should not be closed at this point
96 // Activator unregisters this as JMX listener before close is called
98 LOG.trace("Received notification {}", notification);
99 if (notification instanceof CommitJMXNotification) {
101 handleAfterCommitNotification();
102 } catch (final Exception e) {
103 // log exceptions from notification Handler here since
104 // notificationBroadcastSupport logs only DEBUG level
105 LOG.warn("Failed to handle notification {}", notification, e);
109 throw new IllegalStateException("Unknown config registry notification type " + notification);
113 private void handleAfterCommitNotification() {
115 final Set<Capability> currentCapabilities = facade.getCurrentCapabilities();
116 final Element configSnapshot = facade.createFacade("config-persister").getConfiguration(XmlUtil.newDocument(), Datastore.running, Optional.<String>absent());
117 persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(configSnapshot,
118 ConfigPusherImpl.transformCapabilities(currentCapabilities)));
119 LOG.trace("Configuration persisted successfully");
120 } catch (final IOException e) {
121 throw new RuntimeException("Unable to persist configuration snapshot", e);