Remove DataSchemaContextNode.augmentationProxy()
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextNode.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.Iterables;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.Set;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
26 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36
37 /**
38  * Schema derived data providing necessary information for mapping between
39  * {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode} and serialization format defined in RFC6020,
40  * since the mapping is not one-to-one.
41  *
42  * @param <T> Path Argument type
43  */
44 public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
45     private final DataSchemaNode dataSchemaNode;
46     private final T identifier;
47
48     protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
49         this.identifier = identifier;
50         if (schema instanceof DataSchemaNode) {
51             this.dataSchemaNode = (DataSchemaNode) schema;
52         } else {
53             this.dataSchemaNode = null;
54         }
55     }
56
57     @Override
58     public T getIdentifier() {
59         return identifier;
60     }
61
62     public boolean isMixin() {
63         return false;
64     }
65
66     public boolean isKeyedEntry() {
67         return false;
68     }
69
70     public abstract boolean isLeaf();
71
72     protected Set<QName> getQNameIdentifiers() {
73         return ImmutableSet.of(identifier.getNodeType());
74     }
75
76     /**
77      * Find a child node identifier by its {@link PathArgument}.
78      *
79      * @param child Child path argument
80      * @return A child node, or null if not found
81      */
82     public abstract @Nullable DataSchemaContextNode<?> getChild(PathArgument child);
83
84     public abstract @Nullable DataSchemaContextNode<?> getChild(QName child);
85
86     public @Nullable DataSchemaNode getDataSchemaNode() {
87         return dataSchemaNode;
88     }
89
90     /**
91      * Find a child node as identified by a {@link YangInstanceIdentifier} relative to this node.
92      *
93      * @param path Path towards the child node
94      * @return Child node if present, or empty when corresponding child is not found.
95      * @throws NullPointerException if {@code path} is null
96      */
97     public final @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(
98             final @NonNull YangInstanceIdentifier path) {
99         DataSchemaContextNode<?> currentOp = this;
100         for (PathArgument arg : path.getPathArguments()) {
101             currentOp = currentOp.getChild(arg);
102             if (currentOp == null) {
103                 return Optional.empty();
104             }
105         }
106         return Optional.of(currentOp);
107     }
108
109     static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
110         final DataSchemaNode potential = parent.getDataChildByName(child);
111         return potential == null ? findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child)
112                 : potential;
113     }
114
115     static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
116         final DataSchemaNode result = findChildSchemaNode(schema, child);
117         // We try to look up if this node was added by augmentation
118         if (result != null && schema instanceof DataSchemaNode && result.isAugmenting()) {
119             return fromAugmentation(schema, (AugmentationTarget) schema, result);
120         }
121         return fromDataSchemaNode(result);
122     }
123
124     // FIXME: this looks like it should be a Predicate on a stream with findFirst()
125     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
126         for (ChoiceSchemaNode choice : choices) {
127             // FIXME: this looks weird: what are we looking for again?
128             for (CaseSchemaNode caze : choice.getCases().values()) {
129                 if (findChildSchemaNode(caze, child) != null) {
130                     return choice;
131                 }
132             }
133         }
134         return null;
135     }
136
137     /**
138      * Create AugmentationIdentifier from an AugmentationSchemaNode.
139      *
140      * @param schema Augmentation schema
141      * @return AugmentationIdentifier for the schema
142      * @throws NullPointerException if {@code schema} is null
143      */
144     public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode schema) {
145         return new AugmentationIdentifier(schema.getChildNodes().stream().map(DataSchemaNode::getQName)
146             .collect(Collectors.toSet()));
147     }
148
149     /**
150      * Returns a DataContextNodeOperation for provided child node
151      *
152      * <p>
153      * If supplied child is added by Augmentation this operation returns a
154      * DataContextNodeOperation for augmentation, otherwise returns a
155      * DataContextNodeOperation for child as call for
156      * {@link #fromDataSchemaNode(DataSchemaNode)}.
157      */
158     static @Nullable DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
159             final AugmentationTarget parentAug, final DataSchemaNode child) {
160         for (AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
161             if (aug.findDataChildByName(child.getQName()).isPresent()) {
162                 return new AugmentationContextNode(aug, parent);
163             }
164         }
165         return fromDataSchemaNode(child);
166     }
167
168     public static @Nullable DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
169         if (potential instanceof ContainerSchemaNode) {
170             return new ContainerContextNode((ContainerSchemaNode) potential);
171         } else if (potential instanceof ListSchemaNode) {
172             return fromListSchemaNode((ListSchemaNode) potential);
173         } else if (potential instanceof LeafSchemaNode) {
174             return new LeafContextNode((LeafSchemaNode) potential);
175         } else if (potential instanceof ChoiceSchemaNode) {
176             return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
177         } else if (potential instanceof LeafListSchemaNode) {
178             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
179         } else if (potential instanceof AnyXmlSchemaNode) {
180             return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
181         }
182         return null;
183     }
184
185     private static DataSchemaContextNode<?> fromListSchemaNode(final ListSchemaNode potential) {
186         List<QName> keyDefinition = potential.getKeyDefinition();
187         if (keyDefinition == null || keyDefinition.isEmpty()) {
188             return new UnkeyedListMixinContextNode(potential);
189         }
190         if (potential.isUserOrdered()) {
191             return new OrderedMapMixinContextNode(potential);
192         }
193         return new UnorderedMapMixinContextNode(potential);
194     }
195
196     private static DataSchemaContextNode<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
197         if (potential.isUserOrdered()) {
198             return new OrderedLeafListMixinContextNode(potential);
199         }
200         return new UnorderedLeafListMixinContextNode(potential);
201     }
202
203     public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
204         return new ContainerContextNode(ctx);
205     }
206 }