eb5f70d7c2ba0087c0a84babeb73bbc9ad265c9e
[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.test.util.YangParserTestUtils;
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         context = YangParserTestUtils.parseYangStreams(yangISs);
60         // close ISs
61         for (InputStream is : yangISs) {
62             is.close();
63         }
64         namesToModules = YangModelSearchUtils.mapModulesByNames(context
65                 .getModules());
66         configModule = namesToModules.get(ConfigConstants.CONFIG_MODULE);
67         rpcContextModule = namesToModules.get(ConfigConstants.CONFIG_MODULE);
68         threadsModule = namesToModules
69                 .get(ConfigConstants.CONFIG_THREADS_MODULE);
70         threadsJavaModule = namesToModules.get("config-threads-java");
71         bgpListenerJavaModule = namesToModules.get("config-bgp-listener-impl");
72         ietfInetTypesModule = namesToModules
73                 .get(ConfigConstants.IETF_INET_TYPES);
74         jmxModule = namesToModules.get("config-jmx-it");
75         jmxImplModule = namesToModules.get("config-jmx-it-impl");
76         testFilesModule = namesToModules.get("test-config-files");
77         testFiles1Module = namesToModules.get("test-config-files1");
78
79     }
80
81     public static List<InputStream> getConfigApiYangInputStreams() {
82         return getStreams("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang");
83     }
84
85     public Map<QName, IdentitySchemaNode> mapIdentitiesByQNames(final Module module) {
86         Map<QName, IdentitySchemaNode> result = new HashMap<>();
87         for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
88             QName qName = identitySchemaNode.getQName();
89             Preconditions.checkArgument(
90                     result.containsKey(qName) == false,
91                     "Two identities of %s contain same qname %s",
92                             module, qName);
93             result.put(qName, identitySchemaNode);
94         }
95         return result;
96     }
97
98     protected static List<InputStream> getStreams(final String... paths) {
99         List<InputStream> result = new ArrayList<>();
100         for (String path : paths) {
101             InputStream is = AbstractYangTest.class.getResourceAsStream(path);
102             assertNotNull(path + " is null", is);
103             result.add(is);
104         }
105         return result;
106     }
107
108     protected Map<QName, ServiceInterfaceEntry>  loadThreadsServiceInterfaceEntries(final String packageName) {
109         Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
110         return ServiceInterfaceEntry.create(threadsModule, packageName,identitiesToSIs);
111     }
112
113     protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava(
114             final Map<QName, ServiceInterfaceEntry> modulesToSIEs, final String packageName) {
115         Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
116                 .create(threadsJavaModule, modulesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl
117                 (context)), packageName);
118         Assert.assertNotNull(namesToMBEs);
119         Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
120                 ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
121                 THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
122         assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
123         return namesToMBEs;
124     }
125
126 }