Merge "Initial message bus implementation"
[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 java.util.Set;
12 import javax.management.InstanceAlreadyExistsException;
13 import javax.management.InstanceNotFoundException;
14 import javax.management.MBeanRegistrationException;
15 import javax.management.MBeanServer;
16 import javax.management.NotCompliantMBeanException;
17 import javax.management.NotificationBroadcasterSupport;
18 import javax.management.ObjectName;
19 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
20 import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
21 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Element;
25
26 public class DefaultCommitNotificationProducer extends NotificationBroadcasterSupport implements
27         DefaultCommitOperationMXBean, AutoCloseable, CommitNotifier {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DefaultCommitNotificationProducer.class);
30
31     private final MBeanServer mbeanServer;
32
33     private final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
34
35     public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
36         this.mbeanServer = mBeanServer;
37         LOG.debug("Registering to JMX under {}", on);
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 IllegalStateException("Unable to register " + instance + " as " + on, e);
46         }
47     }
48
49     @Override
50     public void sendCommitNotification(String message, Element cfgSnapshot, Set<String> capabilities) {
51         CommitJMXNotification notif = NetconfJMXNotification.afterCommit(this, message, cfgSnapshot, capabilities);
52         LOG.debug("Notification about commit {} sent", notif);
53         sendNotification(notif);
54     }
55
56     @Override
57     public void close() {
58         try {
59             mbeanServer.unregisterMBean(on);
60         } catch (InstanceNotFoundException | MBeanRegistrationException e) {
61             LOG.warn("Ignoring exception while unregistering {} as {}", this, on, e);
62         }
63     }
64 }