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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertThat;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18 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(), is(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);
144 public void testGetWhenConditionMatcher() {
145 assertMatches("config",
146 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
148 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
149 assertMatches("config",
150 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
153 private void assertMatches(final String prefix, final String input) {
154 RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
155 doReturn(input).when(whenConstraint).toString();
156 Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
158 assertTrue(output.matches());
159 assertEquals("threadpool-dynamic", output.group(1));
163 public void testThreadsJava() {
164 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
166 { // check threadpool-dynamic
167 ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
168 .get(THREADPOOL_DYNAMIC_MXB_NAME);
169 Map<String, AttributeIfc> attributes = dynamicThreadPool
171 // core-size, keepalive, maximum-size
173 Set<String> longAttribs = Sets.newHashSet("core-size",
175 for (String longAttrib : longAttribs) {
177 TypedAttribute attribute = (TypedAttribute) attributes
179 assertThat("Failed to check " + longAttrib,
181 is((Type) Types.typeForClass(Long.class)));
183 // check dependency on thread factory
184 QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
185 THREADS_REVISION_DATE, "threadfactory");
186 ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
187 .get(threadfactoryQName);
188 assertNotNull(threadFactorySIEntry);
189 boolean expectedMandatory = true;
190 TypedAttribute actualThreadFactory = (TypedAttribute) attributes
191 .get("threadfactory");
193 DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
194 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
195 .getUnknownSchemaNodes();
196 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
197 AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
198 mockedDataSchemaNode, threadFactorySIEntry,
199 expectedMandatory, "threadfactory description");
200 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
203 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
204 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
205 assertThat(dynamicThreadPool.getNullableDescription(),
206 is("threadpool-dynamic description"));
207 assertThat(dynamicThreadPool.getYangModuleName(),
208 is("config-threads-java"));
209 assertThat(dynamicThreadPool.getYangModuleLocalname(),
210 is(THREADPOOL_DYNAMIC_MXB_NAME));
212 // check root runtime bean
213 Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
215 assertThat(runtimeBeans.size(), is(1));
216 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
217 THREADPOOL_DYNAMIC_MXB_NAME);
218 assertThat(rootRB.isRoot(), is(true));
219 assertThat(rootRB.getAttributes().size(), is(1));
220 JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
222 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
223 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
224 assertThat(attribute.getType().getFullyQualifiedName(),
225 is(Long.class.getName()));
227 {// check threadfactory-naming
228 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
229 .get(THREADFACTORY_NAMING_MXB_NAME);
230 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
232 assertThat(runtimeBeans.size(), is(4));
234 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
236 assertNotNull(threadRB);
237 assertFalse(threadRB.isRoot());
238 assertEquals("name", threadRB.getKeyYangName().get());
239 assertEquals("Name", threadRB.getKeyJavaName().get());
240 assertThat(threadRB.getAttributes().size(), is(1));
241 AttributeIfc threadNameAttr = threadRB.getAttributes()
243 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
244 assertTrue(threadNameAttr instanceof JavaAttribute);
245 assertThat(((JavaAttribute) threadNameAttr).getType()
246 .getFullyQualifiedName(), is(String.class.getName()));
247 assertThat(threadRB.getRpcs().size(), is(2));
250 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
252 assertNotNull(streamRB);
253 assertFalse(streamRB.getKeyYangName().isPresent());
254 assertFalse(streamRB.getKeyJavaName().isPresent());
255 Map<String, AttributeIfc> attributeMap = streamRB
256 .getYangPropertiesToTypesMap();
257 assertEquals(4, attributeMap.size());
259 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
260 assertNotNull(toAttr);
261 assertThat(toAttr.getAttributeYangName(), is("peer"));
262 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
263 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
264 assertThat(toAttr.getOpenType(), is(CompositeType.class));
265 Set<String> propsExpected = new HashSet<String>(2);
266 propsExpected.add("port");
267 propsExpected.add("core-size");
268 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
270 propsExpected = new HashSet<String>(2);
271 propsExpected.add("Port");
272 propsExpected.add("CoreSize");
274 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
276 propsExpected = new HashSet<String>(2);
277 propsExpected.add("port");
278 propsExpected.add("coreSize");
279 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
282 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
284 assertNotNull(timestampAttr);
286 JavaAttribute stateAttr = (JavaAttribute) attributeMap
288 assertNotNull(stateAttr);
290 ListAttribute innerStream = (ListAttribute) attributeMap
291 .get("inner-stream-list");
292 assertNotNull(innerStream);
293 assertThat(innerStream.getAttributeYangName(),
294 is("inner-stream-list"));
295 assertThat(innerStream.getLowerCaseCammelCase(),
296 is("innerStreamList"));
297 assertThat(innerStream.getUpperCaseCammelCase(),
298 is("InnerStreamList"));
299 assertThat(innerStream.getOpenType(), is(ArrayType.class));
304 { // test multiple dependencies
305 ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
306 Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
307 assertEquals(1, attributes.size());
308 AttributeIfc threadpoolsAttr = attributes.get("threadpools");
309 assertNotNull(threadpoolsAttr);
310 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
311 ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;