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