Fix license header violations in controller
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / JMXNotifierConfigRegistry.java
1 /*
2  * Copyright (c) 2015 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.config.manager.impl.jmx;
10
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanRegistrationException;
17 import javax.management.MBeanServer;
18 import javax.management.NotCompliantMBeanException;
19 import javax.management.NotificationBroadcasterSupport;
20 import javax.management.ObjectName;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
26 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImplMXBean;
27
28 /**
29  * Thin wrapper over ConfigRegistry emitting JMX notifications
30  */
31 public class JMXNotifierConfigRegistry implements ConfigRegistryImplMXBean, AutoCloseable {
32
33     private final ConfigRegistryImplMXBean delegate;
34     private final NotifierMXBeanImpl notifier;
35     private final MBeanServer mBeanServer;
36
37     public JMXNotifierConfigRegistry(final ConfigRegistryImplMXBean delegate, final MBeanServer mBeanServer) {
38         this.delegate = delegate;
39         notifier = new NotifierMXBeanImpl();
40         this.mBeanServer = mBeanServer;
41         registerMBean(notifier, this.mBeanServer, ConfigJMXNotification.OBJECT_NAME);
42     }
43
44     private static void registerMBean(final Object instance, final MBeanServer mbs, final ObjectName on) {
45         try {
46             mbs.registerMBean(instance, on);
47         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
48             throw new IllegalStateException("Unable to register " + instance + " as " + on, e);
49         }
50     }
51
52     @Override
53     public long getVersion() {
54         return delegate.getVersion();
55     }
56
57     @Override
58     public ObjectName beginConfig() {
59         return delegate.beginConfig();
60     }
61
62     @Override
63     public CommitStatus commitConfig(final ObjectName transactionControllerON) throws ConflictingVersionException, ValidationException {
64         final CommitStatus commitStatus = delegate.commitConfig(transactionControllerON);
65         notifier.notifyCommit(ObjectNameUtil.getTransactionName(transactionControllerON));
66         return commitStatus;
67     }
68
69     @Override
70     public List<ObjectName> getOpenConfigs() {
71         return delegate.getOpenConfigs();
72     }
73
74     @Override
75     public boolean isHealthy() {
76         return delegate.isHealthy();
77     }
78
79     @Override
80     public Set<String> getAvailableModuleNames() {
81         return delegate.getAvailableModuleNames();
82     }
83
84     @Override
85     public Set<ObjectName> lookupConfigBeans() {
86         return delegate.lookupConfigBeans();
87     }
88
89     @Override
90     public Set<ObjectName> lookupConfigBeans(final String moduleName) {
91         return delegate.lookupConfigBeans(moduleName);
92     }
93
94     @Override
95     public Set<ObjectName> lookupConfigBeans(final String moduleName, final String instanceName) {
96         return delegate.lookupConfigBeans(moduleName, instanceName);
97     }
98
99     @Override
100     public ObjectName lookupConfigBean(final String moduleName, final String instanceName) throws InstanceNotFoundException {
101         return delegate.lookupConfigBean(moduleName, instanceName);
102     }
103
104     @Override
105     public void checkConfigBeanExists(final ObjectName objectName) throws InstanceNotFoundException {
106         delegate.checkConfigBeanExists(objectName);
107     }
108
109     @Override
110     public Set<String> getAvailableModuleFactoryQNames() {
111         return delegate.getAvailableModuleFactoryQNames();
112     }
113
114     @Override
115     public Set<ObjectName> lookupRuntimeBeans() {
116         return delegate.lookupRuntimeBeans();
117     }
118
119     @Override
120     public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
121         return delegate.lookupRuntimeBeans(moduleName, instanceName);
122     }
123
124     @Override
125     public ObjectName lookupConfigBeanByServiceInterfaceName(final String serviceInterfaceQName, final String refName) {
126         return delegate.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
127     }
128
129     @Override
130     public Map<String, Map<String, ObjectName>> getServiceMapping() {
131         return delegate.getServiceMapping();
132     }
133
134     @Override
135     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(final String serviceInterfaceQName) {
136         return delegate.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
137     }
138
139     @Override
140     public Set<String> lookupServiceInterfaceNames(final ObjectName objectName) throws InstanceNotFoundException {
141         return delegate.lookupServiceInterfaceNames(objectName);
142     }
143
144     @Override
145     public String getServiceInterfaceName(final String namespace, final String localName) {
146         return delegate.getServiceInterfaceName(namespace, localName);
147     }
148
149     @Override
150     public ObjectName getServiceReference(final String serviceInterfaceQName, final String refName) throws InstanceNotFoundException {
151         return delegate.getServiceReference(serviceInterfaceQName, refName);
152     }
153
154     @Override
155     public void checkServiceReferenceExists(final ObjectName objectName) throws InstanceNotFoundException {
156         delegate.checkServiceReferenceExists(objectName);
157     }
158
159     @Override
160     public void close() {
161         try {
162             mBeanServer.unregisterMBean(ConfigJMXNotification.OBJECT_NAME);
163         } catch (InstanceNotFoundException | MBeanRegistrationException e) {
164             throw new IllegalStateException("Notifier: " + ConfigJMXNotification.OBJECT_NAME + " not found in JMX when closing");
165         }
166     }
167
168     public interface NotifierMXBean {}
169
170     public static class NotifierMXBeanImpl extends NotificationBroadcasterSupport implements NotifierMXBean {
171
172         private void notifyCommit(final String transactionName) {
173             sendNotification(ConfigJMXNotification.afterCommit(this, "commit success " + transactionName));
174         }
175     }
176 }

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