<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-parser-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>yang-binding</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-spec-util</artifactId>
+ </dependency>
</dependencies>
<build>
import org.opendaylight.yangtools.yang.common.Revision;
/**
- * Main entrypoint into YANG (Module) Library support instance.
+ * Main entry point into YANG (Module) Library support instance.
*/
@Beta
@NonNullByDefault
/**
* Create a MountPointContextFactory, backed by a specific SchemaContextResolver.
*
- * @param mountId Resulting Mount Point identitifer
+ * @param mountId Resulting Mount Point identifier
* @param resolver SchemaContext resolver
* @return A new factory
* @throws NullPointerException if any argument is null
* @return A revision.
*/
Revision implementedRevision();
+
+ /**
+ * Create a new content builder which is used to serialize yang library content into NormalizedNodes.
+ * This content builder has further options which can influence the resulting content.
+ *
+ * @return A new yang library content builder.
+ */
+ YangLibraryContentBuilder newContentBuilder();
}
package org.opendaylight.mdsal.yanglib.api;
import com.google.common.annotations.Beta;
-import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
@NonNullByDefault
public interface YangLibSupportFactory {
- YangLibSupport createYangLibSupport(YangParserFactory parserFactory) throws YangParserException, IOException;
+ YangLibSupport createYangLibSupport(YangParserFactory parserFactory) throws YangParserException;
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.api;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+
+@Beta
+@NonNullByDefault
+public interface YangLibraryContentBuilder {
+ /**
+ * Set the default EffectiveModelContext used for this content builder.
+ *
+ * @param modelContext EffectiveModelContext to use for content generation.
+ * @return this builder
+ * @throws NullPointerException if any argument is null
+ */
+ YangLibraryContentBuilder defaultContext(EffectiveModelContext modelContext);
+
+ /**
+ * Add a secondary datastore(s) which use different EffectiveModelContext than the default provided context.
+ * This/These datastore/s are used in the output encoding of the YANG library.
+ *
+ * @param identifier identifies the datastore in the ouput
+ * @param context EffectiveModelContext of this datastore
+ * @return this builder
+ * @throws NullPointerException if any argument is null
+ * @throws IllegalArgumentException if this implementation does not support per-datastore contexts and a conflicting
+ * context is already present.
+ */
+ YangLibraryContentBuilder addDatastore(DatastoreIdentifier identifier, EffectiveModelContext context);
+
+ /**
+ * Option to include legacy YANG library content in the resulting output.
+ *
+ * @return LegacyYangLibraryContentBuilder which generates output that contains YANG library data in
+ * both legacy and non-legacy format.
+ */
+ YangLibraryContentBuilderWithLegacy includeLegacy();
+
+ /**
+ * Format the contents of the YANG library into NormalizedNodes using the provided EffectiveModelContext.
+ *
+ * @return List of NormalizedNodes that contain the YANG library content.
+ */
+ ContainerNode formatYangLibraryContent();
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.api;
+
+import com.google.common.annotations.Beta;
+import java.util.Optional;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+
+@Beta
+public interface YangLibraryContentBuilderWithLegacy extends YangLibraryContentBuilder {
+ /**
+ * Format legacy content, if applicable. It is guaranteed to be a sibling of the main content returned by
+ * {@link #formatYangLibraryContent()}.
+ *
+ * @return Legacy content, if applicable.
+ */
+ Optional<ContainerNode> formatYangLibraryLegacyContent();
+}
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
import org.opendaylight.yangtools.yang.common.Revision;
return delegate().implementedRevision();
}
+ @Override
+ public YangLibraryContentBuilder newContentBuilder() {
+ return delegate().newContentBuilder();
+ }
+
@Override
protected abstract @NonNull YangLibSupport delegate();
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.util;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+
+public final class YanglibContextBuilder {
+
+ private YanglibContextBuilder() {
+
+ }
+
+ public static EffectiveModelContext buildContext(YangParserFactory parserFactory, Class<?>... clazz)
+ throws Exception {
+ final YangParser parser = parserFactory.createParser();
+
+ for (Class<?> aclazz : clazz) {
+ final YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(aclazz);
+
+ Set<YangModuleInfo> infos = new HashSet<>();
+ flatDependencies(infos, moduleInfo);
+
+ for (YangModuleInfo info : infos) {
+ final YangTextSchemaSource source =
+ YangTextSchemaSource.delegateForByteSource(sourceIdentifierFrom(info),
+ info.getYangTextByteSource());
+ try {
+ parser.addSource(source);
+ } catch (YangSyntaxErrorException | IOException e) {
+ throw new YangParserException("Failed to add source for " + moduleInfo, e);
+ }
+ }
+ }
+
+ return parser.buildEffectiveModel();
+ }
+
+ static void flatDependencies(final Set<YangModuleInfo> set, final YangModuleInfo moduleInfo) {
+ if (set.add(moduleInfo)) {
+ for (YangModuleInfo dep : moduleInfo.getImportedModules()) {
+ flatDependencies(set, dep);
+ }
+ }
+ }
+
+ private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
+ final QName name = moduleInfo.getName();
+ return RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision());
+ }
+}
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>mdsal-binding-dom-codec</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-generator-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-generator-impl</artifactId>
+ </dependency>
<dependency>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-parser-api</artifactId>
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc7895;
+
+import static com.google.common.base.Verify.verifyNotNull;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilderWithLegacy;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.Submodule;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.SubmoduleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.SubmoduleKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+
+class Rfc7895ContentBuilder implements YangLibraryContentBuilderWithLegacy {
+ private static final CommonLeafs.Revision EMPTY_REV = new CommonLeafs.Revision("");
+
+ private final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec;
+
+ private EffectiveModelContext context;
+
+ Rfc7895ContentBuilder(final BindingCodecTree codecTree) {
+ this.legacyCodec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
+ }
+
+ @Override
+ public Rfc7895ContentBuilder defaultContext(final EffectiveModelContext modelContext) {
+ this.context = modelContext;
+ return this;
+ }
+
+ @Override
+ public YangLibraryContentBuilder addDatastore(final DatastoreIdentifier identifier,
+ final EffectiveModelContext newContext) {
+ // NOOP does not apply for rfc7895
+ return this;
+ }
+
+ @Override
+ public YangLibraryContentBuilderWithLegacy includeLegacy() {
+ // NOOP does not apply for rfc7895
+ return this;
+ }
+
+ @Override
+ public ContainerNode formatYangLibraryContent() {
+ return formatModulesState(context);
+ }
+
+ @VisibleForTesting
+ ContainerNode formatModulesState(final EffectiveModelContext effectiveModelContext) {
+ Map<SubmoduleKey, Submodule> vals;
+ // Two-step process: we first build the content and then use hashCode() to generate module-set-id
+ final ModulesStateBuilder builder = new ModulesStateBuilder().setModuleSetId("")
+ .setModule(effectiveModelContext.getModules().stream()
+ .map(module -> new ModuleBuilder()
+ .setName(new YangIdentifier(module.getName()))
+ .setNamespace(new Uri(module.getNamespace().toString()))
+ .setRevision(convertRevision(module.getRevision()))
+ .setSubmodule(module.getSubmodules().stream()
+ .map(submodule -> new SubmoduleBuilder()
+ .setName(new YangIdentifier(submodule.getName()))
+ .setRevision(convertRevision(submodule.getRevision()))
+ .build())
+ .collect(Collectors.toUnmodifiableList()))
+ .setFeature(module.getFeatures().stream()
+ .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
+ .collect(Collectors.toUnmodifiableList()))
+ .setConformanceType(Module.ConformanceType.Implement)
+ .build())
+ .collect(Collectors.toUnmodifiableList()));
+
+ return (ContainerNode) legacyCodec.serialize(builder.setModuleSetId(String.valueOf(builder.build().hashCode()))
+ .build());
+ }
+
+ private static CommonLeafs.Revision convertRevision(final Optional<Revision> revision) {
+ return revision.map(rev -> new CommonLeafs.Revision(new RevisionIdentifier(rev.toString()))).orElse(EMPTY_REV);
+ }
+
+ @Override
+ public Optional<ContainerNode> formatYangLibraryLegacyContent() {
+ return Optional.of(formatYangLibraryContent());
+ }
+}
import static com.google.common.base.Verify.verifyNotNull;
import com.google.common.annotations.Beta;
-import com.google.common.collect.Collections2;
-import java.io.IOException;
+import com.google.common.base.Throwables;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.aries.blueprint.annotation.service.Reference;
import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.$YangModuleInfoImpl;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
+import org.opendaylight.mdsal.yanglib.util.YanglibContextBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@Beta
@NonNullByDefault
private final BindingDataObjectCodecTreeNode<ModulesState> codec;
private final EffectiveModelContext context;
+ private final BindingCodecTree codecTree;
@Inject
- public YangModuleLibrarySupport(final @Reference YangParserFactory parserFactory)
- throws YangParserException, IOException {
- final YangModuleInfo yangLibModule = $YangModuleInfoImpl.getInstance();
-
- // FIXME: DEFAULT_MODE should not be necessary, but it seems blueprint is still b0rked
- context = parserFactory.createParser(StatementParserMode.DEFAULT_MODE)
- .addLibSources(Collections2.transform(yangLibModule.getImportedModules(),
- YangModuleLibrarySupport::createSource))
- .addSource(createSource(yangLibModule))
- .buildEffectiveModel();
- final BindingCodecTree codecTree = new BindingNormalizedNodeCodecRegistry(BindingRuntimeContext.create(
- SimpleStrategy.INSTANCE, context)).getCodecContext();
-
- this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public YangModuleLibrarySupport(final @Reference YangParserFactory parserFactory) {
+ try {
+ context = YanglibContextBuilder.buildContext(parserFactory, ModulesState.class);
+ final BindingRuntimeContext runtimeContext =
+ BindingRuntimeContext.create(new SimpleStrategy(), context);
+ final BindingNormalizedNodeCodecRegistry codecFactory =
+ new BindingNormalizedNodeCodecRegistry(runtimeContext);
+ codecTree = codecFactory.create(BindingRuntimeContext.create(new SimpleStrategy(), context));
+ codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
+ } catch (Exception e) {
+ Throwables.throwIfUnchecked(e);
+ throw new IllegalStateException("Failed to build context for YangModuleLibrary", e);
+ }
}
@Override
return REVISION;
}
- private static YangTextSchemaSource createSource(final YangModuleInfo info) {
- final QName name = info.getName();
- return YangTextSchemaSource.delegateForByteSource(
- RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision()), info.getYangTextByteSource());
+ @Override
+ public YangLibraryContentBuilder newContentBuilder() {
+ return new Rfc7895ContentBuilder(codecTree);
}
}
*/
package org.opendaylight.mdsal.yanglib.rfc7895;
-import java.io.IOException;
+import com.google.common.annotations.Beta;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.kohsuke.MetaInfServices;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
import org.opendaylight.mdsal.yanglib.api.YangLibSupportFactory;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+@Beta
@MetaInfServices
@NonNullByDefault
public final class YangModuleLibrarySupportFactory implements YangLibSupportFactory {
+
@Override
- public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory)
- throws YangParserException, IOException {
+ public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory) {
return new YangModuleLibrarySupport(parserFactory);
}
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc7895;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.mdsal.yanglib.util.YanglibContextBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs.Revision;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module.ConformanceType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class YangModuleLibrarySupportTest {
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+ private YangModuleLibrarySupport yangLib;
+
+ @Before
+ public void setUp() throws Exception {
+ final EffectiveModelContext modelContext =
+ YanglibContextBuilder.buildContext(YANG_PARSER_FACTORY, ModulesState.class);
+ runtimeContext = BindingRuntimeContext.create(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
+ modelContext);
+ final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
+ codecTree = codecRegistry.create(runtimeContext);
+
+ yangLib = new YangModuleLibrarySupport(YANG_PARSER_FACTORY);
+ }
+
+ @Test
+ public void testModulesState() {
+ final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class));
+ final ModulesState modulesState = legacyCodec.deserialize(
+ yangLib.newContentBuilder().defaultContext(
+ (EffectiveModelContext)runtimeContext.getSchemaContext()).formatYangLibraryContent());
+
+ assertEquals(3, modulesState.nonnullModule().size());
+ // migrate modulesState from codec generated class to proper binding for checking
+ final List<Module> valuesToCheck = modulesState.getModule().stream()
+ .map(module -> createModule(module.getName().getValue(), module.getNamespace().getValue(),
+ module.getRevision().getRevisionIdentifier().getValue()))
+ .collect(Collectors.toList());
+ assertTrue(createControlModules().containsAll(valuesToCheck));
+ }
+
+ private static List<Module> createControlModules() {
+ return List.of(
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2016-06-21"),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"),
+ createModule("ietf-yang-types", "urn:ietf:params:xml:ns:yang:ietf-yang-types", "2013-07-15"));
+ }
+
+ private static Module createModule(final String name, final String namespace, final String revision) {
+ return new ModuleBuilder()
+ .setName(new YangIdentifier(name))
+ .setNamespace(new Uri(namespace))
+ .setRevision(new Revision(new RevisionIdentifier(revision)))
+ .setConformanceType(ConformanceType.Implement)
+ .setFeature(Collections.emptyList())
+ .build();
+ }
+}
\ No newline at end of file
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>mdsal-binding-dom-codec</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-spec-util</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-generator-impl</artifactId>
+ </dependency>
<dependency>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-parser-api</artifactId>
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc8525;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilderWithLegacy;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.SubmoduleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+
+class LegacyContentBuilder implements YangLibraryContentBuilderWithLegacy {
+ private static final CommonLeafs.Revision EMPTY_REV = new CommonLeafs.Revision("");
+
+ private final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec;
+ private final YangLibraryContentBuilderImpl delegate;
+
+ LegacyContentBuilder(final YangLibraryContentBuilderImpl delegate, final BindingCodecTree codecTree) {
+ this.delegate = requireNonNull(delegate);
+ this.legacyCodec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
+ }
+
+ @Override
+ public LegacyContentBuilder defaultContext(final EffectiveModelContext context) {
+ delegate.defaultContext(context);
+ return this;
+ }
+
+ @Override
+ public YangLibraryContentBuilder addDatastore(final DatastoreIdentifier identifier,
+ final EffectiveModelContext context) {
+ return delegate.addDatastore(identifier, context);
+ }
+
+ @Override
+ public YangLibraryContentBuilderWithLegacy includeLegacy() {
+ return this;
+ }
+
+ @Override
+ public ContainerNode formatYangLibraryContent() {
+ return delegate.formatYangLibrary();
+ }
+
+ @Override
+ public Optional<ContainerNode> formatYangLibraryLegacyContent() {
+ return Optional.of(formatModulesState(requireNonNull(delegate.getModelContext())));
+ }
+
+ @VisibleForTesting
+ ContainerNode formatModulesState(final EffectiveModelContext context) {
+ // Two-step process: we first build the content and then use hashCode() to generate module-set-id
+ final ModulesStateBuilder builder = new ModulesStateBuilder().setModuleSetId("")
+ .setModule(context.getModules().stream()
+ .map(module -> new ModuleBuilder()
+ .setName(new YangIdentifier(module.getName()))
+ .setNamespace(new Uri(module.getNamespace().toString()))
+ .setRevision(convertRevision(module.getRevision()))
+ .setSubmodule(module.getSubmodules().stream()
+ .map(submodule -> new SubmoduleBuilder()
+ .setName(new YangIdentifier(submodule.getName()))
+ .setRevision(convertRevision(submodule.getRevision()))
+ .build())
+ .collect(Collectors.toUnmodifiableList()))
+ .setFeature(module.getFeatures().stream()
+ .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
+ .collect(Collectors.toUnmodifiableList()))
+ .setConformanceType(Module.ConformanceType.Implement)
+ .build())
+ .collect(Collectors.toUnmodifiableList()));
+
+ return (ContainerNode) legacyCodec.serialize(builder.setModuleSetId(String.valueOf(builder.build().hashCode()))
+ .build());
+ }
+
+ private static CommonLeafs.Revision convertRevision(final Optional<Revision> revision) {
+ return revision.map(rev -> new CommonLeafs.Revision(new RevisionIdentifier(rev.toString()))).orElse(EMPTY_REV);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc8525;
+
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilderWithLegacy;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.module.SubmoduleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSet;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSetBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+
+class YangLibraryContentBuilderImpl implements YangLibraryContentBuilder {
+ private static final String MODULE_SET_NAME = "ODL_modules";
+
+ private final Map<DatastoreIdentifier, EffectiveModelContext> datastores = new HashMap<>();
+ private final BindingDataObjectCodecTreeNode<YangLibrary> codec;
+ private final BindingCodecTree codecTree;
+
+ private EffectiveModelContext modelContext;
+
+ YangLibraryContentBuilderImpl(final BindingCodecTree codecTree) {
+ this.codecTree = Objects.requireNonNull(codecTree);
+ this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class)));
+ }
+
+ @Override
+ public YangLibraryContentBuilder defaultContext(final EffectiveModelContext context) {
+ modelContext = context;
+ return this;
+ }
+
+ EffectiveModelContext getModelContext() {
+ return modelContext;
+ }
+
+ @Override
+ public YangLibraryContentBuilderWithLegacy includeLegacy() {
+ return new LegacyContentBuilder(this, codecTree);
+ }
+
+ @Override
+ public YangLibraryContentBuilder addDatastore(final DatastoreIdentifier identifier,
+ final EffectiveModelContext context) {
+ datastores.put(identifier, context);
+ return this;
+ }
+
+ @Override
+ public ContainerNode formatYangLibraryContent() {
+ checkState(modelContext != null, "EffectiveModelContext is required to format YangLibrary content");
+ return formatYangLibrary();
+ }
+
+ @NonNull ContainerNode formatYangLibrary() {
+ // Two-step process: we first build the content and then use hashCode() to generate module-set-id
+ final YangLibraryBuilder builder = new YangLibraryBuilder().setContentId("");
+ final ModuleSetBuilder moduleSetBuilder = new ModuleSetBuilder()
+ .setModule(modelContext.getModules().stream()
+ .map(this::buildModule)
+ .collect(Collectors.toUnmodifiableList()))
+ .setName(MODULE_SET_NAME);
+ final ModuleSet moduleSet = moduleSetBuilder.build();
+
+ builder.setModuleSet(List.of(moduleSet));
+ return (ContainerNode) codec.serialize(builder.setContentId(String.valueOf(builder.build().hashCode()))
+ .build());
+ }
+
+ private Module buildModule(final org.opendaylight.yangtools.yang.model.api.Module module) {
+ return new ModuleBuilder()
+ .setName(new YangIdentifier(module.getName()))
+ .setNamespace(new Uri(module.getQNameModule().getNamespace().toString()))
+ .setRevision(convertRevision(module.getRevision()))
+ .setSubmodule(module.getSubmodules().stream()
+ .map(submodule -> new SubmoduleBuilder()
+ .setName(new YangIdentifier(submodule.getName()))
+ .setRevision(convertRevision(submodule.getRevision()))
+ .build())
+ .collect(Collectors.toUnmodifiableList()))
+ .setFeature(module.getFeatures().stream()
+ .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
+ .collect(Collectors.toUnmodifiableList()))
+ .build();
+ }
+
+ private static RevisionIdentifier convertRevision(final Optional<Revision> revision) {
+ return revision.map(rev -> new RevisionIdentifier(rev.toString())).orElse(null);
+ }
+}
import static com.google.common.base.Verify.verifyNotNull;
import com.google.common.annotations.Beta;
-import com.google.common.collect.Collections2;
-import java.io.IOException;
+import com.google.common.base.Throwables;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.aries.blueprint.annotation.service.Reference;
import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.$YangModuleInfoImpl;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
+import org.opendaylight.mdsal.yanglib.util.YanglibContextBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@Beta
@NonNullByDefault
@Singleton
public final class YangLibrarySupport implements YangLibSupport {
private static final Revision REVISION = YangLibrary.QNAME.getRevision().orElseThrow();
+ private static final RevisionIdentifier EMPTY_REV = new RevisionIdentifier("1970-01-01");
+ private static final String MODULE_SET_NAME = "ODL_modules";
private final BindingDataObjectCodecTreeNode<YangLibrary> codec;
@SuppressWarnings("deprecation")
private final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec;
private final BindingIdentityCodec identityCodec;
private final EffectiveModelContext context;
+ private final BindingCodecTree codecTree;
@Inject
- public YangLibrarySupport(final @Reference YangParserFactory parserFactory)
- throws YangParserException, IOException {
- final YangModuleInfo yangLibModule = $YangModuleInfoImpl.getInstance();
+ @SuppressWarnings("checkstyle:IllegalCatch")
+ public YangLibrarySupport(final @Reference YangParserFactory parserFactory) {
+ try {
+ context = YanglibContextBuilder.buildContext(parserFactory, YangLibrary.class);
+ final BindingRuntimeContext runtimeContext =
+ BindingRuntimeContext.create(new SimpleStrategy(), context);
+ final BindingNormalizedNodeCodecRegistry codecFactory =
+ new BindingNormalizedNodeCodecRegistry(runtimeContext);
+ codecTree = codecFactory.create(BindingRuntimeContext.create(new SimpleStrategy(), context));
- // FIXME: DEFAULT_MODE should not be necessary, but it seems blueprint is still b0rked
- context = parserFactory.createParser(StatementParserMode.DEFAULT_MODE)
- .addLibSources(Collections2.transform(yangLibModule.getImportedModules(),
- YangLibrarySupport::createSource))
- .addSource(createSource(yangLibModule))
- .buildEffectiveModel();
- final BindingCodecTree codecTree = new BindingNormalizedNodeCodecRegistry(BindingRuntimeContext.create(
- SimpleStrategy.INSTANCE, context)).getCodecContext();
+ identityCodec = codecTree.getIdentityCodec();
+ codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class)));
+ legacyCodec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
- this.identityCodec = codecTree.getIdentityCodec();
- this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class)));
- this.legacyCodec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
+ } catch (Exception e) {
+ Throwables.throwIfUnchecked(e);
+ throw new IllegalStateException("Failed to build context for YangModuleLibrary", e);
+ }
}
@Override
return REVISION;
}
- private static YangTextSchemaSource createSource(final YangModuleInfo info) {
- final QName name = info.getName();
- return YangTextSchemaSource.delegateForByteSource(
- RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision()), info.getYangTextByteSource());
+ @Override
+ public YangLibraryContentBuilder newContentBuilder() {
+ return new YangLibraryContentBuilderImpl(codecTree);
}
}
*/
package org.opendaylight.mdsal.yanglib.rfc8525;
-import java.io.IOException;
+import com.google.common.annotations.Beta;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.kohsuke.MetaInfServices;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
import org.opendaylight.mdsal.yanglib.api.YangLibSupportFactory;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+@Beta
@MetaInfServices
@NonNullByDefault
public final class YangLibrarySupportFactory implements YangLibSupportFactory {
+
@Override
- public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory)
- throws YangParserException, IOException {
+ public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory) {
return new YangLibrarySupport(parserFactory);
}
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc8525;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.mdsal.yanglib.util.YanglibContextBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module.ConformanceType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class LegacyYangLibraryFormatTest {
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+ private YangLibrarySupport yangLib;
+
+ @Before
+ public void setUp() throws Exception {
+ final EffectiveModelContext modelContext =
+ YanglibContextBuilder.buildContext(YANG_PARSER_FACTORY, ModulesState.class);
+ runtimeContext = BindingRuntimeContext.create(new SimpleStrategy(), modelContext);
+ final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
+ codecTree = codecRegistry.create(runtimeContext);
+ yangLib = new YangLibrarySupport(YANG_PARSER_FACTORY);
+ }
+
+ @Test
+ public void testLegacyFormat() {
+ final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class));
+
+ final Optional<ContainerNode> legacyContent = yangLib.newContentBuilder()
+ .defaultContext((EffectiveModelContext)runtimeContext.getSchemaContext())
+ .includeLegacy()
+ .formatYangLibraryLegacyContent();
+
+ assertTrue(legacyContent.isPresent());
+
+ final ModulesState modulesState = legacyCodec.deserialize(legacyContent.orElseThrow());
+
+ assertEquals(4, modulesState.nonnullModule().size());
+ // migrate modulesState from codec generated class to proper binding for checking
+ final List<Module> valuesToCheck = modulesState.getModule().stream()
+ .map(module -> createModule(module.getName().getValue(), module.getNamespace().getValue(),
+ module.getRevision().getRevisionIdentifier().getValue()))
+ .collect(Collectors.toList());
+ assertTrue(createControlModules().containsAll(valuesToCheck));
+ }
+
+ private List<Module> createControlModules() {
+ return List.of(
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2019-01-04"),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"),
+ createModule("ietf-datastores", "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"),
+ createModule("ietf-yang-types", "urn:ietf:params:xml:ns:yang:ietf-yang-types", "2013-07-15"));
+ }
+
+ private static Module createModule(final String name, final String namespace, final String revision) {
+ return new ModuleBuilder()
+ .setName(new YangIdentifier(name))
+ .setNamespace(new Uri(namespace))
+ .setRevision(new Revision(new RevisionIdentifier(revision)))
+ .setConformanceType(ConformanceType.Implement)
+ .setFeature(Collections.emptyList())
+ .build();
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.yanglib.rfc8525;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.mdsal.yanglib.util.YanglibContextBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSet;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class YangLibrarySupportTest {
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private YangLibrarySupport yangLib;
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+
+ @Before
+ public void setUp() throws Exception {
+ final EffectiveModelContext modelContext =
+ YanglibContextBuilder.buildContext(YANG_PARSER_FACTORY, ModulesState.class);
+ runtimeContext = BindingRuntimeContext.create(new SimpleStrategy(), modelContext);
+ final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
+ codecTree = codecRegistry.create(runtimeContext);
+ yangLib = new YangLibrarySupport(YANG_PARSER_FACTORY);
+ }
+
+ @Test
+ public void testFormatSchema() {
+ final BindingDataObjectCodecTreeNode<YangLibrary> codec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class));
+
+ final ContainerNode nonLegacyContent = yangLib.newContentBuilder()
+ .defaultContext((EffectiveModelContext) runtimeContext.getSchemaContext()).formatYangLibraryContent();
+ final YangLibrary yangLibrary = codec.deserialize(nonLegacyContent);
+
+ assertEquals(1, yangLibrary.nonnullModuleSet().size());
+ final ModuleSet moduleSet = yangLibrary.nonnullModuleSet().get(0);
+ assertEquals(4, moduleSet.nonnullModule().size());
+ // migrate modulesState from codec generated class to proper binding for checking
+ final List<Module> valuesToCheck = moduleSet.getModule().stream()
+ .map(module -> createModule(module.getName().getValue(), module.getNamespace().getValue(),
+ module.getRevision().getValue()))
+ .collect(Collectors.toList());
+ assertTrue(createControlModules().containsAll(valuesToCheck));
+ }
+
+ private List<Module> createControlModules() {
+ return List.of(
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2019-01-04"),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"),
+ createModule("ietf-datastores", "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"),
+ createModule("ietf-yang-types", "urn:ietf:params:xml:ns:yang:ietf-yang-types", "2013-07-15"));
+ }
+
+ private Module createModule(final String name, final String namespace, final String revision) {
+ return new ModuleBuilder().setName(new YangIdentifier(name))
+ .setNamespace(new Uri(namespace))
+ .setRevision(new RevisionIdentifier(revision))
+ .setFeature(Collections.emptyList())
+ .setSubmodule(Collections.emptyList())
+ .build();
+ }
+}
\ No newline at end of file