Merge "Bug 1789: Reorder odl-mdsal-clustering feature before odl-restconf"
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / 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.netconf.persist.impl;
10
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.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
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
27 /**
28  * Responsible for listening for notifications from netconf (via JMX) containing latest
29  * committed configuration that should be persisted, and also for loading last
30  * configuration.
31  */
32 @ThreadSafe
33 public class ConfigPersisterNotificationHandler implements Closeable {
34
35     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
36     private final MBeanServerConnection mBeanServerConnection;
37     private final NotificationListener listener;
38
39
40     public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) {
41         this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator));
42     }
43
44     public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) {
45         this.mBeanServerConnection = mBeanServerConnection;
46         this.listener = notificationListener;
47         registerAsJMXListener(mBeanServerConnection, listener);
48     }
49
50     private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) {
51         logger.trace("Called registerAsJMXListener");
52         try {
53             mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
54         } catch (InstanceNotFoundException | IOException e) {
55             throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
56         }
57     }
58
59     @Override
60     public synchronized void close() {
61         // unregister from JMX
62         final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
63         try {
64             if (mBeanServerConnection.isRegistered(on)) {
65                 mBeanServerConnection.removeNotificationListener(on, listener);
66             }
67         } catch (final Exception e) {
68             logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
69         }
70     }
71 }
72
73 class ConfigPersisterNotificationListener implements NotificationListener {
74     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
75
76     private final Persister persisterAggregator;
77
78     ConfigPersisterNotificationListener(final Persister persisterAggregator) {
79         this.persisterAggregator = persisterAggregator;
80     }
81
82     @Override
83     public void handleNotification(final Notification notification, final Object handback) {
84         if (!(notification instanceof NetconfJMXNotification))
85             return;
86
87         // Socket should not be closed at this point
88         // Activator unregisters this as JMX listener before close is called
89
90         logger.trace("Received notification {}", notification);
91         if (notification instanceof CommitJMXNotification) {
92             try {
93                 handleAfterCommitNotification((CommitJMXNotification) notification);
94             } catch (final Exception e) {
95                 // log exceptions from notification Handler here since
96                 // notificationBroadcastSupport logs only DEBUG level
97                 logger.warn("Failed to handle notification {}", notification, e);
98                 throw e;
99             }
100         } else {
101             throw new IllegalStateException("Unknown config registry notification type " + notification);
102         }
103     }
104
105     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
106         try {
107             persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
108                     notification.getCapabilities()));
109             logger.trace("Configuration persisted successfully");
110         } catch (final IOException e) {
111             throw new RuntimeException("Unable to persist configuration snapshot", e);
112         }
113     }
114 }