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 DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
118 .get("thread-factory");
119 assertNotNull(threadFactoryAttribute);
120 assertFalse(threadFactoryAttribute.getDependency().isMandatory());
121 assertThat(threadFactoryAttribute.getDependency().getSie()
122 .getTypeName(), is("ThreadFactoryServiceInterface"));
123 assertThat(threadFactoryAttribute.getAttributeYangName(),
124 is("thread-factory"));
125 assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
126 is("threadFactory"));
127 assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
128 is("ThreadFactory"));
129 assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
130 assertNull(threadFactoryAttribute.getNullableDefault());
131 assertNull(threadFactoryAttribute.getNullableDescription());
132 assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
135 protected RuntimeBeanEntry findFirstByYangName(
136 Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
137 for (RuntimeBeanEntry rb : runtimeBeans) {
138 if (yangName.equals(rb.getYangName()))
141 throw new IllegalArgumentException("Yang name not found:" + yangName
142 + " in " + runtimeBeans);
146 public void testGetWhenConditionMatcher() {
147 assertMatches("config",
148 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
150 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
151 assertMatches("config",
152 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
155 private void assertMatches(String prefix, String input) {
156 RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
157 doReturn(input).when(whenConstraint).toString();
158 Matcher output = ModuleMXBeanEntry.getWhenConditionMatcher(prefix,
160 assertTrue(output.matches());
161 assertEquals("threadpool-dynamic", output.group(1));
165 public void testThreadsJava() {
166 Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
168 { // check threadpool-dynamic
169 ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
170 .get(THREADPOOL_DYNAMIC_MXB_NAME);
171 Map<String, AttributeIfc> attributes = dynamicThreadPool
173 // core-size, keepalive, maximum-size
175 Set<String> longAttribs = Sets.newHashSet("core-size",
177 for (String longAttrib : longAttribs) {
179 TypedAttribute attribute = (TypedAttribute) attributes
181 assertThat("Failed to check " + longAttrib,
183 is((Type) Types.typeForClass(Long.class)));
185 // check dependency on thread factory
186 QName threadfactoryQName = new QName(THREADS_NAMESPACE,
187 THREADS_REVISION_DATE, "threadfactory");
188 ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
189 .get(threadfactoryQName);
190 assertNotNull(threadFactorySIEntry);
191 boolean expectedMandatory = true;
192 TypedAttribute actualThreadFactory = (TypedAttribute) attributes
193 .get("threadfactory");
195 DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
196 doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
197 .getUnknownSchemaNodes();
198 doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
199 AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
200 mockedDataSchemaNode, threadFactorySIEntry,
201 expectedMandatory, "threadfactory description");
202 assertThat(actualThreadFactory, is(expectedDependencyAttribute));
205 .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
206 is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
207 assertThat(dynamicThreadPool.getNullableDescription(),
208 is("threadpool-dynamic description"));
209 assertThat(dynamicThreadPool.getYangModuleName(),
210 is("config-threads-java"));
211 assertThat(dynamicThreadPool.getYangModuleLocalname(),
212 is(THREADPOOL_DYNAMIC_MXB_NAME));
214 // check root runtime bean
215 Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
217 assertThat(runtimeBeans.size(), is(1));
218 RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
219 THREADPOOL_DYNAMIC_MXB_NAME);
220 assertThat(rootRB.isRoot(), is(true));
221 assertThat(rootRB.getAttributes().size(), is(1));
222 JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
224 assertThat(attribute.getAttributeYangName(), is("created-sessions"));
225 assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
226 assertThat(attribute.getType().getFullyQualifiedName(),
227 is(Long.class.getName()));
229 {// check threadfactory-naming
230 ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
231 .get(THREADFACTORY_NAMING_MXB_NAME);
232 Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
234 assertThat(runtimeBeans.size(), is(4));
236 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
238 assertNotNull(threadRB);
239 assertFalse(threadRB.isRoot());
240 assertEquals("name", threadRB.getKeyYangName().get());
241 assertEquals("Name", threadRB.getKeyJavaName().get());
242 assertThat(threadRB.getAttributes().size(), is(1));
243 AttributeIfc threadNameAttr = threadRB.getAttributes()
245 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
246 assertTrue(threadNameAttr instanceof JavaAttribute);
247 assertThat(((JavaAttribute) threadNameAttr).getType()
248 .getFullyQualifiedName(), is(String.class.getName()));
249 assertThat(threadRB.getRpcs().size(), is(2));
252 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
254 assertNotNull(streamRB);
255 assertFalse(streamRB.getKeyYangName().isPresent());
256 assertFalse(streamRB.getKeyJavaName().isPresent());
257 Map<String, AttributeIfc> attributeMap = streamRB
258 .getYangPropertiesToTypesMap();
259 assertEquals(4, attributeMap.size());
261 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
262 assertNotNull(toAttr);
263 assertThat(toAttr.getAttributeYangName(), is("peer"));
264 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
265 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
266 assertThat(toAttr.getOpenType(), is(CompositeType.class));
267 Set<String> propsExpected = new HashSet<String>(2);
268 propsExpected.add("port");
269 propsExpected.add("core-size");
270 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
272 propsExpected = new HashSet<String>(2);
273 propsExpected.add("Port");
274 propsExpected.add("CoreSize");
276 toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
278 propsExpected = new HashSet<String>(2);
279 propsExpected.add("port");
280 propsExpected.add("coreSize");
281 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
284 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
286 assertNotNull(timestampAttr);
288 JavaAttribute stateAttr = (JavaAttribute) attributeMap
290 assertNotNull(stateAttr);
292 ListAttribute innerStream = (ListAttribute) attributeMap
293 .get("inner-stream-list");
294 assertNotNull(innerStream);
295 assertThat(innerStream.getAttributeYangName(),
296 is("inner-stream-list"));
297 assertThat(innerStream.getLowerCaseCammelCase(),
298 is("innerStreamList"));
299 assertThat(innerStream.getUpperCaseCammelCase(),
300 is("InnerStreamList"));
301 assertThat(innerStream.getOpenType(), is(ArrayType.class));
306 { // test multiple dependencies
307 ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
308 Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
309 assertEquals(1, attributes.size());
310 AttributeIfc threadpoolsAttr = attributes.get("threadpools");
311 assertNotNull(threadpoolsAttr);
312 assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
313 ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;