* fix : yang-generator can't find SIE if its base is another SIE
[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         DependencyAttribute threadFactoryAttribute = (DependencyAttribute) attributes
118                 .get("thread-factory");
119         assertNotNull(threadFactoryAttribute);
120         assertFalse(threadFactoryAttribute.getDependency().isMandatory());
121         assertThat(threadFactoryAttribute.getDependency().getSie()
122                 .getTypeName(), is("ThreadFactoryServiceInterface"));
123         assertThat(threadFactoryAttribute.getAttributeYangName(),
124                 is("thread-factory"));
125         assertThat(threadFactoryAttribute.getLowerCaseCammelCase(),
126                 is("threadFactory"));
127         assertThat(threadFactoryAttribute.getUpperCaseCammelCase(),
128                 is("ThreadFactory"));
129         assertThat(threadFactoryAttribute.getOpenType(), is(SimpleType.class));
130         assertNull(threadFactoryAttribute.getNullableDefault());
131         assertNull(threadFactoryAttribute.getNullableDescription());
132         assertThat(threadFactoryAttribute.getType().getName(), is("ObjectName"));
133     }
134
135     protected RuntimeBeanEntry findFirstByYangName(
136             Collection<RuntimeBeanEntry> runtimeBeans, String yangName) {
137         for (RuntimeBeanEntry rb : runtimeBeans) {
138             if (yangName.equals(rb.getYangName()))
139                 return rb;
140         }
141         throw new IllegalArgumentException("Yang name not found:" + yangName
142                 + " in " + runtimeBeans);
143     }
144
145     @Test
146     public void testGetWhenConditionMatcher() {
147         assertMatches("config",
148                 "/config:modules/config:module/config:type = 'threadpool-dynamic'");
149         assertMatches("ns",
150                 "/ns:modules/ns:module/ns:type = 'threadpool-dynamic'");
151         assertMatches("config",
152                 "/config:modules/config:module/config:type=\"threadpool-dynamic\"");
153     }
154
155     private void assertMatches(String prefix, String input) {
156         RevisionAwareXPath whenConstraint = mock(RevisionAwareXPath.class);
157         doReturn(input).when(whenConstraint).toString();
158         Matcher output = ModuleMXBeanEntry.getWhenConditionMatcher(prefix,
159                 whenConstraint);
160         assertTrue(output.matches());
161         assertEquals("threadpool-dynamic", output.group(1));
162     }
163
164     @Test
165     public void testThreadsJava() {
166         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = loadThreadsJava();
167
168         { // check threadpool-dynamic
169             ModuleMXBeanEntry dynamicThreadPool = namesToMBEs
170                     .get(THREADPOOL_DYNAMIC_MXB_NAME);
171             Map<String, AttributeIfc> attributes = dynamicThreadPool
172                     .getAttributes();
173             // core-size, keepalive, maximum-size
174             // threadfactory
175             Set<String> longAttribs = Sets.newHashSet("core-size",
176                     "maximum-size");
177             for (String longAttrib : longAttribs) {
178
179                 TypedAttribute attribute = (TypedAttribute) attributes
180                         .get(longAttrib);
181                 assertThat("Failed to check " + longAttrib,
182                         attribute.getType(),
183                         is((Type) Types.typeForClass(Long.class)));
184             }
185             // check dependency on thread factory
186             QName threadfactoryQName = new QName(THREADS_NAMESPACE,
187                     THREADS_REVISION_DATE, "threadfactory");
188             ServiceInterfaceEntry threadFactorySIEntry = modulesToSIEs
189                     .get(threadfactoryQName);
190             assertNotNull(threadFactorySIEntry);
191             boolean expectedMandatory = true;
192             TypedAttribute actualThreadFactory = (TypedAttribute) attributes
193                     .get("threadfactory");
194
195             DataSchemaNode mockedDataSchemaNode = mock(DataSchemaNode.class);
196             doReturn(Collections.emptyList()).when(mockedDataSchemaNode)
197                     .getUnknownSchemaNodes();
198             doReturn(threadfactoryQName).when(mockedDataSchemaNode).getQName();
199             AttributeIfc expectedDependencyAttribute = new DependencyAttribute(
200                     mockedDataSchemaNode, threadFactorySIEntry,
201                     expectedMandatory, "threadfactory description");
202             assertThat(actualThreadFactory, is(expectedDependencyAttribute));
203             assertThat(
204                     dynamicThreadPool
205                             .getFullyQualifiedName("DynamicThreadPoolModuleMXBean"),
206                     is(PACKAGE_NAME + ".DynamicThreadPoolModuleMXBean"));
207             assertThat(dynamicThreadPool.getNullableDescription(),
208                     is("threadpool-dynamic description"));
209             assertThat(dynamicThreadPool.getYangModuleName(),
210                     is("config-threads-java"));
211             assertThat(dynamicThreadPool.getYangModuleLocalname(),
212                     is(THREADPOOL_DYNAMIC_MXB_NAME));
213
214             // check root runtime bean
215             Collection<RuntimeBeanEntry> runtimeBeans = dynamicThreadPool
216                     .getRuntimeBeans();
217             assertThat(runtimeBeans.size(), is(1));
218             RuntimeBeanEntry rootRB = findFirstByYangName(runtimeBeans,
219                     THREADPOOL_DYNAMIC_MXB_NAME);
220             assertThat(rootRB.isRoot(), is(true));
221             assertThat(rootRB.getAttributes().size(), is(1));
222             JavaAttribute attribute = (JavaAttribute) rootRB.getAttributes()
223                     .iterator().next();
224             assertThat(attribute.getAttributeYangName(), is("created-sessions"));
225             assertThat(rootRB.getYangName(), is(THREADPOOL_DYNAMIC_MXB_NAME));
226             assertThat(attribute.getType().getFullyQualifiedName(),
227                     is(Long.class.getName()));
228         }
229         {// check threadfactory-naming
230             ModuleMXBeanEntry threadFactoryNaming = namesToMBEs
231                     .get(THREADFACTORY_NAMING_MXB_NAME);
232             Collection<RuntimeBeanEntry> runtimeBeans = threadFactoryNaming
233                     .getRuntimeBeans();
234             assertThat(runtimeBeans.size(), is(4));
235             {
236                 RuntimeBeanEntry threadRB = findFirstByYangName(runtimeBeans,
237                         "thread");
238                 assertNotNull(threadRB);
239                 assertFalse(threadRB.isRoot());
240                 assertEquals("name", threadRB.getKeyYangName().get());
241                 assertEquals("Name", threadRB.getKeyJavaName().get());
242                 assertThat(threadRB.getAttributes().size(), is(1));
243                 AttributeIfc threadNameAttr = threadRB.getAttributes()
244                         .iterator().next();
245                 assertThat(threadNameAttr.getAttributeYangName(), is("name"));
246                 assertTrue(threadNameAttr instanceof JavaAttribute);
247                 assertThat(((JavaAttribute) threadNameAttr).getType()
248                         .getFullyQualifiedName(), is(String.class.getName()));
249                 assertThat(threadRB.getRpcs().size(), is(2));
250             }
251             {
252                 RuntimeBeanEntry streamRB = findFirstByYangName(runtimeBeans,
253                         "stream");
254                 assertNotNull(streamRB);
255                 assertFalse(streamRB.getKeyYangName().isPresent());
256                 assertFalse(streamRB.getKeyJavaName().isPresent());
257                 Map<String, AttributeIfc> attributeMap = streamRB
258                         .getYangPropertiesToTypesMap();
259                 assertEquals(4, attributeMap.size());
260
261                 TOAttribute toAttr = (TOAttribute) attributeMap.get("peer");
262                 assertNotNull(toAttr);
263                 assertThat(toAttr.getAttributeYangName(), is("peer"));
264                 assertThat(toAttr.getLowerCaseCammelCase(), is("peer"));
265                 assertThat(toAttr.getUpperCaseCammelCase(), is("Peer"));
266                 assertThat(toAttr.getOpenType(), is(CompositeType.class));
267                 Set<String> propsExpected = new HashSet<String>(2);
268                 propsExpected.add("port");
269                 propsExpected.add("core-size");
270                 assertThat(toAttr.getYangPropertiesToTypesMap().keySet(),
271                         is(propsExpected));
272                 propsExpected = new HashSet<String>(2);
273                 propsExpected.add("Port");
274                 propsExpected.add("CoreSize");
275                 assertThat(
276                         toAttr.getCapitalizedPropertiesToTypesMap().keySet(),
277                         is(propsExpected));
278                 propsExpected = new HashSet<String>(2);
279                 propsExpected.add("port");
280                 propsExpected.add("coreSize");
281                 assertThat(toAttr.getJmxPropertiesToTypesMap().keySet(),
282                         is(propsExpected));
283
284                 JavaAttribute timestampAttr = (JavaAttribute) attributeMap
285                         .get("timestamp");
286                 assertNotNull(timestampAttr);
287
288                 JavaAttribute stateAttr = (JavaAttribute) attributeMap
289                         .get("state");
290                 assertNotNull(stateAttr);
291
292                 ListAttribute innerStream = (ListAttribute) attributeMap
293                         .get("inner-stream-list");
294                 assertNotNull(innerStream);
295                 assertThat(innerStream.getAttributeYangName(),
296                         is("inner-stream-list"));
297                 assertThat(innerStream.getLowerCaseCammelCase(),
298                         is("innerStreamList"));
299                 assertThat(innerStream.getUpperCaseCammelCase(),
300                         is("InnerStreamList"));
301                 assertThat(innerStream.getOpenType(), is(ArrayType.class));
302
303             }
304
305         }
306         { // test multiple dependencies
307             ModuleMXBeanEntry threadPoolRegistry = namesToMBEs.get(THREADPOOL_REGISTRY_IMPL_NAME);
308             Map<String, AttributeIfc> attributes = threadPoolRegistry.getAttributes();
309             assertEquals(1, attributes.size());
310             AttributeIfc threadpoolsAttr = attributes.get("threadpools");
311             assertNotNull(threadpoolsAttr);
312             assertTrue(threadpoolsAttr instanceof ListDependenciesAttribute);
313             ListDependenciesAttribute threadpools = (ListDependenciesAttribute) threadpoolsAttr;
314         }
315     }
316
317 }