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 com.google.common.collect.Sets;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
18 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
20 import org.opendaylight.yangtools.binding.generator.util.Types;
21 import org.opendaylight.yangtools.sal.binding.model.api.Type;
22 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import javax.management.openmbean.ArrayType;
28 import javax.management.openmbean.CompositeType;
29 import javax.management.openmbean.SimpleType;
31 import java.net.URISyntaxException;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Date;
37 import java.util.HashSet;
40 import java.util.regex.Matcher;
42 import static org.hamcrest.CoreMatchers.is;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertFalse;
45 import static org.junit.Assert.assertNotNull;
46 import static org.junit.Assert.assertNull;
47 import static org.junit.Assert.assertThat;
48 import static org.junit.Assert.assertTrue;
49 import static org.mockito.Mockito.doReturn;
50 import static org.mockito.Mockito.mock;
52 public class ModuleMXBeanEntryTest extends AbstractYangTest {
53 public static final String EVENTBUS_MXB_NAME = "eventbus";
54 public static final String ASYNC_EVENTBUS_MXB_NAME = "async-eventbus";
55 public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
56 public static final String THREADPOOL_DYNAMIC_MXB_NAME = "threadpool-dynamic";
57 public static final String THREADPOOL_REGISTRY_IMPL_NAME = "threadpool-registry-impl";
59 public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
61 public static final String PACKAGE_NAME = "pack2";
63 protected static final URI THREADS_NAMESPACE;
64 protected static final Date THREADS_REVISION_DATE;
68 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
70 } catch (URISyntaxException e) {
73 SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
75 THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
76 } catch (ParseException e) {
81 protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
83 protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
84 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
85 .create(threadsJavaModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl
86 (context)), PACKAGE_NAME);
87 assertNotNull(namesToMBEs);
88 Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
89 ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
90 THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
91 assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
97 modulesToSIEs = ServiceInterfaceEntry.create(threadsModule,
102 public void test_jmxImplModule() {
103 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
104 .create(threadsModule, PACKAGE_NAME);
105 modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
107 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
108 .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
110 Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
113 DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
114 .get("thread-factory");
115 assertNotNull(threadFactoryAttribute);
116 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
117 assertThat(threadFactoryAttribute.getDependency().getSie()
118 .getTypeName(), is("ThreadFactoryServiceInterface"));
119 assertThat(threadFactoryAttribute.getAttributeYangName(),
120 is("thread-factory"));
121 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
122 is("threadFactory"));
123 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
124 is("ThreadFactory"));
125 assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
126 assertNull(threadFactoryAttribute.getNullableDefault());
127 assertNull(threadFactoryAttribute.getNullableDescription());
128 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
131 protected RuntimeBeanEntry findFirstByYangName(
132 Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
133 for (RuntimeBeanEntry rb : runtimeBeans) {
134 if (yangName.equals(rb.getYangName()))
137 throw new IllegalArgumentException("Yang name not found:" + yangName
138 + " in " + runtimeBeans);
142 public void testGetWhenConditionMatcher() {
143 assertMatches("config",
144 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
146 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
147 assertMatches("config",
148 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
151 private void assertMatches(String prefix, String input) {
152 RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
153 doReturn(input).when(whenConstraint).toString();
154 Matcher output = ModuleMXBeanEntry.getWhenConditionMatcher(prefix,
156 assertTrue(output.matches());
157 assertEquals("threadpool-dynamic", output.group(1));
161 public void testThreadsJava() {
162 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
164 { // check threadpool-dynamic
165 ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
166 .get(THREADPOOL_DYNAMIC_MXB_NAME);
167 Map<String, AttributeIfc> attributes = dynamicThreadPool
169 // core-size, keepalive, maximum-size
171 Set<String> longAttribs = Sets.newHashSet("core-size",
173 for (String longAttrib : longAttribs) {
175 TypedAttribute attribute = (TypedAttribute) attributes
177 assertThat("Failed to check " + longAttrib,
179 is((Type) Types.typeForClass(Long.class)));
181 // check dependency on thread factory
182 QName threadfactoryQName = new QName(THREADS_NAMESPACE,
183 THREADS_REVISION_DATE, "threadfactory");
184 ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
185 .get(threadfactoryQName);
186 assertNotNull(threadFactorySIEntry);
187 boolean expectedMandatory = true;
188 TypedAttribute actualThreadFactory = (TypedAttribute) attributes
189 .get("threadfactory");
191 DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
192 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
193 .getUnknownSchemaNodes();
194 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
195 AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
196 mockedDataSchemaNode, threadFactorySIEntry,
197 expectedMandatory, "threadfactory description");
198 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
201 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
202 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
203 assertThat(dynamicThreadPool.getNullableDescription(),
204 is("threadpool-dynamic description"));
205 assertThat(dynamicThreadPool.getYangModuleName(),
206 is("config-threads-java"));
207 assertThat(dynamicThreadPool.getYangModuleLocalname(),
208 is(THREADPOOL_DYNAMIC_MXB_NAME));
210 // check root runtime bean
211 Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
213 assertThat(runtimeBeans.size(), is(1));
214 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
215 THREADPOOL_DYNAMIC_MXB_NAME);
216 assertThat(rootRB.isRoot(), is(true));
217 assertThat(rootRB.getAttributes().size(), is(1));
218 JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
220 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
221 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
222 assertThat(attribute.getType().getFullyQualifiedName(),
223 is(Long.class.getName()));
225 {// check threadfactory-naming
226 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
227 .get(THREADFACTORY_NAMING_MXB_NAME);
228 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
230 assertThat(runtimeBeans.size(), is(4));
232 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
234 assertNotNull(threadRB);
235 assertFalse(threadRB.isRoot());
236 assertEquals("name", threadRB.getKeyYangName().get());
237 assertEquals("Name", threadRB.getKeyJavaName().get());
238 assertThat(threadRB.getAttributes().size(), is(1));
239 AttributeIfc threadNameAttr = threadRB.getAttributes()
241 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
242 assertTrue(threadNameAttr instanceof JavaAttribute);
243 assertThat(((JavaAttribute) threadNameAttr).getType()
244 .getFullyQualifiedName(), is(String.class.getName()));
245 assertThat(threadRB.getRpcs().size(), is(2));
248 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
250 assertNotNull(streamRB);
251 assertFalse(streamRB.getKeyYangName().isPresent());
252 assertFalse(streamRB.getKeyJavaName().isPresent());
253 Map<String, AttributeIfc> attributeMap = streamRB
254 .getYangPropertiesToTypesMap();
255 assertEquals(4, attributeMap.size());
257 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
258 assertNotNull(toAttr);
259 assertThat(toAttr.getAttributeYangName(), is("peer"));
260 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
261 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
262 assertThat(toAttr.getOpenType(), is(CompositeType.class));
263 Set<String> propsExpected = new HashSet<String>(2);
264 propsExpected.add("port");
265 propsExpected.add("core-size");
266 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
268 propsExpected = new HashSet<String>(2);
269 propsExpected.add("Port");
270 propsExpected.add("CoreSize");
272 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
274 propsExpected = new HashSet<String>(2);
275 propsExpected.add("port");
276 propsExpected.add("coreSize");
277 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
280 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
282 assertNotNull(timestampAttr);
284 JavaAttribute stateAttr = (JavaAttribute) attributeMap
286 assertNotNull(stateAttr);
288 ListAttribute innerStream = (ListAttribute) attributeMap
289 .get("inner-stream-list");
290 assertNotNull(innerStream);
291 assertThat(innerStream.getAttributeYangName(),
292 is("inner-stream-list"));
293 assertThat(innerStream.getLowerCaseCammelCase(),
294 is("innerStreamList"));
295 assertThat(innerStream.getUpperCaseCammelCase(),
296 is("InnerStreamList"));
297 assertThat(innerStream.getOpenType(), is(ArrayType.class));
302 { // test multiple dependencies
303 ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
304 Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
305 assertEquals(1, attributes.size());
306 AttributeIfc threadpoolsAttr = attributes.get("threadpools");
307 assertNotNull(threadpoolsAttr);
308 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
309 ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;