Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / HierarchicalRuntimeBeanRegistrationImpl.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 package org.opendaylight.controller.config.manager.impl.jmx;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
18 import org.opendaylight.controller.config.api.runtime.HierarchicalRuntimeBeanRegistration;
19 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
20
21 public class HierarchicalRuntimeBeanRegistrationImpl implements
22         HierarchicalRuntimeBeanRegistration {
23     private final ModuleIdentifier moduleIdentifier;
24     private final InternalJMXRegistrator internalJMXRegistrator;
25     private final Map<String, String> properties;
26
27     public HierarchicalRuntimeBeanRegistrationImpl(
28             ModuleIdentifier moduleIdentifier,
29             InternalJMXRegistrator internalJMXRegistrator,
30             Map<String, String> properties) {
31         this.moduleIdentifier = moduleIdentifier;
32         this.internalJMXRegistrator = internalJMXRegistrator;
33         this.properties = properties;
34     }
35
36     @Override
37     public ObjectName getObjectName() {
38         return ObjectNameUtil.createRuntimeBeanName(
39                 moduleIdentifier.getFactoryName(),
40                 moduleIdentifier.getInstanceName(), properties);
41     }
42
43     @Override
44     public HierarchicalRuntimeBeanRegistrationImpl register(String key,
45             String value, RuntimeBean mxBean) {
46         Map<String, String> currentProperties = new HashMap<>(properties);
47         currentProperties.put(key, value);
48         ObjectName on = ObjectNameUtil.createRuntimeBeanName(
49                 moduleIdentifier.getFactoryName(),
50                 moduleIdentifier.getInstanceName(), currentProperties);
51         InternalJMXRegistrator child = internalJMXRegistrator.createChild();
52         try {
53             child.registerMBean(mxBean, on);
54         } catch (InstanceAlreadyExistsException e) {
55             throw RootRuntimeBeanRegistratorImpl.sanitize(e, moduleIdentifier,
56                     on);
57         }
58         return new HierarchicalRuntimeBeanRegistrationImpl(moduleIdentifier,
59                 child, currentProperties);
60     }
61
62     @Override
63     public void close() {
64         internalJMXRegistrator.close();
65     }
66 }