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