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