Initial code drop of yang model driven configuration system
[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
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
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;
27
28 import com.google.common.base.Preconditions;
29
30 public abstract class AbstractYangTest {
31     protected SchemaContext context;
32     protected Map<String, Module> namesToModules; // are module names globally
33                                                   // unique?
34     protected Module configModule, rpcContextModule, threadsModule,
35             threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
36             jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
37
38     @Before
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"));
46
47         yangISs.addAll(getConfigApiYangInputStreams());
48
49         YangParserImpl parser = new YangParserImpl();
50         Set<Module> modulesToBuild = parser.parseYangModelsFromStreams(yangISs);
51         // close ISs
52         for (InputStream is : yangISs) {
53             is.close();
54         }
55         context = parser.resolveSchemaContext(modulesToBuild);
56         namesToModules = YangModelSearchUtils.mapModulesByNames(context
57                 .getModules());
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");
70
71     }
72
73     public static List<InputStream> getConfigApiYangInputStreams() {
74         return getStreams("/META-INF/yang/config.yang",
75                 "/META-INF/yang/rpc-context.yang");
76     }
77
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",
85                             module, qName));
86             result.put(qName, identitySchemaNode);
87         }
88         return result;
89     }
90
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);
96             result.add(is);
97         }
98         return result;
99     }
100 }