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