Decouple config and netconf subsystems.
[controller.git] / opendaylight / config / config-persister-impl / src / main / java / org / opendaylight / controller / config / persist / impl / ConfigPersisterNotificationHandler.java
@@ -6,22 +6,28 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.controller.netconf.persist.impl;
+package org.opendaylight.controller.config.persist.impl;
 
+import com.google.common.base.Optional;
 import java.io.Closeable;
 import java.io.IOException;
+import java.util.Set;
 import javax.annotation.concurrent.ThreadSafe;
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanServerConnection;
 import javax.management.Notification;
 import javax.management.NotificationListener;
 import javax.management.ObjectName;
+import org.opendaylight.controller.config.api.jmx.notifications.CommitJMXNotification;
+import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
+import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
+import org.opendaylight.controller.config.facade.xml.Datastore;
 import org.opendaylight.controller.config.persist.api.Persister;
-import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
-import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
-import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
+import org.opendaylight.controller.config.util.capability.Capability;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
 
 /**
  * Responsible for listening for notifications from netconf (via JMX) containing latest
@@ -36,8 +42,8 @@ public class ConfigPersisterNotificationHandler implements Closeable {
     private final NotificationListener listener;
 
 
-    public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
-        this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
+    public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
+        this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator, facade));
     }
 
     public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
@@ -49,7 +55,7 @@ public class ConfigPersisterNotificationHandler implements Closeable {
     private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
         LOG.trace("Called registerAsJMXListener");
         try {
-            mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
+            mBeanServerConnection.addNotificationListener(ConfigJMXNotification.OBJECT_NAME, listener, null, null);
         } catch (InstanceNotFoundException | IOException e) {
             throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
         }
@@ -58,7 +64,7 @@ public class ConfigPersisterNotificationHandler implements Closeable {
     @Override
     public synchronized void close() {
         // unregister from JMX
-        final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
+        final ObjectName on = ConfigJMXNotification.OBJECT_NAME;
         try {
             if (mBeanServerConnection.isRegistered(on)) {
                 mBeanServerConnection.removeNotificationListener(on, listener);
@@ -73,14 +79,16 @@ class ConfigPersisterNotificationListener implements NotificationListener {
     private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
 
     private final Persister persisterAggregator;
+    private final ConfigSubsystemFacadeFactory facade;
 
-    ConfigPersisterNotificationListener(final Persister persisterAggregator) {
+    ConfigPersisterNotificationListener(final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
         this.persisterAggregator = persisterAggregator;
+        this.facade = facade;
     }
 
     @Override
     public void handleNotification(final Notification notification, final Object handback) {
-        if (!(notification instanceof NetconfJMXNotification)) {
+        if (!(notification instanceof ConfigJMXNotification)) {
             return;
         }
 
@@ -90,7 +98,7 @@ class ConfigPersisterNotificationListener implements NotificationListener {
         LOG.trace("Received notification {}", notification);
         if (notification instanceof CommitJMXNotification) {
             try {
-                handleAfterCommitNotification((CommitJMXNotification) notification);
+                handleAfterCommitNotification();
             } catch (final Exception e) {
                 // log exceptions from notification Handler here since
                 // notificationBroadcastSupport logs only DEBUG level
@@ -102,10 +110,12 @@ class ConfigPersisterNotificationListener implements NotificationListener {
         }
     }
 
-    private void handleAfterCommitNotification(final CommitJMXNotification notification) {
+    private void handleAfterCommitNotification() {
         try {
-            persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
-                    notification.getCapabilities()));
+            final Set<Capability> currentCapabilities = facade.getCurrentCapabilities();
+            final Element configSnapshot = facade.createFacade("config-persister").getConfiguration(XmlUtil.newDocument(), Datastore.running, Optional.<String>absent());
+            persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(configSnapshot,
+                    ConfigPusherImpl.transformCapabilities(currentCapabilities)));
             LOG.trace("Configuration persisted successfully");
         } catch (final IOException e) {
             throw new RuntimeException("Unable to persist configuration snapshot", e);