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