Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ConfigRegistryImplLookupTest.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;
9
10 import static org.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13
14 import java.lang.management.ManagementFactory;
15 import java.lang.reflect.Field;
16 import java.util.Collections;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.ObjectName;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.config.api.ModuleIdentifier;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
29 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
30 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
31 import org.opendaylight.controller.config.manager.impl.runtimembean.TestingRuntimeBean;
32
33 import com.google.common.collect.Sets;
34
35 public class ConfigRegistryImplLookupTest extends
36         AbstractLockedPlatformMBeanServerTest {
37
38     private ConfigRegistryImpl configRegistryImpl;
39     private BaseJMXRegistrator baseJMXRegistrator;
40
41     private static final String moduleNameA = "moduleA";
42     private static final String moduleNameB = "moduleB";
43
44     private static final String instanceNameA = "instA";
45     private static final String instanceNameB = "instB";
46     private static final String instanceNameC = "instC";
47
48     private static final ObjectName name1 = ObjectNameUtil
49             .createReadOnlyModuleON(moduleNameA, instanceNameA);
50     private static final ObjectName name2 = ObjectNameUtil
51             .createReadOnlyModuleON(moduleNameA, instanceNameB);
52     private static final ObjectName name3 = ObjectNameUtil
53             .createReadOnlyModuleON(moduleNameA, instanceNameC);
54     private static final ObjectName name4 = ObjectNameUtil
55             .createReadOnlyModuleON(moduleNameB, instanceNameA);
56
57     private static final ObjectName name5 = ObjectNameUtil
58             .createRuntimeBeanName(moduleNameA, instanceNameA, Collections.<String, String>emptyMap());
59     private static final ObjectName name6 = ObjectNameUtil
60             .createRuntimeBeanName(moduleNameA, instanceNameB, Collections.<String, String>emptyMap());
61     private static final ObjectName name8 = ObjectNameUtil
62             .createRuntimeBeanName(moduleNameB, instanceNameA, Collections.<String, String>emptyMap());
63
64     private static final ObjectName name9 = ObjectNameUtil
65             .createTransactionModuleON("transaction", moduleNameA, instanceNameA);
66
67     @Before
68     public void setUp() throws Exception {
69         configRegistryImpl = new ConfigRegistryImpl(null, null,
70                 ManagementFactory.getPlatformMBeanServer());
71         Field field = configRegistryImpl.getClass().getDeclaredField(
72                 "baseJMXRegistrator");
73         field.setAccessible(true);
74         baseJMXRegistrator = (BaseJMXRegistrator) field.get(configRegistryImpl);
75
76         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, name1);
77         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, name2);
78         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, name3);
79         registerModuleBean(new TestingRuntimeBean(), baseJMXRegistrator, name4);
80
81         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, name5);
82         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, name6);
83         registerRuntimeBean(new TestingRuntimeBean(), baseJMXRegistrator, name8);
84
85         baseJMXRegistrator.createTransactionJMXRegistrator("transaction")
86                 .createTransactionModuleJMXRegistrator()
87                 .registerMBean(new TestingRuntimeBean(), name9);
88
89     }
90
91     private void registerModuleBean(TestingRuntimeBean testingRuntimeBean,
92             BaseJMXRegistrator baseJMXRegistrator, ObjectName objectName)
93             throws InstanceAlreadyExistsException {
94         baseJMXRegistrator.createModuleJMXRegistrator().registerMBean(
95                 testingRuntimeBean, objectName);
96     }
97
98     private void registerRuntimeBean(RuntimeBean object,
99             BaseJMXRegistrator baseJMXRegistrator, ObjectName runtimeON)
100             throws InstanceAlreadyExistsException {
101         String factoryName = ObjectNameUtil.getFactoryName(runtimeON);
102         String instanceName = ObjectNameUtil.getInstanceName(runtimeON);
103         Map<String, String> properties = ObjectNameUtil
104                 .getAdditionalPropertiesOfRuntimeBeanName(runtimeON);
105
106         RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = baseJMXRegistrator
107                 .createRuntimeBeanRegistrator(new ModuleIdentifier(factoryName, instanceName));
108
109         assertThat(properties.isEmpty(), is(true));
110
111         runtimeBeanRegistrator.registerRoot(object);
112     }
113
114     @After
115     public void cleanUp() {
116         baseJMXRegistrator.close();
117     }
118
119     @Test
120     public void testLookupConfigBeans() throws Exception {
121         Set<ObjectName> beans = configRegistryImpl.lookupConfigBeans();
122         assertEquals(Sets.newHashSet(name1, name2, name3, name4), beans);
123         beans = configRegistryImpl.lookupConfigBeans();
124         assertEquals(Sets.newHashSet(name1, name2, name3, name4), beans);
125     }
126
127     @Test
128     public void testLookupConfigBeanWithModuleName() throws Exception {
129         Set<ObjectName> bean = configRegistryImpl
130                 .lookupConfigBeans(moduleNameA);
131         assertEquals(Sets.newHashSet(name1, name2, name3), bean);
132     }
133
134     @Test
135     public void testLookupConfigBeanWithModuleNameAndInstanceName()
136             throws Exception {
137         Set<ObjectName> bean = configRegistryImpl.lookupConfigBeans(
138                 moduleNameA, instanceNameA);
139         assertEquals(Sets.newHashSet(name1), bean);
140     }
141
142     @Test
143     public void testLookupRuntimeBeans() throws Exception {
144         Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans();
145         assertEquals(Sets.newHashSet(name5, name6, name8), beans);
146         beans = configRegistryImpl.lookupRuntimeBeans(null, null);
147         assertEquals(Sets.newHashSet(name5, name6, name8), beans);
148     }
149
150     @Test
151     public void testLookupRuntimeBeansWithIFcNameAndImplName() throws Exception {
152         Set<ObjectName> beans = configRegistryImpl.lookupRuntimeBeans(
153                 moduleNameA, instanceNameA);
154         assertEquals(Sets.newHashSet(name5), beans);
155     }
156
157 }

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