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