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

©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.