Merge "Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP."
[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 RuntimeException("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 == false) {
83             return;
84         }
85
86         // Socket should not be closed at this point
87         // Activator unregisters this as JMX listener before close is called
88
89         logger.trace("Received notification {}", notification);
90         if (notification instanceof CommitJMXNotification) {
91             try {
92                 handleAfterCommitNotification((CommitJMXNotification) notification);
93             } catch (Throwable e) {
94                 // log exceptions from notification Handler here since
95                 // notificationBroadcastSupport logs only DEBUG level
96                 logger.warn("Exception occured during notification handling: ", 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             logger.trace("Configuration persisted successfully");
109         } catch (IOException e) {
110             throw new RuntimeException("Unable to persist configuration snapshot", e);
111         }
112     }
113 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.