2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl;
10 import static org.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
14 import com.google.common.collect.Sets;
15 import java.lang.management.ManagementFactory;
16 import java.lang.reflect.Field;
17 import java.util.Collections;
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.ObjectName;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.config.api.ModuleIdentifier;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
28 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
29 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
30 import org.opendaylight.controller.config.manager.impl.runtimembean.TestingRuntimeBean;
32 public class ConfigRegistryImplLookupTest extends AbstractLockedPlatformMBeanServerTest {
34 private ConfigRegistryImpl configRegistryImpl;
35 private BaseJMXRegistrator baseJMXRegistrator;
37 private static final String MODULE_NAMEA = "moduleA";
38 private static final String MODULE_NAMEB = "moduleB";
40 private static final String INSTANCE_NAMEA = "instA";
41 private static final String INSTANCE_NAMEB = "instB";
42 private static final String INSTANCE_NAMEC = "instC";
44 private static final ObjectName NAME1 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEA);
45 private static final ObjectName NAME2 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEB);
46 private static final ObjectName NAME3 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEA, INSTANCE_NAMEC);
47 private static final ObjectName NAME4 = ObjectNameUtil.createReadOnlyModuleON(MODULE_NAMEB, INSTANCE_NAMEA);
49 private static final ObjectName NAME5 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEA, INSTANCE_NAMEA,
50 Collections.<String, String>emptyMap());
51 private static final ObjectName NAME6 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEA, INSTANCE_NAMEB,
52 Collections.<String, String>emptyMap());
53 private static final ObjectName NAME8 = ObjectNameUtil.createRuntimeBeanName(MODULE_NAMEB, INSTANCE_NAMEA,
54 Collections.<String, String>emptyMap());
56 private static final ObjectName NAME9 = ObjectNameUtil.createTransactionModuleON("transaction", MODULE_NAMEA,
60 public void setUp() throws Exception {
61 configRegistryImpl = new ConfigRegistryImpl(null, ManagementFactory.getPlatformMBeanServer(), null);
62 Field field = configRegistryImpl.getClass().getDeclaredField("baseJMXRegistrator");
63 field.setAccessible(true);
64 baseJMXRegistrator = (BaseJMXRegistrator) field.get(configRegistryImpl);
66 registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME1);
67 registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME2);
68 registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME3);
69 registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME4);
71 registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME5);
72 registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME6);
73 registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, NAME8);
75 baseJMXRegistrator.createTransactionJMXRegistrator("transaction").createTransactionModuleJMXRegistrator()
76 .registerMBean(new TestingRuntimeBean(), NAME9);
80 private static void registerModuleBean(final TestingRuntimeBean testingRuntimeBean,
81 final BaseJMXRegistrator baseJMXRegistrator, final ObjectName objectName)
82 throws InstanceAlreadyExistsException {
83 baseJMXRegistrator.createModuleJMXRegistrator().registerMBean(testingRuntimeBean, objectName);
86 private static void registerRuntimeBean(final RuntimeBean object, final BaseJMXRegistrator baseJMXRegistrator,
87 final ObjectName runtimeON) throws InstanceAlreadyExistsException {
88 String factoryName = ObjectNameUtil.getFactoryName(runtimeON);
89 String instanceName = ObjectNameUtil.getInstanceName(runtimeON);
90 Map<String, String> properties = ObjectNameUtil.getAdditionalPropertiesOfRuntimeBeanName(runtimeON);
92 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = baseJMXRegistrator
93 .createRuntimeBeanRegistrator(new ModuleIdentifier(factoryName, instanceName));
95 assertThat(properties.isEmpty(), is(true));
97 runtimeBeanRegistrator.registerRoot(object);
101 public void cleanUp() {
102 baseJMXRegistrator.close();
106 public void testLookupConfigBeans() throws Exception {
107 Set<ObjectName> beans = configRegistryImpl.lookupConfigBeans();
108 assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3, NAME4), beans);
109 beans = configRegistryImpl.lookupConfigBeans();
110 assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3, NAME4), beans);
114 public void testLookupConfigBeanWithModuleName() throws Exception {
115 Set<ObjectName> bean = configRegistryImpl.lookupConfigBeans(MODULE_NAMEA);
116 assertEquals(Sets.newHashSet(NAME1, NAME2, NAME3), bean);
120 public void testLookupConfigBeanWithModuleNameAndInstanceName() throws Exception {
121 Set<ObjectName> bean = configRegistryImpl.lookupConfigBeans(MODULE_NAMEA, INSTANCE_NAMEA);
122 assertEquals(Sets.newHashSet(NAME1), bean);
126 public void testLookupRuntimeBeans() throws Exception {
127 Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans();
128 assertEquals(Sets.newHashSet(NAME5, NAME6, NAME8), beans);
129 beans = configRegistryImpl.lookupRuntimeBeans(null, null);
130 assertEquals(Sets.newHashSet(NAME5, NAME6, NAME8), beans);
134 public void testLookupRuntimeBeansWithIFcNameAndImplName() throws Exception {
135 Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans(MODULE_NAMEA, INSTANCE_NAMEA);
136 assertEquals(Sets.newHashSet(NAME5), beans);