Merge "Changed maximumEntries to correct int rather than long"
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / ModuleMXBeanEntryTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator;
9
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;
26
27 import javax.management.openmbean.ArrayType;
28 import javax.management.openmbean.CompositeType;
29 import javax.management.openmbean.SimpleType;
30 import java.net.URI;
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;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.regex.Matcher;
41
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;
51
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";
58
59     public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
60
61     public static final String PACKAGE_NAME = "pack2";
62
63     protected static final URI THREADS_NAMESPACE;
64     protected static final Date THREADS_REVISION_DATE;
65
66     static {
67         try {
68             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
69                     + ":threads");
70         } catch (URISyntaxException e) {
71             throw new Error(e);
72         }
73         SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
74         try {
75             THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
76         } catch (ParseException e) {
77             throw new Error(e);
78         }
79     }
80
81     protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
82
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));
92         return namesToMBEs;
93     }
94
95     @Before
96     public void setUp() {
97         modulesToSIEs = ServiceInterfaceEntry.create(threadsModule,
98                 "packages.sis");
99     }
100
101     @Test
102     public void test_jmxImplModule() {
103         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
104                 .create(threadsModule, PACKAGE_NAME);
105         modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
106                 PACKAGE_NAME));
107         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
108                 .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
109                         , PACKAGE_NAME);
110         Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
111                 .getAttributes();
112         //
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"));
129     }
130
131     protected RuntimeBeanEntry findFirstByYangName(
132             Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
133         for (RuntimeBeanEntry rb : runtimeBeans) {
134             if (yangName.equals(rb.getYangName()))
135                 return rb;
136         }
137         throw new IllegalArgumentException("Yang name not found:" + yangName
138                 + " in " + runtimeBeans);
139     }
140
141     @Test
142     public void testGetWhenConditionMatcher() {
143         assertMatches("config",
144                 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
145         assertMatches("ns",
146                 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
147         assertMatches("config",
148                 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
149     }
150
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,
155                 whenConstraint);
156         assertTrue(output.matches());
157         assertEquals("threadpool-dynamic", output.group(1));
158     }
159
160     @Test
161     public void testThreadsJava() {
162         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
163
164         { // check threadpool-dynamic
165             ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
166                     .get(THREADPOOL_DYNAMIC_MXB_NAME);
167             Map<String, AttributeIfc> attributes = dynamicThreadPool
168                     .getAttributes();
169             // core-size, keepalive, maximum-size
170             // threadfactory
171             Set<String> longAttribs = Sets.newHashSet("core-size",
172                     "maximum-size");
173             for (String longAttrib : longAttribs) {
174
175                 TypedAttribute attribute = (TypedAttribute) attributes
176                         .get(longAttrib);
177                 assertThat("Failed to check " + longAttrib,
178                         attribute.getType(),
179                         is((Type) Types.typeForClass(Long.class)));
180             }
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");
190
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));
199             assertThat(
200                     dynamicThreadPool
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));
209
210             // check root runtime bean
211             Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
212                     .getRuntimeBeans();
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()
219                     .iterator().next();
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()));
224         }
225         {// check threadfactory-naming
226             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
227                     .get(THREADFACTORY_NAMING_MXB_NAME);
228             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
229                     .getRuntimeBeans();
230             assertThat(runtimeBeans.size(), is(4));
231             {
232                 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
233                         "thread");
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()
240                         .iterator().next();
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));
246             }
247             {
248                 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
249                         "stream");
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());
256
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(),
267                         is(propsExpected));
268                 propsExpected = new HashSet<String>(2);
269                 propsExpected.add("Port");
270                 propsExpected.add("CoreSize");
271                 assertThat(
272                         toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
273                         is(propsExpected));
274                 propsExpected = new HashSet<String>(2);
275                 propsExpected.add("port");
276                 propsExpected.add("coreSize");
277                 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
278                         is(propsExpected));
279
280                 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
281                         .get("timestamp");
282                 assertNotNull(timestampAttr);
283
284                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
285                         .get("state");
286                 assertNotNull(stateAttr);
287
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));
298
299             }
300
301         }
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;
310         }
311     }
312
313 }