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;
20 import com.google.common.collect.Sets;
22 import java.net.URISyntaxException;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.HashSet;
32 import java.util.regex.Matcher;
33 import javax.management.openmbean.ArrayType;
34 import javax.management.openmbean.CompositeType;
35 import javax.management.openmbean.SimpleType;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
39 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
40 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
41 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
42 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
43 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
44 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
45 import org.opendaylight.yangtools.binding.generator.util.Types;
46 import org.opendaylight.yangtools.sal.binding.model.api.Type;
47 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
53 public class ModuleMXBeanEntryTest extends AbstractYangTest {
55 public static final String PACKAGE_NAME = "pack2";
57 protected static final URI THREADS_NAMESPACE;
58 protected static final Date THREADS_REVISION_DATE;
62 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
64 } catch (URISyntaxException e) {
67 SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
69 THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
70 } catch (ParseException e) {
75 protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
80 modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
84 protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
85 return loadThreadsJava(modulesToSIEs, PACKAGE_NAME);
89 public void test_jmxImplModule() {
90 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
91 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
92 .create(threadsModule, PACKAGE_NAME,identitiesToSIs);
93 modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
94 PACKAGE_NAME,identitiesToSIs));
95 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
96 .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
98 Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
101 assertCorrectAttributesSize(namesToMBEs, attributes);
104 DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
105 .get("thread-factory");
106 assertNotNull(threadFactoryAttribute);
107 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
108 assertThat(threadFactoryAttribute.getDependency().getSie()
109 .getTypeName(), is("ThreadFactoryServiceInterface"));
110 assertThat(threadFactoryAttribute.getAttributeYangName(),
111 is("thread-factory"));
112 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
113 is("threadFactory"));
114 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
115 is("ThreadFactory"));
116 assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
117 assertNull(threadFactoryAttribute.getNullableDefault());
118 assertNull(threadFactoryAttribute.getNullableDescription());
119 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
122 private void assertCorrectAttributesSize(final Map<String, ModuleMXBeanEntry> namesToMBEs, final Map<String, AttributeIfc> attributes) {
123 assertEquals(14, attributes.size());
124 assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
125 assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
127 assertEquals(4, namesToMBEs.get("impl").getAttributes().size());
128 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().size());
129 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().iterator().next().getAttributes().size());
132 protected RuntimeBeanEntry findFirstByYangName(
133 final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
134 for (RuntimeBeanEntry rb : runtimeBeans) {
135 if (yangName.equals(rb.getYangName())) {
139 throw new IllegalArgumentException("Yang name not found:" + yangName
140 + " in " + runtimeBeans);
143 protected RuntimeBeanEntry findFirstByNamePrefix(final Collection<RuntimeBeanEntry> runtimeBeans, final String namePrefix) {
144 for (RuntimeBeanEntry rb : runtimeBeans) {
145 if (namePrefix.equals(rb.getJavaNamePrefix())) {
150 throw new IllegalArgumentException("Name prefix not found:" + namePrefix
151 + " in " + runtimeBeans);
155 public void testGetWhenConditionMatcher() {
156 assertMatches("config",
157 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
159 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
160 assertMatches("config",
161 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
164 private void assertMatches(final String prefix, final String input) {
165 RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
166 doReturn(input).when(whenConstraint).toString();
167 Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
169 assertTrue(output.matches());
170 assertEquals("threadpool-dynamic", output.group(1));
174 public void testThreadsJava() {
175 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
177 { // check threadpool-dynamic
178 ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
179 .get(THREADPOOL_DYNAMIC_MXB_NAME);
180 Map<String, AttributeIfc> attributes = dynamicThreadPool
182 // core-size, keepalive, maximum-size
184 Set<String> longAttribs = Sets.newHashSet("core-size",
186 for (String longAttrib : longAttribs) {
188 TypedAttribute attribute = (TypedAttribute) attributes
190 assertThat("Failed to check " + longAttrib,
192 is((Type) Types.typeForClass(Long.class)));
194 // check dependency on thread factory
195 QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
196 THREADS_REVISION_DATE, "threadfactory");
197 ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
198 .get(threadfactoryQName);
199 assertNotNull(threadFactorySIEntry);
200 boolean expectedMandatory = true;
201 TypedAttribute actualThreadFactory = (TypedAttribute) attributes
202 .get("threadfactory");
204 DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
205 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
206 .getUnknownSchemaNodes();
207 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
208 AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
209 mockedDataSchemaNode, threadFactorySIEntry,
210 expectedMandatory, "threadfactory description");
211 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
214 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
215 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
216 assertThat(dynamicThreadPool.getNullableDescription(),
217 is("threadpool-dynamic description"));
218 assertThat(dynamicThreadPool.getYangModuleName(),
219 is("config-threads-java"));
220 assertThat(dynamicThreadPool.getYangModuleLocalname(),
221 is(THREADPOOL_DYNAMIC_MXB_NAME));
223 // check root runtime bean
224 Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
226 assertThat(runtimeBeans.size(), is(1));
227 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
228 THREADPOOL_DYNAMIC_MXB_NAME);
229 assertThat(rootRB.isRoot(), is(true));
230 assertThat(rootRB.getAttributes().size(), is(1));
231 JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
233 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
234 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
235 assertThat(attribute.getType().getFullyQualifiedName(),
236 is(Long.class.getName()));
238 {// check threadfactory-naming
239 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
240 .get(THREADFACTORY_NAMING_MXB_NAME);
241 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
243 assertThat(runtimeBeans.size(), is(4));
245 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
247 assertNotNull(threadRB);
248 assertFalse(threadRB.isRoot());
249 assertEquals("name", threadRB.getKeyYangName().get());
250 assertEquals("Name", threadRB.getKeyJavaName().get());
251 assertThat(threadRB.getAttributes().size(), is(1));
252 AttributeIfc threadNameAttr = threadRB.getAttributes()
254 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
255 assertTrue(threadNameAttr instanceof JavaAttribute);
256 assertThat(((JavaAttribute) threadNameAttr).getType()
257 .getFullyQualifiedName(), is(String.class.getName()));
258 assertThat(threadRB.getRpcs().size(), is(2));
261 RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
263 assertNotNull(streamRB);
264 assertFalse(streamRB.getKeyYangName().isPresent());
265 assertFalse(streamRB.getKeyJavaName().isPresent());
266 Map<String, AttributeIfc> attributeMap = streamRB
267 .getYangPropertiesToTypesMap();
268 assertEquals(4, attributeMap.size());
270 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
271 assertNotNull(toAttr);
272 assertThat(toAttr.getAttributeYangName(), is("peer"));
273 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
274 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
275 assertThat(toAttr.getOpenType(), isA(CompositeType.class));
276 Set<String> propsExpected = new HashSet<String>(2);
277 propsExpected.add("port");
278 propsExpected.add("core-size");
279 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
281 propsExpected = new HashSet<String>(2);
282 propsExpected.add("Port");
283 propsExpected.add("CoreSize");
285 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
287 propsExpected = new HashSet<String>(2);
288 propsExpected.add("port");
289 propsExpected.add("coreSize");
290 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
293 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
295 assertNotNull(timestampAttr);
297 JavaAttribute stateAttr = (JavaAttribute) attributeMap
299 assertNotNull(stateAttr);
301 ListAttribute innerStream = (ListAttribute) attributeMap
302 .get("inner-stream-list");
303 assertNotNull(innerStream);
304 assertThat(innerStream.getAttributeYangName(),
305 is("inner-stream-list"));
306 assertThat(innerStream.getLowerCaseCammelCase(),
307 is("innerStreamList"));
308 assertThat(innerStream.getUpperCaseCammelCase(),
309 is("InnerStreamList"));
310 assertThat(innerStream.getOpenType(), isA(ArrayType.class));
315 { // test multiple dependencies
316 ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
317 Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
318 assertEquals(1, attributes.size());
319 AttributeIfc threadpoolsAttr = attributes.get("threadpools");
320 assertNotNull(threadpoolsAttr);
321 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
322 ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;