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