Merge "Complete implementation of DataChangeListenerProxy"
[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
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
35 import javax.management.openmbean.ArrayType;
36 import javax.management.openmbean.CompositeType;
37 import javax.management.openmbean.SimpleType;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
42 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
43 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
44 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
45 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
46 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
47 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
48 import org.opendaylight.yangtools.binding.generator.util.Types;
49 import org.opendaylight.yangtools.sal.binding.model.api.Type;
50 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
55
56 public class ModuleMXBeanEntryTest extends AbstractYangTest {
57
58     public static final String PACKAGE_NAME = "pack2";
59
60     protected static final URI THREADS_NAMESPACE;
61     protected static final Date THREADS_REVISION_DATE;
62
63     static {
64         try {
65             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
66                     + ":threads");
67         } catch (URISyntaxException e) {
68             throw new Error(e);
69         }
70         SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
71         try {
72             THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
73         } catch (ParseException e) {
74             throw new Error(e);
75         }
76     }
77
78     protected Map<QName, ServiceInterfaceEntry> modulesToSIEs;
79
80
81     @Before
82     public void setUp() {
83         modulesToSIEs = loadThreadsServiceInterfaceEntries("packages.sis");
84     }
85
86
87     protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava() {
88         return loadThreadsJava(modulesToSIEs, PACKAGE_NAME);
89     }
90
91     @Test
92     public void test_jmxImplModule() {
93         Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
94         Map<QName, ServiceInterfaceEntry> modulesToSIEs = ServiceInterfaceEntry
95                 .create(threadsModule, PACKAGE_NAME,identitiesToSIs);
96         modulesToSIEs.putAll(ServiceInterfaceEntry.create(jmxModule,
97                 PACKAGE_NAME,identitiesToSIs));
98         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
99                 .create(jmxImplModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context))
100                 , PACKAGE_NAME);
101         Map<String, AttributeIfc> attributes = namesToMBEs.get("impl-netconf")
102                 .getAttributes();
103
104         assertCorrectAttributesSize(namesToMBEs, attributes);
105
106         //
107         DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
108                 .get("thread-factory");
109         assertNotNull(threadFactoryAttribute);
110         assertFalse(threadFactoryAttribute.getDependency().isMandatory());
111         assertThat(threadFactoryAttribute.getDependency().getSie()
112                 .getTypeName(), is("ThreadFactoryServiceInterface"));
113         assertThat(threadFactoryAttribute.getAttributeYangName(),
114                 is("thread-factory"));
115         assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
116                 is("threadFactory"));
117         assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
118                 is("ThreadFactory"));
119         assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
120         assertNull(threadFactoryAttribute.getNullableDefault());
121         assertNull(threadFactoryAttribute.getNullableDescription());
122         assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
123     }
124
125     private void assertCorrectAttributesSize(final Map<String, ModuleMXBeanEntry> namesToMBEs, final Map<String, AttributeIfc> attributes) {
126         assertEquals(14, attributes.size());
127         assertEquals(1, namesToMBEs.get("impl-netconf").getRuntimeBeans().size());
128         assertEquals(2, namesToMBEs.get("impl-netconf").getRuntimeBeans().iterator().next().getAttributes().size());
129
130         assertEquals(4, namesToMBEs.get("impl").getAttributes().size());
131         assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().size());
132         assertEquals(1, namesToMBEs.get("impl").getRuntimeBeans().iterator().next().getAttributes().size());
133     }
134
135     protected RuntimeBeanEntry findFirstByYangName(
136             final Collection<RuntimeBeanEntry> runtimeBeans, final String yangName) {
137         for (RuntimeBeanEntry rb : runtimeBeans) {
138             if (yangName.equals(rb.getYangName())) {
139                 return rb;
140             }
141         }
142         throw new IllegalArgumentException("Yang name not found:" + yangName
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         RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
158         doReturn(input).when(whenConstraint).toString();
159         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         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
168
169         { // check threadpool-dynamic
170             ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
171                     .get(THREADPOOL_DYNAMIC_MXB_NAME);
172             Map<String, AttributeIfc> attributes = dynamicThreadPool
173                     .getAttributes();
174             // core-size, keepalive, maximum-size
175             // threadfactory
176             Set<String> longAttribs = Sets.newHashSet("core-size",
177                     "maximum-size");
178             for (String longAttrib : longAttribs) {
179
180                 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             QName threadfactoryQName = QName.create(THREADS_NAMESPACE,
188                     THREADS_REVISION_DATE, "threadfactory");
189             ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
190                     .get(threadfactoryQName);
191             assertNotNull(threadFactorySIEntry);
192             boolean expectedMandatory = true;
193             TypedAttribute actualThreadFactory = (TypedAttribute) attributes
194                     .get("threadfactory");
195
196             DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
197             doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
198             .getUnknownSchemaNodes();
199             doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
200             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             Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
217                     .getRuntimeBeans();
218             assertThat(runtimeBeans.size(), is(1));
219             RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
220                     THREADPOOL_DYNAMIC_MXB_NAME);
221             assertThat(rootRB.isRoot(), is(true));
222             assertThat(rootRB.getAttributes().size(), is(1));
223             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             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
232                     .get(THREADFACTORY_NAMING_MXB_NAME);
233             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
234                     .getRuntimeBeans();
235             assertThat(runtimeBeans.size(), is(4));
236             {
237                 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                 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                 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
254                         "stream");
255                 assertNotNull(streamRB);
256                 assertFalse(streamRB.getKeyYangName().isPresent());
257                 assertFalse(streamRB.getKeyJavaName().isPresent());
258                 Map<String, AttributeIfc> attributeMap = streamRB
259                         .getYangPropertiesToTypesMap();
260                 assertEquals(4, attributeMap.size());
261
262                 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(), is(CompositeType.class));
268                 Set<String> propsExpected = new HashSet<String>(2);
269                 propsExpected.add("port");
270                 propsExpected.add("core-size");
271                 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
272                         is(propsExpected));
273                 propsExpected = new HashSet<String>(2);
274                 propsExpected.add("Port");
275                 propsExpected.add("CoreSize");
276                 assertThat(
277                         toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
278                         is(propsExpected));
279                 propsExpected = new HashSet<String>(2);
280                 propsExpected.add("port");
281                 propsExpected.add("coreSize");
282                 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
283                         is(propsExpected));
284
285                 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
286                         .get("timestamp");
287                 assertNotNull(timestampAttr);
288
289                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
290                         .get("state");
291                 assertNotNull(stateAttr);
292
293                 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(), is(ArrayType.class));
303
304             }
305
306         }
307         { // test multiple dependencies
308             ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
309             Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
310             assertEquals(1, attributes.size());
311             AttributeIfc threadpoolsAttr = attributes.get("threadpools");
312             assertNotNull(threadpoolsAttr);
313             assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
314             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.