Move MountPointNormalizedNodeWriter
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractMountPointContextFactory.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.yangtools.yang.data.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Iterator;
16 import java.util.stream.Collectors;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.rfc8528.model.api.MountPointLabel;
20 import org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
30 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContextFactory;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Abstract base class for MountPointContextFactory implementations, which can process RFC8525 mount point definitions.
38  */
39 @Beta
40 @NonNullByDefault
41 public abstract class AbstractMountPointContextFactory extends AbstractDynamicMountPointContextFactory {
42     /**
43      * Definition of a MountPoint, as known to RFC8528.
44      */
45     protected record MountPointDefinition(
46             MountPointLabel label,
47             boolean config,
48             // FIXME: make this return a set of XPath expressions
49             ImmutableSet<String> parentReferences) implements Immutable {
50         protected MountPointDefinition {
51             requireNonNull(parentReferences);
52             requireNonNull(label);
53         }
54     }
55
56     private static final Logger LOG = LoggerFactory.getLogger(AbstractMountPointContextFactory.class);
57     private static final NodeIdentifier SCHEMA_MOUNTS = NodeIdentifier.create(
58         QName.create(SchemaMountConstants.RFC8528_MODULE, "schema-mounts").intern());
59     private static final NodeIdentifier MOUNT_POINT = NodeIdentifier.create(
60         QName.create(SchemaMountConstants.RFC8528_MODULE, "mount-point").intern());
61     private static final NodeIdentifier CONFIG = NodeIdentifier.create(
62         QName.create(SchemaMountConstants.RFC8528_MODULE, "config").intern());
63     private static final NodeIdentifier MODULE = NodeIdentifier.create(
64         QName.create(SchemaMountConstants.RFC8528_MODULE, "module").intern());
65     private static final NodeIdentifier LABEL = NodeIdentifier.create(
66         QName.create(SchemaMountConstants.RFC8528_MODULE, "label").intern());
67     private static final NodeIdentifier SCHEMA_REF = NodeIdentifier.create(
68         QName.create(SchemaMountConstants.RFC8528_MODULE, "schema-ref").intern());
69     private static final NodeIdentifier INLINE = NodeIdentifier.create(
70         QName.create(SchemaMountConstants.RFC8528_MODULE, "inline").intern());
71     private static final NodeIdentifier SHARED_SCHEMA = NodeIdentifier.create(
72         QName.create(SchemaMountConstants.RFC8528_MODULE, "shared-schema").intern());
73     private static final NodeIdentifier PARENT_REFERENCE = NodeIdentifier.create(
74         QName.create(SchemaMountConstants.RFC8528_MODULE, "parent-reference").intern());
75
76     protected AbstractMountPointContextFactory(final MountPointLabel label) {
77         super(label);
78     }
79
80     @Override
81     protected final MountPointContext createMountPointContext(final EffectiveModelContext schemaContext,
82             final ContainerNode mountData) {
83         checkArgument(SCHEMA_MOUNTS.equals(mountData.getIdentifier()), "Unexpected top-level container %s", mountData);
84
85         final var mountPoint = mountData.childByArg(MOUNT_POINT);
86         if (mountPoint == null) {
87             LOG.debug("mount-point list not present in {}", mountData);
88             return MountPointContext.of(schemaContext);
89         }
90         if (!(mountPoint instanceof MapNode mapMountPoint)) {
91             throw new IllegalArgumentException("mount-point list " + mountPoint + " is not a MapNode");
92         }
93
94         return new ImmutableMountPointContext(schemaContext, mapMountPoint.body().stream().map(entry -> {
95             final String moduleName = entry.findChildByArg(MODULE).map(mod -> {
96                 checkArgument(mod instanceof LeafNode, "Unexpected module leaf %s", mod);
97                 final Object value = mod.body();
98                 checkArgument(value instanceof String, "Unexpected module leaf value %s", value);
99                 return (String) value;
100             }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry));
101             final Iterator<? extends Module> it = schemaContext.findModules(moduleName).iterator();
102             checkArgument(it.hasNext(), "Failed to find a module named %s", moduleName);
103             final QNameModule module = it.next().getQNameModule();
104
105             return new MountPointDefinition(
106                 MountPointLabel.create(QName.create(module, entry.findChildByArg(LABEL).map(lbl -> {
107                     checkArgument(lbl instanceof LeafNode, "Unexpected label leaf %s", lbl);
108                     final Object value = lbl.body();
109                     checkArgument(value instanceof String, "Unexpected label leaf value %s", value);
110                     return (String) value;
111                 }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry)))),
112                 entry.findChildByArg(CONFIG).map(cfg -> {
113                     checkArgument(cfg instanceof LeafNode, "Unexpected config leaf %s", cfg);
114                     final Object value = cfg.body();
115                     checkArgument(value instanceof Boolean, "Unexpected config leaf value %s", cfg);
116                     return (Boolean) value;
117                 }).orElse(Boolean.TRUE),
118                 getSchema(entry.findChildByArg(SCHEMA_REF)
119                     .orElseThrow(() -> new IllegalArgumentException("Missing schema-ref choice in " + entry))));
120         }).collect(Collectors.toList()), this::createContextFactory);
121     }
122
123     private static ImmutableSet<String> getSchema(final DataContainerChild child) {
124         checkArgument(child instanceof ChoiceNode, "Unexpected schema-ref choice %s", child);
125         final ChoiceNode schemaRef = (ChoiceNode) child;
126
127         return schemaRef.findChildByArg(SHARED_SCHEMA).map(sharedSchema -> {
128             checkArgument(sharedSchema instanceof ContainerNode, "Unexpected shared-schema container %s", sharedSchema);
129             return ((ContainerNode) sharedSchema).findChildByArg(PARENT_REFERENCE)
130                 // FIXME: 7.0.0: parse XPaths. Do we have enough context for that?
131                 .map(parentRef -> ImmutableSet.<String>of())
132                 .orElseGet(ImmutableSet::of);
133         }).orElseGet(() -> {
134             checkArgument(schemaRef.findChildByArg(INLINE).isPresent(), "Unhandled schema-ref type in %s", schemaRef);
135             return ImmutableSet.of();
136         });
137     }
138
139     /**
140      * Create a fresh {@link MountPointContextFactory} for a nested {@link MountPointDefinition}.
141      *
142      * @param mountPoint Mount point definition
143      * @return A new factory, dealing with mount points nested within the mount point.
144      */
145     protected abstract MountPointContextFactory createContextFactory(MountPointDefinition mountPoint);
146 }