Decouple config and netconf subsystems.
[controller.git] / opendaylight / config / config-persister-impl / src / main / java / org / opendaylight / controller / config / persist / impl / ConfigPersisterNotificationHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.persist.impl;
10
11 import com.google.common.base.Optional;
12 import java.io.Closeable;
13 import java.io.IOException;
14 import java.util.Set;
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;
31
32 /**
33  * Responsible for listening for notifications from netconf (via JMX) containing latest
34  * committed configuration that should be persisted, and also for loading last
35  * configuration.
36  */
37 @ThreadSafe
38 public class ConfigPersisterNotificationHandler implements Closeable {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
41     private final MBeanServerConnection mBeanServerConnection;
42     private final NotificationListener listener;
43
44
45     public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
46         this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator, facade));
47     }
48
49     public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
50         this.mBeanServerConnection = mBeanServerConnection;
51         this.listener = notificationListener;
52         registerAsJMXListener(mBeanServerConnection, listener);
53     }
54
55     private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
56         LOG.trace("Called registerAsJMXListener");
57         try {
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);
61         }
62     }
63
64     @Override
65     public synchronized void close() {
66         // unregister from JMX
67         final ObjectName on = ConfigJMXNotification.OBJECT_NAME;
68         try {
69             if (mBeanServerConnection.isRegistered(on)) {
70                 mBeanServerConnection.removeNotificationListener(on, listener);
71             }
72         } catch (final Exception e) {
73             LOG.warn("Unable to unregister {} as listener for {}", listener, on, e);
74         }
75     }
76 }
77
78 class ConfigPersisterNotificationListener implements NotificationListener {
79     private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
80
81     private final Persister persisterAggregator;
82     private final ConfigSubsystemFacadeFactory facade;
83
84     ConfigPersisterNotificationListener(final Persister persisterAggregator, final ConfigSubsystemFacadeFactory facade) {
85         this.persisterAggregator = persisterAggregator;
86         this.facade = facade;
87     }
88
89     @Override
90     public void handleNotification(final Notification notification, final Object handback) {
91         if (!(notification instanceof ConfigJMXNotification)) {
92             return;
93         }
94
95         // Socket should not be closed at this point
96         // Activator unregisters this as JMX listener before close is called
97
98         LOG.trace("Received notification {}", notification);
99         if (notification instanceof CommitJMXNotification) {
100             try {
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);
106                 throw e;
107             }
108         } else {
109             throw new IllegalStateException("Unknown config registry notification type " + notification);
110         }
111     }
112
113     private void handleAfterCommitNotification() {
114         try {
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);
122         }
123     }
124 }