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();
}
<artifactId>mdsal-binding-dom-codec</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-generator-impl</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
--- /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.function.Function;
+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.toUnmodifiableMap(Submodule::key, Function.identity())))
+ .setFeature(module.getFeatures().stream()
+ .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
+ .collect(Collectors.toUnmodifiableList()))
+ .setConformanceType(Module.ConformanceType.Implement)
+ .build())
+ .collect(Collectors.toUnmodifiableMap(Module::key, Function.identity())));
+
+ 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 org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotBuilder;
import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
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;
private final BindingDataObjectCodecTreeNode<ModulesState> codec;
private final EffectiveModelContext context;
+ private final BindingCodecTree codecTree;
@Inject
public YangModuleLibrarySupport(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
.build();
context = snapshot.getEffectiveModelContext();
- final BindingCodecTree codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
+ codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
generator.generateTypeMapping(context), snapshot));
this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
public Revision implementedRevision() {
return REVISION;
}
+
+ @Override
+ public YangLibraryContentBuilder newContentBuilder() {
+ return new Rfc7895ContentBuilder(codecTree);
+ }
}
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import java.io.IOException;
import java.util.ServiceLoader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.kohsuke.MetaInfServices;
}
@Override
- public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory)
- throws YangParserException, IOException {
+ public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory) throws YangParserException {
return new YangModuleLibrarySupport(parserFactory, generator, codecFactory);
}
--- /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 java.util.Collections;
+import java.util.Map;
+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.DefaultBindingCodecTreeFactory;
+import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
+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.library.rev160621.module.list.ModuleKey;
+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.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class YangModuleLibrarySupportTest {
+
+ private static final BindingRuntimeGenerator BINDING_RUNTIME_GENERATOR = new DefaultBindingRuntimeGenerator();
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+ private YangModuleLibrarySupport yangLib;
+
+ @Before
+ public void setUp() throws YangParserException {
+ runtimeContext = BindingRuntimeHelpers.createRuntimeContext();
+ final DefaultBindingCodecTreeFactory codecFactory = new DefaultBindingCodecTreeFactory();
+ codecTree = codecFactory.create(runtimeContext);
+
+ yangLib = new YangModuleLibrarySupport(YANG_PARSER_FACTORY, BINDING_RUNTIME_GENERATOR, codecFactory);
+ }
+
+ @Test
+ public void testModulesState() {
+ final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class));
+ final ModulesState modulesState = legacyCodec.deserialize(
+ yangLib.newContentBuilder().defaultContext(
+ runtimeContext.getEffectiveModelContext()).formatYangLibraryContent());
+
+ assertEquals(3, modulesState.nonnullModule().size());
+ assertEquals(createControlModules(), modulesState.getModule());
+ }
+
+ private static Map<ModuleKey, Module> createControlModules() {
+ return Map.of(
+ new ModuleKey(new YangIdentifier("ietf-yang-library"), new Revision(new RevisionIdentifier("2016-06-21"))),
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2016-06-21"),
+ new ModuleKey(new YangIdentifier("ietf-inet-types"), new Revision(new RevisionIdentifier("2013-07-15"))),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"),
+ new ModuleKey(new YangIdentifier("ietf-yang-types"), new Revision(new RevisionIdentifier("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
<artifactId>mdsal-binding-dom-codec</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.opendaylight.mdsal</groupId>
+ <artifactId>mdsal-binding-generator-impl</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
--- /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.function.Function;
+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.Submodule;
+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.toUnmodifiableMap(Submodule::key, Function.identity())))
+ .setFeature(module.getFeatures().stream()
+ .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
+ .collect(Collectors.toUnmodifiableList()))
+ .setConformanceType(Module.ConformanceType.Implement)
+ .build())
+ .collect(Collectors.toUnmodifiableMap(Module::key, Function.identity())));
+
+ 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.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+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.Submodule;
+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.library.rev190104.yang.library.parameters.ModuleSetKey;
+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.toUnmodifiableMap(Module::key, Function.identity())))
+ .setName(MODULE_SET_NAME);
+ final ModuleSet moduleSet = moduleSetBuilder.build();
+
+ builder.setModuleSet(Map.of(new ModuleSetKey(moduleSet.getName()), 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.toUnmodifiableMap(Submodule::key, Function.identity())))
+ .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 org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotBuilder;
import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
+import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
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;
@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 YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
.build();
context = snapshot.getEffectiveModelContext();
- final BindingCodecTree codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
+ codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
generator.generateTypeMapping(context), snapshot));
this.identityCodec = codecTree.getIdentityCodec();
public Revision implementedRevision() {
return REVISION;
}
+
+ @Override
+ public YangLibraryContentBuilder newContentBuilder() {
+ return new YangLibraryContentBuilderImpl(codecTree);
+ }
}
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import java.io.IOException;
import java.util.ServiceLoader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.kohsuke.MetaInfServices;
}
@Override
- public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory)
- throws YangParserException, IOException {
+ public YangLibSupport createYangLibSupport(final YangParserFactory parserFactory) throws YangParserException {
return new YangLibrarySupport(parserFactory, generator, codecFactory);
}
--- /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.HashMap;
+import java.util.Map;
+import java.util.Optional;
+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.DefaultBindingCodecTreeFactory;
+import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
+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.library.rev190104.module.list.ModuleKey;
+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.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class LegacyYangLibraryFormatTest {
+
+ private static final BindingRuntimeGenerator BINDING_RUNTIME_GENERATOR = new DefaultBindingRuntimeGenerator();
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+ private YangLibrarySupport yangLib;
+
+ @Before
+ public void setUp() throws YangParserException {
+ runtimeContext = BindingRuntimeHelpers.createRuntimeContext();
+ final DefaultBindingCodecTreeFactory codecFactory = new DefaultBindingCodecTreeFactory();
+ yangLib = new YangLibrarySupport(YANG_PARSER_FACTORY, BINDING_RUNTIME_GENERATOR,
+ codecFactory);
+ codecTree = codecFactory.create(runtimeContext);
+ }
+
+ @Test
+ public void testLegacyFormat() {
+ final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class));
+
+ final Optional<ContainerNode> legacyContent = yangLib.newContentBuilder()
+ .defaultContext(runtimeContext.getEffectiveModelContext())
+ .includeLegacy()
+ .formatYangLibraryLegacyContent();
+
+ assertTrue(legacyContent.isPresent());
+
+ final ModulesState modulesState = legacyCodec.deserialize(legacyContent.orElseThrow());
+
+ assertEquals(4, modulesState.nonnullModule().size());
+ assertEquals(createControlModules(), modulesState.getModule());
+ }
+
+ private Map<ModuleKey, Module> createControlModules() {
+ final Map<ModuleKey, Module> modules = new HashMap<>();
+ modules.put(new ModuleKey(new YangIdentifier("ietf-yang-library"),
+ new Revision(new RevisionIdentifier("2019-01-04"))),
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2019-01-04"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-inet-types"),
+ new Revision(new RevisionIdentifier("2013-07-15"))),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-datastores"),
+ new Revision(new RevisionIdentifier("2018-02-14"))),
+ createModule("ietf-datastores", "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-yang-types"),
+ new Revision(new RevisionIdentifier("2013-07-15"))),
+ createModule("ietf-yang-types", "urn:ietf:params:xml:ns:yang:ietf-yang-types", "2013-07-15"));
+ return modules;
+ }
+
+ 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 java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+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.DefaultBindingCodecTreeFactory;
+import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
+import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
+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.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.ModuleKey;
+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.ModuleSetKey;
+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.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserFactoryImpl;
+
+public class YangLibrarySupportTest {
+
+ private static final BindingRuntimeGenerator BINDING_RUNTIME_GENERATOR = new DefaultBindingRuntimeGenerator();
+
+ private static final YangParserFactory YANG_PARSER_FACTORY = new YangParserFactoryImpl();
+
+ private YangLibrarySupport yangLib;
+ private BindingRuntimeContext runtimeContext;
+ private BindingCodecTree codecTree;
+
+ @Before
+ public void setUp() throws Exception {
+ runtimeContext = BindingRuntimeHelpers.createRuntimeContext();
+ final DefaultBindingCodecTreeFactory codecFactory = new DefaultBindingCodecTreeFactory();
+ yangLib = new YangLibrarySupport(YANG_PARSER_FACTORY, BINDING_RUNTIME_GENERATOR,
+ codecFactory);
+ codecTree = codecFactory.create(runtimeContext);
+ }
+
+ @Test
+ public void testFormatSchema() {
+ final BindingDataObjectCodecTreeNode<YangLibrary> codec =
+ codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class));
+
+ final ContainerNode nonLegacyContent = yangLib.newContentBuilder()
+ .defaultContext(runtimeContext.getEffectiveModelContext()).formatYangLibraryContent();
+ final YangLibrary yangLibrary = codec.deserialize(nonLegacyContent);
+
+ assertEquals(1, yangLibrary.nonnullModuleSet().size());
+ final ModuleSet moduleSet = yangLibrary.nonnullModuleSet().get(new ModuleSetKey("ODL_modules"));
+ assertEquals(4, moduleSet.nonnullModule().size());
+ assertEquals(moduleSet.getModule(), createControlModules());
+ }
+
+ private Map<ModuleKey, Module> createControlModules() {
+ final Map<ModuleKey, Module> modules = new HashMap<>();
+ modules.put(new ModuleKey(new YangIdentifier("ietf-yang-library")),
+ createModule("ietf-yang-library", "urn:ietf:params:xml:ns:yang:ietf-yang-library", "2019-01-04"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-inet-types")),
+ createModule("ietf-inet-types", "urn:ietf:params:xml:ns:yang:ietf-inet-types", "2013-07-15"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-datastores")),
+ createModule("ietf-datastores", "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
+ modules.put(new ModuleKey(new YangIdentifier("ietf-yang-types")),
+ createModule("ietf-yang-types", "urn:ietf:params:xml:ns:yang:ietf-yang-types", "2013-07-15"));
+ return modules;
+ }
+
+ 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.emptyMap())
+ .build();
+ }
+}
\ No newline at end of file