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 java.util.HashMap;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
18 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
21 import org.opendaylight.yangtools.binding.generator.util.Types;
22 import org.opendaylight.yangtools.sal.binding.model.api.Type;
23 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
29 import javax.management.openmbean.ArrayType;
30 import javax.management.openmbean.CompositeType;
31 import javax.management.openmbean.SimpleType;
33 import java.net.URISyntaxException;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.Date;
39 import java.util.HashSet;
42 import java.util.regex.Matcher;
44 import static org.hamcrest.CoreMatchers.is;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertNotNull;
48 import static org.junit.Assert.assertNull;
49 import static org.junit.Assert.assertThat;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.doReturn;
52 import static org.mockito.Mockito.mock;
54 public class ModuleMXBeanEntryTest extends AbstractYangTest {
55 public static final String EVENTBUS_MXB_NAME = "eventbus";
56 public static final String ASYNC_EVENTBUS_MXB_NAME = "async-eventbus";
57 public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
58 public static final String THREADPOOL_DYNAMIC_MXB_NAME = "threadpool-dynamic";
59 public static final String THREADPOOL_REGISTRY_IMPL_NAME = "threadpool-registry-impl";
61 public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
63 public static final String PACKAGE_NAME = "pack2";
65 protected static final URI THREADS_NAMESPACE;
66 protected static final Date THREADS_REVISION_DATE;
70 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
72 } catch (URISyntaxException e) {
75 SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
77 THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
78 } catch (ParseException e) {
83 protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
85 protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
86 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
87 .create(threadsJavaModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl
88 (context)), PACKAGE_NAME);
89 assertNotNull(namesToMBEs);
90 Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
91 ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
92 THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
93 assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
99 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
100 modulesToSIEs = ServiceInterfaceEntry.create(threadsModule,
101 "packages.sis",identitiesToSIs);
105 public void test_jmxImplModule() {
106 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
107 Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
108 .create(threadsModule, PACKAGE_NAME,identitiesToSIs);
109 modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
110 PACKAGE_NAME,identitiesToSIs));
111 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
112 .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
114 Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
117 assertCorrectAttributesSize(namesToMBEs, attributes);
120 DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
121 .get("thread-factory");
122 assertNotNull(threadFactoryAttribute);
123 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
124 assertThat(threadFactoryAttribute.getDependency().getSie()
125 .getTypeName(), is("ThreadFactoryServiceInterface"));
126 assertThat(threadFactoryAttribute.getAttributeYangName(),
127 is("thread-factory"));
128 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
129 is("threadFactory"));
130 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
131 is("ThreadFactory"));
132 assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
133 assertNull(threadFactoryAttribute.getNullableDefault());
134 assertNull(threadFactoryAttribute.getNullableDescription());
135 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
138 private void assertCorrectAttributesSize(Map<String, ModuleMXBeanEntry> namesToMBEs, Map<String, AttributeIfc> attributes) {
139 assertEquals(14, attributes.size());
140 assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
141 assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
143 assertEquals(4, namesToMBEs.get("impl").getAttributes().size());
144 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().size());
145 assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().iterator().next().getAttributes().size());
148 protected RuntimeBeanEntry findFirstByYangName(
149 Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
150 for (RuntimeBeanEntry rb : runtimeBeans) {
151 if (yangName.equals(rb.getYangName()))
154 throw new IllegalArgumentException("Yang name not found:" + yangName
155 + " in " + runtimeBeans);
159 public void testGetWhenConditionMatcher() {
160 assertMatches("config",
161 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
163 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
164 assertMatches("config",
165 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
168 private void assertMatches(String prefix, String input) {
169 RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
170 doReturn(input).when(whenConstraint).toString();
171 Matcher output = ModuleMXBeanEntryBuilder.getWhenConditionMatcher(prefix,
173 assertTrue(output.matches());
174 assertEquals("threadpool-dynamic", output.group(1));
178 public void testThreadsJava() {
179 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
181 { // check threadpool-dynamic
182 ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
183 .get(THREADPOOL_DYNAMIC_MXB_NAME);
184 Map<String, AttributeIfc> attributes = dynamicThreadPool
186 // core-size, keepalive, maximum-size
188 Set<String> longAttribs = Sets.newHashSet("core-size",
190 for (String longAttrib : longAttribs) {
192 TypedAttribute attribute = (TypedAttribute) attributes
194 assertThat("Failed to check " + longAttrib,
196 is((Type) Types.typeForClass(Long.class)));
198 // check dependency on thread factory
199 QName threadfactoryQName = new QName(THREADS_NAMESPACE,
200 THREADS_REVISION_DATE, "threadfactory");
201 ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
202 .get(threadfactoryQName);
203 assertNotNull(threadFactorySIEntry);
204 boolean expectedMandatory = true;
205 TypedAttribute actualThreadFactory = (TypedAttribute) attributes
206 .get("threadfactory");
208 DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
209 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
210 .getUnknownSchemaNodes();
211 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
212 AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
213 mockedDataSchemaNode, threadFactorySIEntry,
214 expectedMandatory, "threadfactory description");
215 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
218 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
219 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
220 assertThat(dynamicThreadPool.getNullableDescription(),
221 is("threadpool-dynamic description"));
222 assertThat(dynamicThreadPool.getYangModuleName(),
223 is("config-threads-java"));
224 assertThat(dynamicThreadPool.getYangModuleLocalname(),
225 is(THREADPOOL_DYNAMIC_MXB_NAME));
227 // check root runtime bean
228 Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
230 assertThat(runtimeBeans.size(), is(1));
231 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
232 THREADPOOL_DYNAMIC_MXB_NAME);
233 assertThat(rootRB.isRoot(), is(true));
234 assertThat(rootRB.getAttributes().size(), is(1));
235 JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
237 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
238 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
239 assertThat(attribute.getType().getFullyQualifiedName(),
240 is(Long.class.getName()));
242 {// check threadfactory-naming
243 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
244 .get(THREADFACTORY_NAMING_MXB_NAME);
245 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
247 assertThat(runtimeBeans.size(), is(4));
249 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
251 assertNotNull(threadRB);
252 assertFalse(threadRB.isRoot());
253 assertEquals("name", threadRB.getKeyYangName().get());
254 assertEquals("Name", threadRB.getKeyJavaName().get());
255 assertThat(threadRB.getAttributes().size(), is(1));
256 AttributeIfc threadNameAttr = threadRB.getAttributes()
258 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
259 assertTrue(threadNameAttr instanceof JavaAttribute);
260 assertThat(((JavaAttribute) threadNameAttr).getType()
261 .getFullyQualifiedName(), is(String.class.getName()));
262 assertThat(threadRB.getRpcs().size(), is(2));
265 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
267 assertNotNull(streamRB);
268 assertFalse(streamRB.getKeyYangName().isPresent());
269 assertFalse(streamRB.getKeyJavaName().isPresent());
270 Map<String, AttributeIfc> attributeMap = streamRB
271 .getYangPropertiesToTypesMap();
272 assertEquals(4, attributeMap.size());
274 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
275 assertNotNull(toAttr);
276 assertThat(toAttr.getAttributeYangName(), is("peer"));
277 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
278 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
279 assertThat(toAttr.getOpenType(), is(CompositeType.class));
280 Set<String> propsExpected = new HashSet<String>(2);
281 propsExpected.add("port");
282 propsExpected.add("core-size");
283 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
285 propsExpected = new HashSet<String>(2);
286 propsExpected.add("Port");
287 propsExpected.add("CoreSize");
289 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
291 propsExpected = new HashSet<String>(2);
292 propsExpected.add("port");
293 propsExpected.add("coreSize");
294 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
297 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
299 assertNotNull(timestampAttr);
301 JavaAttribute stateAttr = (JavaAttribute) attributeMap
303 assertNotNull(stateAttr);
305 ListAttribute innerStream = (ListAttribute) attributeMap
306 .get("inner-stream-list");
307 assertNotNull(innerStream);
308 assertThat(innerStream.getAttributeYangName(),
309 is("inner-stream-list"));
310 assertThat(innerStream.getLowerCaseCammelCase(),
311 is("innerStreamList"));
312 assertThat(innerStream.getUpperCaseCammelCase(),
313 is("InnerStreamList"));
314 assertThat(innerStream.getOpenType(), is(ArrayType.class));
319 { // test multiple dependencies
320 ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
321 Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
322 assertEquals(1, attributes.size());
323 AttributeIfc threadpoolsAttr = attributes.get("threadpools");
324 assertNotNull(threadpoolsAttr);
325 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
326 ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;