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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.