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.rfc8528.data.api.MountPointContextFactory;
31 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
32 import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName;
33 import org.opendaylight.yangtools.rfc8528.data.util.AbstractMountPointContextFactory;
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.repo.api.RevisionSourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 final class MountPointContextFactoryImpl extends AbstractMountPointContextFactory {
43 private static final Logger LOG = LoggerFactory.getLogger(MountPointContextFactoryImpl.class);
45 private final BindingDataObjectCodecTreeNode<ModulesState> codec;
46 private final EffectiveModelContext yangLibContext;
47 private final SchemaContextResolver resolver;
49 MountPointContextFactoryImpl(final MountPointIdentifier mountId, final SchemaContextResolver resolver,
50 final EffectiveModelContext yangLibContext,
51 final BindingDataObjectCodecTreeNode<ModulesState> moduleStateCodec) {
53 this.resolver = requireNonNull(resolver);
54 this.yangLibContext = requireNonNull(yangLibContext);
55 this.codec = requireNonNull(moduleStateCodec);
59 protected MountPointContextFactory createContextFactory(final MountPointDefinition mountPoint) {
60 return new MountPointContextFactoryImpl(mountPoint.getIdentifier(), resolver, yangLibContext, codec);
64 protected Optional<EffectiveModelContext> findSchemaForLibrary(final ContainerName containerName) {
65 switch (containerName) {
67 return Optional.of(yangLibContext);
69 LOG.debug("Unhandled YANG library container {}", containerName);
70 return Optional.empty();
75 protected EffectiveModelContext bindLibrary(final ContainerName containerName, final ContainerNode libData)
76 throws YangParserException {
77 checkArgument(containerName == ContainerName.RFC7895, "Unsupported container type %s", containerName);
78 checkArgument(ModulesState.QNAME.equals(libData.getIdentifier().getNodeType()),
79 "Unexpected container %s", libData);
81 // DOM-to-Binding magic
82 return bindLibrary(verifyNotNull(codec.deserialize(libData)));
85 private @NonNull EffectiveModelContext bindLibrary(final @NonNull ModulesState modState)
86 throws YangParserException {
87 final List<SourceReference> requiredSources = new ArrayList<>();
88 final List<SourceReference> librarySources = new ArrayList<>();
90 for (Module module : modState.nonnullModule().values()) {
91 final SourceReference modRef = sourceRefFor(module, module.getSchema());
93 // TODO: take deviations/features into account
95 if (ConformanceType.Import == module.getConformanceType()) {
96 librarySources.add(modRef);
98 requiredSources.add(modRef);
101 for (Submodule submodule : module.nonnullSubmodule().values()) {
102 // Submodules go to library, as they are pulled in as needed
103 librarySources.add(sourceRefFor(submodule, submodule.getSchema()));
107 return resolver.resolveSchemaContext(librarySources, requiredSources);
110 private static SourceReference sourceRefFor(final CommonLeafs obj, final Uri uri) {
111 final SourceIdentifier sourceId = RevisionSourceIdentifier.create(obj.getName().getValue(),
112 RevisionUtils.toYangCommon(obj.getRevision()));
115 return SourceReference.of(sourceId, new URL(uri.getValue()));
116 } catch (MalformedURLException e) {
117 LOG.debug("Ignoring invalid schema location {}", uri, e);
121 return SourceReference.of(sourceId);