Merge "Fix checkstyle warnings in yang-jmx-generator."
[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 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;
19
20 import com.google.common.collect.Sets;
21 import java.net.URI;
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;
30 import java.util.Map;
31 import java.util.Set;
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;
52
53 public class ModuleMXBeanEntryTest extends AbstractYangTest {
54
55     public static final String PACKAGE_NAME = "pack2";
56
57     protected static final URI THREADS_NAMESPACE;
58     protected static final Date THREADS_REVISION_DATE;
59
60     static {
61         try {
62             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
63                     + ":threads");
64         } catch (URISyntaxException e) {
65             throw new Error(e);
66         }
67         SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
68         try {
69             THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
70         } catch (ParseException e) {
71             throw new Error(e);
72         }
73     }
74
75     protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
76
77
78     @Before
79     public void setUp() {
80         modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
81     }
82
83
84     protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
85         return loadThreadsJava(modulesToSIEs, PACKAGE_NAME);
86     }
87
88     @Test
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))
97                 , PACKAGE_NAME);
98         Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
99                 .getAttributes();
100
101         assertCorrectAttributesSize(namesToMBEs, attributes);
102
103         //
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"));
120     }
121
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());
126
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());
130     }
131
132     protected RuntimeBeanEntry findFirstByYangName(
133             final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
134         for (RuntimeBeanEntry rb : runtimeBeans) {
135             if (yangName.equals(rb.getYangName())) {
136                 return rb;
137             }
138         }
139         throw new IllegalArgumentException("Yang name not found:" + yangName
140                 + " in " + runtimeBeans);
141     }
142
143     @Test
144     public void testGetWhenConditionMatcher() {
145         assertMatches("config",
146                 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
147         assertMatches("ns",
148                 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
149         assertMatches("config",
150                 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
151     }
152
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,
157                 whenConstraint);
158         assertTrue(output.matches());
159         assertEquals("threadpool-dynamic", output.group(1));
160     }
161
162     @Test
163     public void testThreadsJava() {
164         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
165
166         { // check threadpool-dynamic
167             ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
168                     .get(THREADPOOL_DYNAMIC_MXB_NAME);
169             Map<String, AttributeIfc> attributes = dynamicThreadPool
170                     .getAttributes();
171             // core-size, keepalive, maximum-size
172             // threadfactory
173             Set<String> longAttribs = Sets.newHashSet("core-size",
174                     "maximum-size");
175             for (String longAttrib : longAttribs) {
176
177                 TypedAttribute attribute = (TypedAttribute) attributes
178                         .get(longAttrib);
179                 assertThat("Failed to check " + longAttrib,
180                         attribute.getType(),
181                         is((Type) Types.typeForClass(Long.class)));
182             }
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");
192
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));
201             assertThat(
202                     dynamicThreadPool
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));
211
212             // check root runtime bean
213             Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
214                     .getRuntimeBeans();
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()
221                     .iterator().next();
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()));
226         }
227         {// check threadfactory-naming
228             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
229                     .get(THREADFACTORY_NAMING_MXB_NAME);
230             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
231                     .getRuntimeBeans();
232             assertThat(runtimeBeans.size(), is(4));
233             {
234                 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
235                         "thread");
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()
242                         .iterator().next();
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));
248             }
249             {
250                 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
251                         "stream");
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());
258
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(),
269                         is(propsExpected));
270                 propsExpected = new HashSet<String>(2);
271                 propsExpected.add("Port");
272                 propsExpected.add("CoreSize");
273                 assertThat(
274                         toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
275                         is(propsExpected));
276                 propsExpected = new HashSet<String>(2);
277                 propsExpected.add("port");
278                 propsExpected.add("coreSize");
279                 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
280                         is(propsExpected));
281
282                 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
283                         .get("timestamp");
284                 assertNotNull(timestampAttr);
285
286                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
287                         .get("state");
288                 assertNotNull(stateAttr);
289
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));
300
301             }
302
303         }
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;
312         }
313     }
314
315 }