Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / DefaultCommitNotificationProducer.java
diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/DefaultCommitNotificationProducer.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/DefaultCommitNotificationProducer.java
new file mode 100644 (file)
index 0000000..305fb57
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.impl;
+
+import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
+import org.opendaylight.controller.netconf.api.jmx.DefaultCommitOperationMXBean;
+import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
+
+import javax.management.*;
+import java.util.Set;
+
+public class DefaultCommitNotificationProducer extends NotificationBroadcasterSupport implements
+        DefaultCommitOperationMXBean, AutoCloseable {
+
+    private static final Logger logger = LoggerFactory.getLogger(DefaultCommitNotificationProducer.class);
+
+    private final MBeanServer mbeanServer;
+
+    private final ObjectName on = DefaultCommitOperationMXBean.objectName;
+
+    public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
+        this.mbeanServer = mBeanServer;
+        registerMBean(this, mbeanServer, on);
+    }
+
+    private static void registerMBean(final Object instance, final MBeanServer mbs, final ObjectName on) {
+        try {
+            mbs.registerMBean(instance, on);
+        } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
+            throw new RuntimeException("Unable to register " + instance + " as " + on, e);
+        }
+    }
+
+    public void sendCommitNotification(String message, Element cfgSnapshot, Set<String> capabilities) {
+        CommitJMXNotification notif = NetconfJMXNotification.afterCommit(this, message, cfgSnapshot, capabilities);
+        logger.debug("Notification about commit {} sent", notif);
+        sendNotification(notif);
+    }
+
+    @Override
+    public void close() {
+        try {
+            mbeanServer.unregisterMBean(on);
+        } catch (InstanceNotFoundException | MBeanRegistrationException e) {
+            logger.warn("Ignoring exception while unregistering {} as {}", this, on, e);
+        }
+    }
+}