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 static org.hamcrest.CoreMatchers.is;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThat;
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;
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;
32 public abstract class AbstractYangTest {
33 protected SchemaContext context;
34 protected Map<String, Module> namesToModules; // are module names globally
36 protected Module configModule, rpcContextModule, threadsModule,
37 threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
38 jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
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";
46 public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
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"));
57 yangISs.addAll(getConfigApiYangInputStreams());
59 context = YangParserTestUtils.parseYangStreams(yangISs);
61 for (InputStream is : yangISs) {
64 namesToModules = YangModelSearchUtils.mapModulesByNames(context
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");
81 public static List<InputStream> getConfigApiYangInputStreams() {
82 return getStreams("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang");
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",
93 result.put(qName, identitySchemaNode);
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);
108 protected Map<QName, ServiceInterfaceEntry> loadThreadsServiceInterfaceEntries(final String packageName) {
109 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
110 return ServiceInterfaceEntry.create(threadsModule, packageName,identitiesToSIs);
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));