2 * Copyright (c) 2014 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.yangtools.it.yang.runtime.tests;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableList.Builder;
16 import com.google.common.collect.ImmutableSet;
18 import java.io.InputStream;
20 import java.util.List;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.sal.binding.generator.impl.BindingGeneratorImpl;
26 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleContext;
27 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
37 public class MultipleRevisionsSupportTest {
39 public static final YangModuleInfo TOPOLOGY_OLD_MODULE = org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev130712.$YangModuleInfoImpl
41 public static final YangModuleInfo TOPOLOGY_NEW_MODULE = org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.$YangModuleInfoImpl
44 public static final Set<YangModuleInfo> DEPENDENCIES = ImmutableSet.<YangModuleInfo> builder() //
45 .addAll(TOPOLOGY_OLD_MODULE.getImportedModules()) //
46 .addAll(TOPOLOGY_NEW_MODULE.getImportedModules()).build();
49 public void dependenciesOlderNewer() throws Exception {
50 List<InputStream> streams = ImmutableList.<InputStream> builder()//
51 .addAll(toInputStreams(DEPENDENCIES)) //
52 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
53 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
55 SchemaContext schemaContext = contextVerified(streams);
56 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
57 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
61 public void dependenciesNewerOlder() throws Exception {
62 List<InputStream> streams = ImmutableList.<InputStream> builder()//
63 .addAll(toInputStreams(DEPENDENCIES)) //
64 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
65 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
67 SchemaContext schemaContext = contextVerified(streams);
68 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
69 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
73 public void newerOlderDependencies() throws Exception {
74 List<InputStream> streams = ImmutableList.<InputStream> builder()//
75 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
76 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
77 .addAll(toInputStreams(DEPENDENCIES)) //
79 SchemaContext schemaContext = contextVerified(streams);
80 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
81 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
85 public void newerDependenciesOlder() throws Exception {
86 List<InputStream> streams = ImmutableList.<InputStream> builder()//
87 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
88 .addAll(toInputStreams(DEPENDENCIES)) //
89 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
91 SchemaContext schemaContext = contextVerified(streams);
92 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
93 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
97 public void OlderNewerDependencies() throws Exception {
98 List<InputStream> streams = ImmutableList.<InputStream> builder()//
99 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
100 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
101 .addAll(toInputStreams(DEPENDENCIES)) //
103 SchemaContext schemaContext = contextVerified(streams);
104 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
105 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
109 public void olderDependenciesNewer() throws Exception {
110 List<InputStream> streams = ImmutableList.<InputStream> builder()//
111 .add(TOPOLOGY_OLD_MODULE.getModuleSourceStream()) //
112 .add(TOPOLOGY_NEW_MODULE.getModuleSourceStream()) //
113 .addAll(toInputStreams(DEPENDENCIES)) //
115 SchemaContext schemaContext = contextVerified(streams);
116 verifySchemaDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
117 verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE);
120 private SchemaContext contextVerified(final List<InputStream> streams) {
121 YangParserImpl parser = new YangParserImpl();
122 Set<Module> modules = parser.parseYangModelsFromStreams(streams);
123 assertNotNull(modules);
124 assertFalse(modules.isEmpty());
125 SchemaContext context = parser.resolveSchemaContext(modules);
126 assertNotNull(context);
130 private void verifyBindingDifference(final SchemaContext schemaContext, final YangModuleInfo oldModule, final YangModuleInfo newModule) {
131 generatedTypesVerified(schemaContext, oldModule, newModule);
134 private Map<Module, ModuleContext> generatedTypesVerified(final SchemaContext schemaContext, final YangModuleInfo oldModule,
135 final YangModuleInfo newModule) {
136 BindingGeneratorImpl generator = new BindingGeneratorImpl();
137 generator.generateTypes(schemaContext);
138 return generator.getModuleContexts();
141 private void verifySchemaDifference(final SchemaContext context, final YangModuleInfo topologyOldModule,
142 final YangModuleInfo topologyNewModule) {
143 Module oldModel = context.findModuleByNamespaceAndRevision(//
144 URI.create(TOPOLOGY_OLD_MODULE.getNamespace()), QName.parseRevision(TOPOLOGY_OLD_MODULE.getRevision()));
146 Module newModel = context.findModuleByNamespaceAndRevision(//
147 URI.create(TOPOLOGY_NEW_MODULE.getNamespace()), QName.parseRevision(TOPOLOGY_NEW_MODULE.getRevision()));
149 SchemaNode oldNode = findSchemaNode(oldModel, "network-topology", "topology", "link");
150 SchemaNode newNode = findSchemaNode(newModel, "network-topology", "topology", "link");
152 assertNotNull(oldNode);
153 assertNotNull(newNode);
155 assertDeepRevision(TOPOLOGY_OLD_MODULE.getRevision(), oldNode);
156 assertDeepRevision(TOPOLOGY_NEW_MODULE.getRevision(), newNode);
159 private static void assertDeepRevision(final String revision, final SchemaNode node) {
160 assertEquals("Wrong revision: " + node.getPath(), revision, node.getQName().getFormattedRevision());
161 if (node instanceof DataNodeContainer) {
162 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
163 assertDeepRevision(revision, child);
165 } else if (node instanceof ChoiceSchemaNode) {
166 for (DataSchemaNode child : ((ChoiceSchemaNode) node).getCases()) {
167 assertDeepRevision(revision, child);
172 private static final SchemaNode findSchemaNode(final DataNodeContainer container, final String... pathArgs) {
173 DataNodeContainer previous = container;
175 SchemaNode result = (container instanceof SchemaNode) ? (SchemaNode) container : null;
176 for (String arg : pathArgs) {
177 if (previous == null) {
180 for (DataSchemaNode child : previous.getChildNodes()) {
181 if (child.getQName().getLocalName().equals(arg)) {
182 if (child instanceof DataNodeContainer) {
183 previous = (DataNodeContainer) child;
195 private static final Iterable<? extends InputStream> toInputStreams(final Set<YangModuleInfo> moduleInfos)
197 Builder<InputStream> streams = ImmutableList.<InputStream> builder();
198 for (YangModuleInfo yangModuleInfo : moduleInfos) {
199 streams.add(yangModuleInfo.getModuleSourceStream());
201 return streams.build();