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