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

©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.