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.text.ParseException;
25 import java.text.SimpleDateFormat;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import java.util.regex.Matcher;
34 import javax.management.openmbean.ArrayType;
35 import javax.management.openmbean.CompositeType;
36 import javax.management.openmbean.SimpleType;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
40 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
41 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
42 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
43 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
44 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
45 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
46 import org.opendaylight.mdsal.binding.model.api.Type;
47 import org.opendaylight.mdsal.binding.model.util.Types;
48 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
54 public class ModuleMXBeanEntryTest extends AbstractYangTest {
56 public static final String PACKAGE_NAME = "pack2";
58 protected static final URI THREADS_NAMESPACE;
59 protected static final Date THREADS_REVISION_DATE;
63 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
65 } catch (final URISyntaxException e) {
68 final SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
70 THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
71 } catch (final ParseException e) {
76 protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
81 this.modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
85 protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
86 return loadThreadsJava(this.modulesToSIEs, PACKAGE_NAME);
90 public void test_jmxImplModule() {
91 final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
92 final Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
93 .create(this.threadsModule, PACKAGE_NAME,identitiesToSIs);
94 modulesToSIEs.putAll(ServiceInterfaceEntry.create(this.jmxModule,
95 PACKAGE_NAME,identitiesToSIs));
96 final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
97 .create(this.jmxImplModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl(this.context))
99 final Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
102 assertCorrectAttributesSize(namesToMBEs, attributes);
105 final DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
106 .get("thread-factory");
107 assertNotNull(threadFactoryAttribute);
108 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
109 assertThat(threadFactoryAttribute.getDependency().getSie()
110 .getTypeName(), is("ThreadFactoryServiceInterface"));
111 assertThat(threadFactoryAttribute.getAttributeYangName(),
112 is("thread-factory"));
113 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
114 is("threadFactory"));
115 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
116 is("ThreadFactory"));
117 assertThat(threadFactoryAttribute.getOpenType(), isA(SimpleType.class));
118 assertNull(threadFactoryAttribute.getNullableDefault());
119 assertNull(threadFactoryAttribute.getNullableDescription());
120 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
123 private void assertCorrectAttributesSize(final Map<String, ModuleMXBeanEntry> namesToMBEs, final Map<String, AttributeIfc> attributes) {
124 assertEquals(14, attributes.size());
125 assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
126 assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
128 assertEquals(4, namesToMBEs.get("impl").getAttributes().size());
129 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().size());
130 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().iterator().next().getAttributes().size());
133 protected RuntimeBeanEntry findFirstByYangName(
134 final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
135 for (final RuntimeBeanEntry rb : runtimeBeans) {
136 if (yangName.equals(rb.getYangName())) {
140 throw new IllegalArgumentException("Yang name not found:" + yangName
141 + " in " + runtimeBeans);
144 protected RuntimeBeanEntry findFirstByNamePrefix(final Collection<RuntimeBeanEntry> runtimeBeans, final String namePrefix) {
145 for (final RuntimeBeanEntry rb : runtimeBeans) {
146 if (namePrefix.equals(rb.getJavaNamePrefix())) {
151 throw new IllegalArgumentException("Name prefix not found:" + namePrefix
152 + " in " + runtimeBeans);
156 public void testGetWhenConditionMatcher() {
157 assertMatches("config",
158 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
160 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
161 assertMatches("config",
162 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
165 private void assertMatches(final String prefix, final String input) {
166 final RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
167 doReturn(input).when(whenConstraint).toString();
168 final Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
170 assertTrue(output.matches());
171 assertEquals("threadpool-dynamic", output.group(1));
175 public void testThreadsJava() {
176 final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
178 { // check threadpool-dynamic
179 final ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
180 .get(THREADPOOL_DYNAMIC_MXB_NAME);
181 final Map<String, AttributeIfc> attributes = dynamicThreadPool
183 // core-size, keepalive, maximum-size
185 final Set<String> longAttribs = Sets.newHashSet("core-size",
187 for (final String longAttrib : longAttribs) {
189 final TypedAttribute attribute = (TypedAttribute) attributes
191 assertThat("Failed to check " + longAttrib,
193 is((Type) Types.typeForClass(Long.class)));
195 // check dependency on thread factory
196 final QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
197 THREADS_REVISION_DATE, "threadfactory");
198 final ServiceInterfaceEntry threadFactorySIEntry = this.modulesToSIEs
199 .get(threadfactoryQName);
200 assertNotNull(threadFactorySIEntry);
201 final boolean expectedMandatory = true;
202 final TypedAttribute actualThreadFactory = (TypedAttribute) attributes
203 .get("threadfactory");
205 final DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
206 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
207 .getUnknownSchemaNodes();
208 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
209 final AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
210 mockedDataSchemaNode, threadFactorySIEntry,
211 expectedMandatory, "threadfactory description");
212 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
215 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
216 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
217 assertThat(dynamicThreadPool.getNullableDescription(),
218 is("threadpool-dynamic description"));
219 assertThat(dynamicThreadPool.getYangModuleName(),
220 is("config-threads-java"));
221 assertThat(dynamicThreadPool.getYangModuleLocalname(),
222 is(THREADPOOL_DYNAMIC_MXB_NAME));
224 // check root runtime bean
225 final Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
227 assertThat(runtimeBeans.size(), is(1));
228 final RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
229 THREADPOOL_DYNAMIC_MXB_NAME);
230 assertThat(rootRB.isRoot(), is(true));
231 assertThat(rootRB.getAttributes().size(), is(1));
232 final JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
234 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
235 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
236 assertThat(attribute.getType().getFullyQualifiedName(),
237 is(Long.class.getName()));
239 {// check threadfactory-naming
240 final ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
241 .get(THREADFACTORY_NAMING_MXB_NAME);
242 final Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
244 assertThat(runtimeBeans.size(), is(4));
246 final RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
248 assertNotNull(threadRB);
249 assertFalse(threadRB.isRoot());
250 assertEquals("name", threadRB.getKeyYangName().get());
251 assertEquals("Name", threadRB.getKeyJavaName().get());
252 assertThat(threadRB.getAttributes().size(), is(1));
253 final AttributeIfc threadNameAttr = threadRB.getAttributes()
255 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
256 assertTrue(threadNameAttr instanceof JavaAttribute);
257 assertThat(((JavaAttribute) threadNameAttr).getType()
258 .getFullyQualifiedName(), is(String.class.getName()));
259 assertThat(threadRB.getRpcs().size(), is(2));
262 final RuntimeBeanEntry streamRB = findFirstByNamePrefix(runtimeBeans,
264 assertNotNull(streamRB);
265 assertFalse(streamRB.getKeyYangName().isPresent());
266 assertFalse(streamRB.getKeyJavaName().isPresent());
267 final Map<String, AttributeIfc> attributeMap = streamRB
268 .getYangPropertiesToTypesMap();
269 assertEquals(4, attributeMap.size());
271 final TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
272 assertNotNull(toAttr);
273 assertThat(toAttr.getAttributeYangName(), is("peer"));
274 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
275 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
276 assertThat(toAttr.getOpenType(), isA(CompositeType.class));
277 Set<String> propsExpected = new HashSet<>(2);
278 propsExpected.add("port");
279 propsExpected.add("core-size");
280 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
282 propsExpected = new HashSet<>(2);
283 propsExpected.add("Port");
284 propsExpected.add("CoreSize");
286 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
288 propsExpected = new HashSet<>(2);
289 propsExpected.add("port");
290 propsExpected.add("coreSize");
291 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
294 final JavaAttribute timestampAttr = (JavaAttribute) attributeMap
296 assertNotNull(timestampAttr);
298 final JavaAttribute stateAttr = (JavaAttribute) attributeMap
300 assertNotNull(stateAttr);
302 final ListAttribute innerStream = (ListAttribute) attributeMap
303 .get("inner-stream-list");
304 assertNotNull(innerStream);
305 assertThat(innerStream.getAttributeYangName(),
306 is("inner-stream-list"));
307 assertThat(innerStream.getLowerCaseCammelCase(),
308 is("innerStreamList"));
309 assertThat(innerStream.getUpperCaseCammelCase(),
310 is("InnerStreamList"));
311 assertThat(innerStream.getOpenType(), isA(ArrayType.class));
316 { // test multiple dependencies
317 final ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
318 final Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
319 assertEquals(1, attributes.size());
320 final AttributeIfc threadpoolsAttr = attributes.get("threadpools");
321 assertNotNull(threadpoolsAttr);
322 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
323 final ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;