2 * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.yanglib.rfc7895;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import java.net.MalformedURLException;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
23 import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
24 import org.opendaylight.mdsal.yanglib.api.SourceReference;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module.ConformanceType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.Submodule;
32 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
33 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
34 import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName;
35 import org.opendaylight.yangtools.rfc8528.data.util.AbstractMountPointContextFactory;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.XMLNamespace;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
41 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
42 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 final class MountPointContextFactoryImpl extends AbstractMountPointContextFactory {
47 private static final Logger LOG = LoggerFactory.getLogger(MountPointContextFactoryImpl.class);
49 private final BindingDataObjectCodecTreeNode<ModulesState> codec;
50 private final EffectiveModelContext yangLibContext;
51 private final SchemaContextResolver resolver;
53 MountPointContextFactoryImpl(final MountPointIdentifier mountId, final SchemaContextResolver resolver,
54 final EffectiveModelContext yangLibContext,
55 final BindingDataObjectCodecTreeNode<ModulesState> moduleStateCodec) {
57 this.resolver = requireNonNull(resolver);
58 this.yangLibContext = requireNonNull(yangLibContext);
59 codec = requireNonNull(moduleStateCodec);
63 protected MountPointContextFactory createContextFactory(final MountPointDefinition mountPoint) {
64 return new MountPointContextFactoryImpl(mountPoint.getIdentifier(), resolver, yangLibContext, codec);
68 protected Optional<EffectiveModelContext> findSchemaForLibrary(final ContainerName containerName) {
69 switch (containerName) {
71 return Optional.of(yangLibContext);
73 LOG.debug("Unhandled YANG library container {}", containerName);
74 return Optional.empty();
79 protected EffectiveModelContext bindLibrary(final ContainerName containerName, final ContainerNode libData)
80 throws YangParserException {
81 checkArgument(containerName == ContainerName.RFC7895, "Unsupported container type %s", containerName);
82 checkArgument(ModulesState.QNAME.equals(libData.getIdentifier().getNodeType()),
83 "Unexpected container %s", libData);
85 // DOM-to-Binding magic
86 return bindLibrary(verifyNotNull(codec.deserialize(libData)));
89 private @NonNull EffectiveModelContext bindLibrary(final @NonNull ModulesState modState)
90 throws YangParserException {
91 final List<SourceReference> requiredSources = new ArrayList<>();
92 final List<SourceReference> librarySources = new ArrayList<>();
93 final Set<QName> supportedFeatures = new HashSet<>();
95 for (Module module : modState.nonnullModule().values()) {
96 final SourceReference modRef = sourceRefFor(module, module.getSchema());
97 final var namespace = XMLNamespace.of(module.requireNamespace().getValue());
98 for (var feature : module.requireFeature()) {
99 supportedFeatures.add(QName.create(namespace, feature.getValue()).intern());
102 // TODO: take deviations into account
104 if (ConformanceType.Import == module.getConformanceType()) {
105 librarySources.add(modRef);
107 requiredSources.add(modRef);
110 for (Submodule submodule : module.nonnullSubmodule().values()) {
111 // Submodules go to library, as they are pulled in as needed
112 librarySources.add(sourceRefFor(submodule, submodule.getSchema()));
116 return resolver.resolveSchemaContext(librarySources, requiredSources, supportedFeatures);
119 private static SourceReference sourceRefFor(final CommonLeafs obj, final Uri uri) {
120 final SourceIdentifier sourceId = RevisionSourceIdentifier.create(obj.getName().getValue(),
121 RevisionUtils.toYangCommon(obj.getRevision()));
124 return SourceReference.of(sourceId, new URL(uri.getValue()));
125 } catch (MalformedURLException e) {
126 LOG.debug("Ignoring invalid schema location {}", uri, e);
130 return SourceReference.of(sourceId);