Bump to odlparent-9.0.0/yangtools-7.0.1-SNAPSHOT
[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.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;
41
42 final class MountPointContextFactoryImpl extends AbstractMountPointContextFactory {
43     private static final Logger LOG = LoggerFactory.getLogger(MountPointContextFactoryImpl.class);
44
45     private final BindingDataObjectCodecTreeNode<ModulesState> codec;
46     private final EffectiveModelContext yangLibContext;
47     private final SchemaContextResolver resolver;
48
49     MountPointContextFactoryImpl(final MountPointIdentifier mountId, final SchemaContextResolver resolver,
50             final EffectiveModelContext yangLibContext,
51             final BindingDataObjectCodecTreeNode<ModulesState> moduleStateCodec) {
52         super(mountId);
53         this.resolver = requireNonNull(resolver);
54         this.yangLibContext = requireNonNull(yangLibContext);
55         this.codec = requireNonNull(moduleStateCodec);
56     }
57
58     @Override
59     protected MountPointContextFactory createContextFactory(final MountPointDefinition mountPoint) {
60         return new MountPointContextFactoryImpl(mountPoint.getIdentifier(), resolver, yangLibContext, codec);
61     }
62
63     @Override
64     protected Optional<EffectiveModelContext> findSchemaForLibrary(final ContainerName containerName) {
65         switch (containerName) {
66             case RFC7895:
67                 return Optional.of(yangLibContext);
68             default:
69                 LOG.debug("Unhandled YANG library container {}", containerName);
70                 return Optional.empty();
71         }
72     }
73
74     @Override
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);
80
81         // DOM-to-Binding magic
82         return bindLibrary(verifyNotNull(codec.deserialize(libData)));
83     }
84
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<>();
89
90         for (Module module : modState.nonnullModule().values()) {
91             final SourceReference modRef = sourceRefFor(module, module.getSchema());
92
93             // TODO: take deviations/features into account
94
95             if (ConformanceType.Import == module.getConformanceType()) {
96                 librarySources.add(modRef);
97             } else {
98                 requiredSources.add(modRef);
99             }
100
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()));
104             }
105         }
106
107         return resolver.resolveSchemaContext(librarySources, requiredSources);
108     }
109
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()));
113         if (uri != null) {
114             try {
115                 return SourceReference.of(sourceId, new URL(uri.getValue()));
116             } catch (MalformedURLException e) {
117                 LOG.debug("Ignoring invalid schema location {}", uri, e);
118             }
119         }
120
121         return SourceReference.of(sourceId);
122     }
123 }