From 9642f37f7a23be0206d2f04a7c160c245a0b43ed Mon Sep 17 00:00:00 2001 From: "matus.matok" Date: Thu, 6 Apr 2023 14:27:49 +0200 Subject: [PATCH] Propagate supported features from ModuleInfoSnapshotBuilder Allow a FeatureSet to be created from a collected Module/Set mapping, allowing each reported module to be registered in ModuleInfoSnapshotBuilder. These are then transformed into a FeatureSet and passed down to parser. JIRA: MDSAL-789 Change-Id: I7837fb1debf2ebff3382422b232f047ec05316c1 Signed-off-by: matus.matok Signed-off-by: Robert Varga --- .../spi/ModuleInfoSnapshotBuilder.java | 30 ++++++++++++ .../spi/ModuleInfoSnapshotBuilderTest.java | 47 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 binding/mdsal-binding-runtime-spi/src/test/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilderTest.java diff --git a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilder.java b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilder.java index f716448cb8..83c7a2b001 100644 --- a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilder.java +++ b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilder.java @@ -11,6 +11,12 @@ import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.base.Throwables; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimaps; +import com.google.common.collect.SetMultimap; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; @@ -20,8 +26,13 @@ import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.yang.binding.BindingObject; +import org.opendaylight.yangtools.yang.binding.DataRoot; +import org.opendaylight.yangtools.yang.binding.YangFeature; import org.opendaylight.yangtools.yang.binding.YangModuleInfo; import org.opendaylight.yangtools.yang.binding.contract.Naming; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.model.repo.api.FeatureSet; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.opendaylight.yangtools.yang.parser.api.YangParser; @@ -31,6 +42,7 @@ import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException; @Beta public final class ModuleInfoSnapshotBuilder { + private final SetMultimap, YangFeature> moduleFeatures = HashMultimap.create(); private final Set moduleInfos = new HashSet<>(); private final YangParserFactory parserFactory; @@ -78,6 +90,12 @@ public final class ModuleInfoSnapshotBuilder { return this; } + public @NonNull ModuleInfoSnapshotBuilder addModuleFeatures(final Class module, + final Set> supportedFeatures) { + moduleFeatures.putAll(requireNonNull(module), ImmutableList.copyOf(supportedFeatures)); + return this; + } + /** * Build {@link ModuleInfoSnapshot} from all {@code moduleInfos} in this builder. * @@ -86,6 +104,7 @@ public final class ModuleInfoSnapshotBuilder { */ public @NonNull ModuleInfoSnapshot build() throws YangParserException { final YangParser parser = parserFactory.createParser(); + final Map mappedInfos = new HashMap<>(); final Map classLoaders = new HashMap<>(); for (YangModuleInfo info : moduleInfos) { @@ -103,6 +122,17 @@ public final class ModuleInfoSnapshotBuilder { } } + if (!moduleFeatures.isEmpty()) { + final var featuresByModule = + ImmutableMap.>builderWithExpectedSize(moduleFeatures.size()); + for (var entry : Multimaps.asMap(moduleFeatures).entrySet()) { + featuresByModule.put(BindingReflections.getQNameModule(entry.getKey()), + entry.getValue().stream().map(YangFeature::qname).map(QName::getLocalName).sorted() + .collect(ImmutableSet.toImmutableSet())); + } + parser.setSupportedFeatures(new FeatureSet(featuresByModule.build())); + } + return new DefaultModuleInfoSnapshot(parser.buildEffectiveModel(), mappedInfos, classLoaders); } } diff --git a/binding/mdsal-binding-runtime-spi/src/test/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilderTest.java b/binding/mdsal-binding-runtime-spi/src/test/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilderTest.java new file mode 100644 index 0000000000..69306a65fc --- /dev/null +++ b/binding/mdsal-binding-runtime-spi/src/test/java/org/opendaylight/mdsal/binding/runtime/spi/ModuleInfoSnapshotBuilderTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 PANTHEON.tech s.r.o. 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.binding.runtime.spi; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; +import java.util.Set; +import org.junit.Test; +import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot; +import org.opendaylight.yang.gen.v1.mdsal767.norev.$YangModuleInfoImpl; +import org.opendaylight.yang.gen.v1.mdsal767.norev.Mdsal767Data; +import org.opendaylight.yang.gen.v1.mdsal767.norev.One$F; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.XMLNamespace; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; +import org.opendaylight.yangtools.yang.parser.api.YangParserException; +import org.opendaylight.yangtools.yang.parser.api.YangParserFactory; +import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory; + +public class ModuleInfoSnapshotBuilderTest { + private static final YangParserFactory PARSER_FACTORY = new DefaultYangParserFactory(); + + @Test + public void testModuleRegistration() throws YangParserException { + final ModuleInfoSnapshotBuilder snapshotBuilder = new ModuleInfoSnapshotBuilder(PARSER_FACTORY); + snapshotBuilder.add($YangModuleInfoImpl.getInstance()); + snapshotBuilder.addModuleFeatures(Mdsal767Data.class, Set.of(One$F.VALUE)); + + final ModuleInfoSnapshot snapshot = snapshotBuilder.build(); + final EffectiveModelContext modelContext = snapshot.getEffectiveModelContext(); + final Map modules = modelContext.getModuleStatements(); + final ModuleEffectiveStatement module = modules.get(QNameModule.create(XMLNamespace.of("mdsal767"))); + assertEquals(1, module.features().size()); + final FeatureEffectiveStatement feature = module.features().stream().findAny().orElseThrow(); + assertEquals(QName.create("mdsal767", "one"), feature.argument()); + } +} + -- 2.36.6