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