4461054437ce61ef47fd2eea76a9b23ae15b5ef3
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / DefaultCommitNotificationProducer.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.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.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.w3c.dom.Element;
17
18 import javax.management.InstanceAlreadyExistsException;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.MBeanRegistrationException;
21 import javax.management.MBeanServer;
22 import javax.management.NotCompliantMBeanException;
23 import javax.management.NotificationBroadcasterSupport;
24 import javax.management.ObjectName;
25 import java.util.Set;
26
27 public class DefaultCommitNotificationProducer extends NotificationBroadcasterSupport implements
28         DefaultCommitOperationMXBean, AutoCloseable {
29
30     private static final Logger logger = LoggerFactory.getLogger(DefaultCommitNotificationProducer.class);
31
32     private final MBeanServer mbeanServer;
33
34     private final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
35
36     public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
37         this.mbeanServer = mBeanServer;
38         registerMBean(this, mbeanServer, on);
39     }
40
41     private static void registerMBean(final Object instance, final MBeanServer mbs, final ObjectName on) {
42         try {
43             mbs.registerMBean(instance, on);
44         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
45             throw new RuntimeException("Unable to register " + instance + " as " + on, e);
46         }
47     }
48
49     public void sendCommitNotification(String message, Element cfgSnapshot, Set<String> capabilities) {
50         CommitJMXNotification notif = NetconfJMXNotification.afterCommit(this, message, cfgSnapshot, capabilities);
51         logger.debug("Notification about commit {} sent", notif);
52         sendNotification(notif);
53     }
54
55     @Override
56     public void close() {
57         try {
58             mbeanServer.unregisterMBean(on);
59         } catch (InstanceNotFoundException | MBeanRegistrationException e) {
60             logger.warn("Ignoring exception while unregistering {} as {}", this, on, e);
61         }
62     }
63 }