2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator;
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;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
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;
34 public abstract class AbstractYangTest {
35 protected SchemaContext context;
36 protected Map<String, Module> namesToModules; // are module names globally
38 protected Module configModule, rpcContextModule, threadsModule,
39 threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
40 jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
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";
48 public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
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"));
59 yangISs.addAll(getConfigApiYangInputStreams());
61 YangParserImpl parser = new YangParserImpl();
62 Set<Module> modulesToBuild = parser.parseYangModelsFromStreams(yangISs);
64 for (InputStream is : yangISs) {
67 context = parser.resolveSchemaContext(modulesToBuild);
68 namesToModules = YangModelSearchUtils.mapModulesByNames(context
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");
85 public static List<InputStream> getConfigApiYangInputStreams() {
86 return getStreams("/META-INF/yang/config.yang",
87 "/META-INF/yang/rpc-context.yang");
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",
98 result.put(qName, identitySchemaNode);
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);
113 protected Map<QName, ServiceInterfaceEntry> loadThreadsServiceInterfaceEntries(String packageName) {
114 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
115 return ServiceInterfaceEntry.create(threadsModule, packageName,identitiesToSIs);
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));