Initial code drop of netconf protocol 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 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.*;
19 import java.util.Set;
20
21 public class DefaultCommitNotificationProducer extends NotificationBroadcasterSupport implements
22         DefaultCommitOperationMXBean, AutoCloseable {
23
24     private static final Logger logger = LoggerFactory.getLogger(DefaultCommitNotificationProducer.class);
25
26     private final MBeanServer mbeanServer;
27
28     private final ObjectName on = DefaultCommitOperationMXBean.objectName;
29
30     public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
31         this.mbeanServer = mBeanServer;
32         registerMBean(this, mbeanServer, on);
33     }
34
35     private static void registerMBean(final Object instance, final MBeanServer mbs, final ObjectName on) {
36         try {
37             mbs.registerMBean(instance, on);
38         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
39             throw new RuntimeException("Unable to register " + instance + " as " + on, e);
40         }
41     }
42
43     public void sendCommitNotification(String message, Element cfgSnapshot, Set<String> capabilities) {
44         CommitJMXNotification notif = NetconfJMXNotification.afterCommit(this, message, cfgSnapshot, capabilities);
45         logger.debug("Notification about commit {} sent", notif);
46         sendNotification(notif);
47     }
48
49     @Override
50     public void close() {
51         try {
52             mbeanServer.unregisterMBean(on);
53         } catch (InstanceNotFoundException | MBeanRegistrationException e) {
54             logger.warn("Ignoring exception while unregistering {} as {}", this, on, e);
55         }
56     }
57 }