2 * Copyright (c) 2013 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.yangjmxgenerator;
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.hamcrest.CoreMatchers.isA;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertThat;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
21 import com.google.common.collect.Sets;
23 import java.net.URISyntaxException;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
30 import java.util.regex.Matcher;
31 import javax.management.openmbean.ArrayType;
32 import javax.management.openmbean.CompositeType;
33 import javax.management.openmbean.SimpleType;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
37 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
38 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
39 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
40 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
41 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
42 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
43 import org.opendaylight.mdsal.binding.model.api.Type;
44 import org.opendaylight.mdsal.binding.model.util.Types;
45 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.common.Revision;
48 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
52 public class ModuleMXBeanEntryTest extends AbstractYangTest {
54 public static final String PACKAGE_NAME = "pack2";
56 protected static final URI THREADS_NAMESPACE;
57 protected static final Revision THREADS_REVISION_DATE = Revision.of("2013-04-09");
61 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE + ":threads");
62 } catch (final URISyntaxException e) {
63 throw new ExceptionInInitializerError(e);
67 protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
72 this.modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
76 protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
77 return loadThreadsJava(this.modulesToSIEs, PACKAGE_NAME);
81 public void test_jmxImplModule() {
82 final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
83 final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
84 .create(this.threadsModule, PACKAGE_NAME,identitiesToSIs);
85 modulesToSIEs.putAll(ServiceInterfaceEntry.create(this.jmxModule,
86 PACKAGE_NAME,identitiesToSIs));
87 final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
88 .create(this.jmxImplModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context))
90 final Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
93 assertCorrectAttributesSize(namesToMBEs, attributes);
96 final DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
97 .get("thread-factory");
98 assertNotNull(threadFactoryAttribute);
99 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
100 assertThat(threadFactoryAttribute.getDependency().getSie()
101 .getTypeName(), is("ThreadFactoryServiceInterface"));
102 assertThat(threadFactoryAttribute.getAttributeYangName(),
103 is("thread-factory"));
104 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
105 is("threadFactory"));
106 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
107 is("ThreadFactory"));
108 assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
109 assertNull(threadFactoryAttribute.getNullableDefault());
110 assertNull(threadFactoryAttribute.getNullableDescription());
111 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
114 private void assertCorrectAttributesSize(final Map<String, ModuleMXBeanEntry> namesToMBEs, final Map<String, AttributeIfc> attributes) {
115 assertEquals(14, attributes.size());
116 assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
117 assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
119 assertEquals(4, namesToMBEs.get("impl").getAttributes().size());
120 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().size());
121 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().iterator().next().getAttributes().size());
124 protected RuntimeBeanEntry findFirstByYangName(
125 final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
126 for (final RuntimeBeanEntry rb : runtimeBeans) {
127 if (yangName.equals(rb.getYangName())) {
131 throw new IllegalArgumentException("Yang name not found:" + yangName
132 + " in " + runtimeBeans);
135 protected RuntimeBeanEntry findFirstByNamePrefix(final Collection<RuntimeBeanEntry> runtimeBeans, final String namePrefix) {
136 for (final RuntimeBeanEntry rb : runtimeBeans) {
137 if (namePrefix.equals(rb.getJavaNamePrefix())) {
142 throw new IllegalArgumentException("Name prefix not found:" + namePrefix
143 + " in " + runtimeBeans);
147 public void testGetWhenConditionMatcher() {
148 assertMatches("config",
149 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
151 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
152 assertMatches("config",
153 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
156 private void assertMatches(final String prefix, final String input) {
157 final RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
158 doReturn(input).when(whenConstraint).toString();
159 final Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
161 assertTrue(output.matches());
162 assertEquals("threadpool-dynamic", output.group(1));
166 public void testThreadsJava() {
167 final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
169 { // check threadpool-dynamic
170 final ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
171 .get(THREADPOOL_DYNAMIC_MXB_NAME);
172 final Map<String, AttributeIfc> attributes = dynamicThreadPool
174 // core-size, keepalive, maximum-size
176 final Set<String> longAttribs = Sets.newHashSet("core-size",
178 for (final String longAttrib : longAttribs) {
180 final TypedAttribute attribute = (TypedAttribute) attributes
182 assertThat("Failed to check " + longAttrib,
184 is((Type) Types.typeForClass(Long.class)));
186 // check dependency on thread factory
187 final QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
188 THREADS_REVISION_DATE, "threadfactory");
189 final ServiceInterfaceEntry threadFactorySIEntry = this.modulesToSIEs
190 .get(threadfactoryQName);
191 assertNotNull(threadFactorySIEntry);
192 final boolean expectedMandatory = true;
193 final TypedAttribute actualThreadFactory = (TypedAttribute) attributes
194 .get("threadfactory");
196 final DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
197 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
198 .getUnknownSchemaNodes();
199 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
200 final AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
201 mockedDataSchemaNode, threadFactorySIEntry,
202 expectedMandatory, "threadfactory description");
203 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
206 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
207 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
208 assertThat(dynamicThreadPool.getNullableDescription(),
209 is("threadpool-dynamic description"));
210 assertThat(dynamicThreadPool.getYangModuleName(),
211 is("config-threads-java"));
212 assertThat(dynamicThreadPool.getYangModuleLocalname(),
213 is(THREADPOOL_DYNAMIC_MXB_NAME));
215 // check root runtime bean
216 final Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
218 assertThat(runtimeBeans.size(), is(1));
219 final RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
220 THREADPOOL_DYNAMIC_MXB_NAME);
221 assertThat(rootRB.isRoot(), is(true));
222 assertThat(rootRB.getAttributes().size(), is(1));
223 final JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
225 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
226 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
227 assertThat(attribute.getType().getFullyQualifiedName(),
228 is(Long.class.getName()));
230 {// check threadfactory-naming
231 final ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
232 .get(THREADFACTORY_NAMING_MXB_NAME);
233 final Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
235 assertThat(runtimeBeans.size(), is(4));
237 final RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
239 assertNotNull(threadRB);
240 assertFalse(threadRB.isRoot());
241 assertEquals("name", threadRB.getKeyYangName().get());
242 assertEquals("Name", threadRB.getKeyJavaName().get());
243 assertThat(threadRB.getAttributes().size(), is(1));
244 final AttributeIfc threadNameAttr = threadRB.getAttributes()
246 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
247 assertTrue(threadNameAttr instanceof JavaAttribute);
248 assertThat(((JavaAttribute) threadNameAttr).getType()
249 .getFullyQualifiedName(), is(String.class.getName()));
250 assertThat(threadRB.getRpcs().size(), is(2));
253 final RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
255 assertNotNull(streamRB);
256 assertFalse(streamRB.getKeyYangName().isPresent());
257 assertFalse(streamRB.getKeyJavaName().isPresent());
258 final Map<String, AttributeIfc> attributeMap = streamRB
259 .getYangPropertiesToTypesMap();
260 assertEquals(4, attributeMap.size());
262 final TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
263 assertNotNull(toAttr);
264 assertThat(toAttr.getAttributeYangName(), is("peer"));
265 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
266 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
267 assertThat(toAttr.getOpenType(), isA(CompositeType.class));
268 Set<String> propsExpected = new HashSet<>(2);
269 propsExpected.add("port");
270 propsExpected.add("core-size");
271 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
273 propsExpected = new HashSet<>(2);
274 propsExpected.add("Port");
275 propsExpected.add("CoreSize");
277 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
279 propsExpected = new HashSet<>(2);
280 propsExpected.add("port");
281 propsExpected.add("coreSize");
282 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
285 final JavaAttribute timestampAttr = (JavaAttribute) attributeMap
287 assertNotNull(timestampAttr);
289 final JavaAttribute stateAttr = (JavaAttribute) attributeMap
291 assertNotNull(stateAttr);
293 final ListAttribute innerStream = (ListAttribute) attributeMap
294 .get("inner-stream-list");
295 assertNotNull(innerStream);
296 assertThat(innerStream.getAttributeYangName(),
297 is("inner-stream-list"));
298 assertThat(innerStream.getLowerCaseCammelCase(),
299 is("innerStreamList"));
300 assertThat(innerStream.getUpperCaseCammelCase(),
301 is("InnerStreamList"));
302 assertThat(innerStream.getOpenType(), isA(ArrayType.class));
307 { // test multiple dependencies
308 final ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
309 final Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
310 assertEquals(1, attributes.size());
311 final AttributeIfc threadpoolsAttr = attributes.get("threadpools");
312 assertNotNull(threadpoolsAttr);
313 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
314 final ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;