Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / DynamicReadableWrapper.java
1 /*
2  * Copyright (c) 2013, 2017 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 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
9
10 import javax.management.Attribute;
11 import javax.management.AttributeList;
12 import javax.management.AttributeNotFoundException;
13 import javax.management.InvalidAttributeValueException;
14 import javax.management.MBeanException;
15 import javax.management.MBeanServer;
16 import javax.management.ReflectionException;
17 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
20 import org.opendaylight.controller.config.spi.Module;
21
22 /**
23  * Wraps {@link org.opendaylight.controller.config.spi.Module} in a
24  * {@link DynamicMBeanWithInstance}. Setting attributes is disabled.
25  */
26 public class DynamicReadableWrapper extends AbstractDynamicWrapper implements
27         DynamicMBeanWithInstance {
28     private final AutoCloseable instance;
29
30     public DynamicReadableWrapper(final Module module, final AutoCloseable instance,
31             final ModuleIdentifier moduleIdentifier, final MBeanServer internalServer,
32             final MBeanServer configMBeanServer) {
33         super(module, false, moduleIdentifier, ObjectNameUtil
34                 .createReadOnlyModuleON(moduleIdentifier),
35                 getEmptyOperations(), internalServer, configMBeanServer);
36         this.instance = instance;
37     }
38
39     @Override
40     public Module getModule() {
41         return module;
42     }
43
44     @Override
45     public AutoCloseable getInstance() {
46         return instance;
47     }
48
49     @Override
50     public Object invoke(final String actionName, final Object[] params, final String[] signature)
51             throws MBeanException, ReflectionException {
52         if ("getInstance".equals(actionName)
53                 && (params == null || params.length == 0)
54                 && (signature == null || signature.length == 0)) {
55             return getInstance();
56         }
57         return super.invoke(actionName, params, signature);
58     }
59
60     @Override
61     public Object getAttribute(final String attributeName)
62             throws AttributeNotFoundException, MBeanException,
63             ReflectionException {
64         if ("getInstance".equals(attributeName)) {
65             return getInstance();
66         }
67         return super.getAttribute(attributeName);
68     }
69
70     @Override
71     public void setAttribute(final Attribute attribute)
72             throws AttributeNotFoundException, InvalidAttributeValueException,
73             MBeanException, ReflectionException {
74         throw new UnsupportedOperationException(
75                 "setAttributes is not supported on " + moduleIdentifier);
76     }
77
78     @Override
79     public AttributeList setAttributes(final AttributeList attributes) {
80         throw new UnsupportedOperationException(
81                 "setAttributes is not supported on " + moduleIdentifier);
82     }
83 }