Examine supported features in yanglib
[mdsal.git] / yanglib / mdsal-yanglib-rfc7895 / src / main / java / org / opendaylight / mdsal / yanglib / rfc7895 / MountPointContextFactoryImpl.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.yanglib.rfc7895;
9
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;
13
14 import java.net.MalformedURLException;
15 import java.net.URL;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.Set;
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;
45
46 final class MountPointContextFactoryImpl extends AbstractMountPointContextFactory {
47     private static final Logger LOG = LoggerFactory.getLogger(MountPointContextFactoryImpl.class);
48
49     private final BindingDataObjectCodecTreeNode<ModulesState> codec;
50     private final EffectiveModelContext yangLibContext;
51     private final SchemaContextResolver resolver;
52
53     MountPointContextFactoryImpl(final MountPointIdentifier mountId, final SchemaContextResolver resolver,
54             final EffectiveModelContext yangLibContext,
55             final BindingDataObjectCodecTreeNode<ModulesState> moduleStateCodec) {
56         super(mountId);
57         this.resolver = requireNonNull(resolver);
58         this.yangLibContext = requireNonNull(yangLibContext);
59         codec = requireNonNull(moduleStateCodec);
60     }
61
62     @Override
63     protected MountPointContextFactory createContextFactory(final MountPointDefinition mountPoint) {
64         return new MountPointContextFactoryImpl(mountPoint.getIdentifier(), resolver, yangLibContext, codec);
65     }
66
67     @Override
68     protected Optional<EffectiveModelContext> findSchemaForLibrary(final ContainerName containerName) {
69         switch (containerName) {
70             case RFC7895:
71                 return Optional.of(yangLibContext);
72             default:
73                 LOG.debug("Unhandled YANG library container {}", containerName);
74                 return Optional.empty();
75         }
76     }
77
78     @Override
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);
84
85         // DOM-to-Binding magic
86         return bindLibrary(verifyNotNull(codec.deserialize(libData)));
87     }
88
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<>();
94
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());
100             }
101
102             // TODO: take deviations into account
103
104             if (ConformanceType.Import == module.getConformanceType()) {
105                 librarySources.add(modRef);
106             } else {
107                 requiredSources.add(modRef);
108             }
109
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()));
113             }
114         }
115
116         return resolver.resolveSchemaContext(librarySources, requiredSources, supportedFeatures);
117     }
118
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()));
122         if (uri != null) {
123             try {
124                 return SourceReference.of(sourceId, new URL(uri.getValue()));
125             } catch (MalformedURLException e) {
126                 LOG.debug("Ignoring invalid schema location {}", uri, e);
127             }
128         }
129
130         return SourceReference.of(sourceId);
131     }
132 }