Merge "Bug 809: Enhancements to the toaster example"
[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 ConfigPersisterNotificationListener listener;
38
39
40     public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection,
41                                               Persister persisterAggregator) {
42         this.mBeanServerConnection = mBeanServerConnection;
43         listener = new ConfigPersisterNotificationListener(persisterAggregator);
44         registerAsJMXListener(mBeanServerConnection, listener);
45
46     }
47
48     private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
49         logger.trace("Called registerAsJMXListener");
50         try {
51             mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
52         } catch (InstanceNotFoundException | IOException e) {
53             throw new IllegalStateException("Cannot register as JMX listener to netconf", e);
54         }
55     }
56
57     @Override
58     public synchronized void close() {
59         // unregister from JMX
60         ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
61         try {
62             if (mBeanServerConnection.isRegistered(on)) {
63                 mBeanServerConnection.removeNotificationListener(on, listener);
64             }
65         } catch (Exception e) {
66             logger.warn("Unable to unregister {} as listener for {}", listener, on, e);
67         }
68     }
69 }
70
71 class ConfigPersisterNotificationListener implements NotificationListener {
72     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationListener.class);
73
74     private final Persister persisterAggregator;
75
76     ConfigPersisterNotificationListener(Persister persisterAggregator) {
77         this.persisterAggregator = persisterAggregator;
78     }
79
80     @Override
81     public void handleNotification(Notification notification, Object handback) {
82         if (!(notification instanceof NetconfJMXNotification))
83             return;
84
85         // Socket should not be closed at this point
86         // Activator unregisters this as JMX listener before close is called
87
88         logger.trace("Received notification {}", notification);
89         if (notification instanceof CommitJMXNotification) {
90             try {
91                 handleAfterCommitNotification((CommitJMXNotification) notification);
92             } catch (Exception e) {
93                 // log exceptions from notification Handler here since
94                 // notificationBroadcastSupport logs only DEBUG level
95                 logger.warn("Failed to handle notification {}", notification, e);
96                 throw e;
97             }
98         } else {
99             throw new IllegalStateException("Unknown config registry notification type " + notification);
100         }
101     }
102
103     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
104         try {
105             persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
106                     notification.getCapabilities()));
107             logger.trace("Configuration persisted successfully");
108         } catch (IOException e) {
109             throw new RuntimeException("Unable to persist configuration snapshot", e);
110         }
111     }
112 }