Merge "Refactor yang-jmx-generator and -plugin to support list of dependencies."
[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.netconf.api.jmx.CommitJMXNotification;
12 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
13 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
14 import org.opendaylight.controller.netconf.client.NetconfClient;
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 import java.util.regex.Pattern;
27
28 /**
29  * Responsible for listening for notifications from netconf containing latest
30  * committed configuration that should be persisted, and also for loading last
31  * configuration.
32  */
33 @ThreadSafe
34 public class ConfigPersisterNotificationHandler implements NotificationListener, Closeable {
35
36     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class);
37     private final MBeanServerConnection mBeanServerConnection;
38     private final NetconfClient netconfClient;
39     private final PersisterAggregator persisterAggregator;
40     private final Pattern ignoredMissingCapabilityRegex;
41
42     public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, NetconfClient netconfClient,
43                                               PersisterAggregator persisterAggregator, Pattern ignoredMissingCapabilityRegex) {
44         this.mBeanServerConnection = mBeanServerConnection;
45         this.netconfClient = netconfClient;
46         this.persisterAggregator = persisterAggregator;
47         this.ignoredMissingCapabilityRegex = ignoredMissingCapabilityRegex;
48     }
49
50     public void init() {
51         registerAsJMXListener();
52     }
53
54     private void registerAsJMXListener() {
55         logger.trace("Called registerAsJMXListener");
56         try {
57             mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, this, null, null);
58         } catch (InstanceNotFoundException | IOException e) {
59             throw new RuntimeException("Cannot register as JMX listener to netconf", e);
60         }
61     }
62
63     @Override
64     public void handleNotification(Notification notification, Object handback) {
65         if (notification instanceof NetconfJMXNotification == false)
66             return;
67
68         // Socket should not be closed at this point
69         // Activator unregisters this as JMX listener before close is called
70
71         logger.info("Received notification {}", notification);
72         if (notification instanceof CommitJMXNotification) {
73             try {
74                 handleAfterCommitNotification((CommitJMXNotification) notification);
75             } catch (Exception e) {
76                 // TODO: notificationBroadcast support logs only DEBUG
77                 logger.warn("Exception occured during notification handling: ", e);
78                 throw e;
79             }
80         } else
81             throw new IllegalStateException("Unknown config registry notification type " + notification);
82     }
83
84     private void handleAfterCommitNotification(final CommitJMXNotification notification) {
85         try {
86             persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(),
87                     notification.getCapabilities(), ignoredMissingCapabilityRegex));
88             logger.info("Configuration persisted successfully");
89         } catch (IOException e) {
90             throw new RuntimeException("Unable to persist configuration snapshot", e);
91         }
92     }
93
94     @Override
95     public synchronized void close() {
96         // unregister from JMX
97         ObjectName on = DefaultCommitOperationMXBean.objectName;
98         try {
99             if (mBeanServerConnection.isRegistered(on)) {
100                 mBeanServerConnection.removeNotificationListener(on, this);
101             }
102         } catch (Exception e) {
103             logger.warn("Unable to unregister {} as listener for {}", this, on, e);
104         }
105     }
106
107     public NetconfClient getNetconfClient() {
108         return netconfClient;
109     }
110
111 }