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.List;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
21 import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
22 import org.opendaylight.mdsal.yanglib.api.SourceReference;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.CommonLeafs;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module.ConformanceType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.Submodule;
30 import org.opendaylight.yangtools.rcf8528.data.util.AbstractMountPointContextFactory;
31 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
32 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
33 import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
38 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 final class MountPointContextFactoryImpl extends AbstractMountPointContextFactory {
44 private static final Logger LOG = LoggerFactory.getLogger(MountPointContextFactoryImpl.class);
46 private final BindingDataObjectCodecTreeNode<ModulesState> codec;
47 private final EffectiveModelContext yangLibContext;
48 private final SchemaContextResolver resolver;
50 MountPointContextFactoryImpl(final MountPointIdentifier mountId, final SchemaContextResolver resolver,
51 final EffectiveModelContext yangLibContext,
52 final BindingDataObjectCodecTreeNode<ModulesState> moduleStateCodec) {
54 this.resolver = requireNonNull(resolver);
55 this.yangLibContext = requireNonNull(yangLibContext);
56 this.codec = requireNonNull(moduleStateCodec);
60 protected MountPointContextFactory createContextFactory(final MountPointDefinition mountPoint) {
61 return new MountPointContextFactoryImpl(mountPoint.getIdentifier(), resolver, yangLibContext, codec);
65 protected Optional<SchemaContext> findSchemaForLibrary(final ContainerName containerName) {
66 switch (containerName) {
68 return Optional.of(yangLibContext);
70 LOG.debug("Unhandled YANG library container {}", containerName);
71 return Optional.empty();
76 protected SchemaContext bindLibrary(final ContainerName containerName, final ContainerNode libData)
77 throws YangParserException {
78 checkArgument(containerName == ContainerName.RFC7895, "Unsupported container type %s", containerName);
79 checkArgument(ModulesState.QNAME.equals(libData.getNodeType()), "Unexpected container %s", libData);
81 // DOM-to-Binding magic
82 return bindLibrary(verifyNotNull(codec.deserialize(libData)));
85 private @NonNull SchemaContext bindLibrary(final @NonNull ModulesState modState) throws YangParserException {
86 final List<SourceReference> requiredSources = new ArrayList<>();
87 final List<SourceReference> librarySources = new ArrayList<>();
89 for (Module module : modState.nonnullModule().values()) {
90 final SourceReference modRef = sourceRefFor(module, module.getSchema());
92 // TODO: take deviations/features into account
94 if (ConformanceType.Import == module.getConformanceType()) {
95 librarySources.add(modRef);
97 requiredSources.add(modRef);
100 for (Submodule submodule : module.nonnullSubmodule().values()) {
101 // Submodules go to library, as they are pulled in as needed
102 librarySources.add(sourceRefFor(submodule, submodule.getSchema()));
106 return resolver.resolveSchemaContext(librarySources, requiredSources);
109 private static SourceReference sourceRefFor(final CommonLeafs obj, final Uri uri) {
110 final SourceIdentifier sourceId = RevisionSourceIdentifier.create(obj.getName().getValue(),
111 RevisionUtils.toYangCommon(obj.getRevision()));
114 return SourceReference.of(sourceId, new URL(uri.getValue()));
115 } catch (MalformedURLException e) {
116 LOG.debug("Ignoring invalid schema location {}", uri, e);
120 return SourceReference.of(sourceId);