Adjust for yangtools normalizing module names
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / AbstractYangTest.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.assertNotNull;
12 import static org.junit.Assert.assertThat;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.YangModelSearchUtils;
24 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30
31 public abstract class AbstractYangTest {
32     protected SchemaContext context;
33     protected Map<String, Module> namesToModules; // are module names globally
34                                                   // unique?
35     protected Module configModule, rpcContextModule, threadsModule,
36             threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
37             jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
38
39     public static final String EVENTBUS_MXB_NAME = "eventbus";
40     public static final String ASYNC_EVENTBUS_MXB_NAME = "async-eventbus";
41     public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
42     public static final String THREADPOOL_DYNAMIC_MXB_NAME = "threadpool-dynamic";
43     public static final String THREADPOOL_REGISTRY_IMPL_NAME = "threadpool-registry-impl";
44
45     public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
46
47     @Before
48     public void loadYangFiles() throws Exception {
49         final List<InputStream> yangISs = new ArrayList<>();
50         yangISs.addAll(getStreams("/test-config-threads.yang",
51                 "/test-config-threads-java.yang",
52                 "/config-bgp-listener-impl.yang", "/ietf-inet-types.yang",
53                 "/config-jmx-it.yang", "/config-jmx-it-impl.yang",
54                 "/test-config-files.yang", "/test-config-files1.yang"));
55
56         yangISs.addAll(getConfigApiYangInputStreams());
57
58         this.context = YangParserTestUtils.parseYangStreams(yangISs);
59         // close ISs
60         for (final InputStream is : yangISs) {
61             is.close();
62         }
63         this.namesToModules = YangModelSearchUtils.mapModulesByNames(this.context
64                 .getModules());
65         this.configModule = this.namesToModules.get(ConfigConstants.CONFIG_MODULE);
66         this.rpcContextModule = this.namesToModules.get(ConfigConstants.CONFIG_MODULE);
67         this.threadsModule = this.namesToModules
68                 .get(ConfigConstants.CONFIG_THREADS_MODULE);
69         this.threadsJavaModule = this.namesToModules.get("config-threads-java");
70         this.bgpListenerJavaModule = this.namesToModules.get("config-bgp-listener-impl");
71         this.ietfInetTypesModule = this.namesToModules
72                 .get(ConfigConstants.IETF_INET_TYPES);
73         this.jmxModule = this.namesToModules.get("config-jmx-it");
74         this.jmxImplModule = this.namesToModules.get("config-jmx-it-impl");
75         this.testFilesModule = this.namesToModules.get("test-config-files");
76         this.testFiles1Module = this.namesToModules.get("test-config-files1");
77
78     }
79
80     public static List<InputStream> getConfigApiYangInputStreams() {
81         return getStreams("/META-INF/yang/config@2013-04-05.yang", "/META-INF/yang/rpc-context@2013-06-17.yang");
82     }
83
84     public Map<QName, IdentitySchemaNode> mapIdentitiesByQNames(final Module module) {
85         final Map<QName, IdentitySchemaNode> result = new HashMap<>();
86         for (final IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
87             final QName qName = identitySchemaNode.getQName();
88             Preconditions.checkArgument(
89                     result.containsKey(qName) == false,
90                     "Two identities of %s contain same qname %s",
91                             module, qName);
92             result.put(qName, identitySchemaNode);
93         }
94         return result;
95     }
96
97     protected static List<InputStream> getStreams(final String... paths) {
98         final List<InputStream> result = new ArrayList<>();
99         for (final String path : paths) {
100             final InputStream is = AbstractYangTest.class.getResourceAsStream(path);
101             assertNotNull(path + " is null", is);
102             result.add(is);
103         }
104         return result;
105     }
106
107     protected Map<QName, ServiceInterfaceEntry>  loadThreadsServiceInterfaceEntries(final String packageName) {
108         final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
109         return ServiceInterfaceEntry.create(this.threadsModule, packageName,identitiesToSIs);
110     }
111
112     protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava(
113             final Map<QName, ServiceInterfaceEntry> modulesToSIEs, final String packageName) {
114         final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
115                 .create(this.threadsJavaModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl
116                 (this.context)), packageName);
117         Assert.assertNotNull(namesToMBEs);
118         final Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
119                 ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
120                 THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
121         assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
122         return namesToMBEs;
123     }
124
125 }