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 junit.framework.Assert.assertNotNull;
11 import static junit.framework.Assert.format;
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
20 import org.junit.Before;
21 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.YangModelSearchUtils;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28 import com.google.common.base.Preconditions;
30 public abstract class AbstractYangTest {
31 protected SchemaContext context;
32 protected Map<String, Module> namesToModules; // are module names globally
34 protected Module configModule, rpcContextModule, threadsModule,
35 threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
36 jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
39 public void loadYangFiles() throws Exception {
40 List<InputStream> yangISs = new ArrayList<>();
41 yangISs.addAll(getStreams("/test-config-threads.yang",
42 "/test-config-threads-java.yang",
43 "/config-bgp-listener-impl.yang", "/ietf-inet-types.yang",
44 "/config-jmx-it.yang", "/config-jmx-it-impl.yang",
45 "/test-config-files.yang", "/test-config-files1.yang"));
47 yangISs.addAll(getConfigApiYangInputStreams());
49 YangParserImpl parser = new YangParserImpl();
50 Set<Module> modulesToBuild = parser.parseYangModelsFromStreams(yangISs);
52 for (InputStream is : yangISs) {
55 context = parser.resolveSchemaContext(modulesToBuild);
56 namesToModules = YangModelSearchUtils.mapModulesByNames(context
58 configModule = namesToModules.get(ConfigConstants.CONFIG_MODULE);
59 rpcContextModule = namesToModules.get(ConfigConstants.CONFIG_MODULE);
60 threadsModule = namesToModules
61 .get(ConfigConstants.CONFIG_THREADS_MODULE);
62 threadsJavaModule = namesToModules.get("config-threads-java");
63 bgpListenerJavaModule = namesToModules.get("config-bgp-listener-impl");
64 ietfInetTypesModule = namesToModules
65 .get(ConfigConstants.IETF_INET_TYPES);
66 jmxModule = namesToModules.get("config-jmx-it");
67 jmxImplModule = namesToModules.get("config-jmx-it-impl");
68 testFilesModule = namesToModules.get("test-config-files");
69 testFiles1Module = namesToModules.get("test-config-files1");
73 public static List<InputStream> getConfigApiYangInputStreams() {
74 return getStreams("/META-INF/yang/config.yang",
75 "/META-INF/yang/rpc-context.yang");
78 public Map<QName, IdentitySchemaNode> mapIdentitiesByQNames(Module module) {
79 Map<QName, IdentitySchemaNode> result = new HashMap<>();
80 for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
81 QName qName = identitySchemaNode.getQName();
82 Preconditions.checkArgument(
83 result.containsKey(qName) == false,
84 format("Two identities of %s contain same " + "qname %s",
86 result.put(qName, identitySchemaNode);
91 protected static List<InputStream> getStreams(String... paths) {
92 List<InputStream> result = new ArrayList<>();
93 for (String path : paths) {
94 InputStream is = AbstractYangTest.class.getResourceAsStream(path);
95 assertNotNull(path + " is null", is);