Add AbstractIdentifiable
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToNodes.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.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.collect.Iterables;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Optional;
17 import javax.xml.transform.dom.DOMSource;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40
41 /**
42  * Base strategy for converting an instance identifier into a normalized node structure.
43  * Use provided static methods for generic YangInstanceIdentifier -> NormalizedNode translation in ImmutableNodes.
44  */
45 abstract class InstanceIdToNodes<T extends PathArgument> extends AbstractIdentifiable<T> {
46     InstanceIdToNodes(final T identifier) {
47         super(identifier);
48     }
49
50     /**
51      * Build a strategy for the next path argument.
52      *
53      * @param child child identifier
54      * @return transformation strategy for a specific child
55      */
56     abstract InstanceIdToNodes<?> getChild(PathArgument child);
57
58     /**
59      * Convert instance identifier into a NormalizedNode structure.
60      *
61      * @param instanceId Instance identifier to transform into NormalizedNodes
62      * @param deepestChild Optional normalized node to be inserted as the last child
63      * @param operation Optional modify operation to be set on the last child
64      * @return NormalizedNode structure corresponding to submitted instance ID
65      */
66     abstract @NonNull NormalizedNode<?, ?> create(PathArgument first, Iterator<PathArgument> others,
67             Optional<NormalizedNode<?, ?>> deepestChild);
68
69     abstract boolean isMixin();
70
71     private static final class UnkeyedListMixinNormalization extends InstanceIdToCompositeNodes<NodeIdentifier> {
72         private final UnkeyedListItemNormalization innerNode;
73
74         UnkeyedListMixinNormalization(final ListSchemaNode list) {
75             super(NodeIdentifier.create(list.getQName()));
76             this.innerNode = new UnkeyedListItemNormalization(list);
77         }
78
79         @Override
80         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> createBuilder(final PathArgument compositeNode) {
81             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
82         }
83
84         @Override
85         InstanceIdToNodes<?> getChild(final PathArgument child) {
86             return child.getNodeType().equals(getIdentifier().getNodeType()) ? innerNode : null;
87         }
88
89         @Override
90         boolean isMixin() {
91             return true;
92         }
93     }
94
95     private static final class AnyXmlNormalization extends InstanceIdToNodes<NodeIdentifier> {
96         AnyXmlNormalization(final AnyXmlSchemaNode schema) {
97             super(NodeIdentifier.create(schema.getQName()));
98         }
99
100         @Override
101         InstanceIdToNodes<?> getChild(final PathArgument child) {
102             return null;
103         }
104
105         @Override
106         NormalizedNode<?, ?> create(final PathArgument first, final Iterator<PathArgument> others,
107                 final Optional<NormalizedNode<?, ?>> deepestChild) {
108             final NormalizedNodeBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder = Builders.anyXmlBuilder()
109                     .withNodeIdentifier(getIdentifier());
110             if (deepestChild.isPresent()) {
111                 final NormalizedNode<?, ?> child = deepestChild.get();
112                 checkState(child instanceof AnyXmlNode);
113                 builder.withValue(((AnyXmlNode) child).getValue());
114             }
115
116             return builder.build();
117         }
118
119         @Override
120         boolean isMixin() {
121             return false;
122         }
123     }
124
125     private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
126         final Optional<DataSchemaNode> potential = parent.findDataChildByName(child);
127         return potential.isPresent() ? potential : Optional.ofNullable(
128             findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child));
129     }
130
131     static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
132         final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
133         checkArgument(potential.isPresent(),
134                 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
135                 schema.getChildNodes());
136
137         final DataSchemaNode result = potential.get();
138         // We try to look up if this node was added by augmentation
139         if (schema instanceof DataSchemaNode && result.isAugmenting()) {
140             return fromAugmentation(schema, (AugmentationTarget) schema, result);
141         }
142         return fromDataSchemaNode(result);
143     }
144
145     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
146         for (final ChoiceSchemaNode choice : choices) {
147             for (final CaseSchemaNode caze : choice.getCases().values()) {
148                 if (findChildSchemaNode(caze, child).isPresent()) {
149                     return choice;
150                 }
151             }
152         }
153         return null;
154     }
155
156     /**
157      * Returns a SchemaPathUtil for provided child node
158      * <p/>
159      * If supplied child is added by Augmentation this operation returns
160      * a SchemaPathUtil for augmentation,
161      * otherwise returns a SchemaPathUtil for child as
162      * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
163      */
164     private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
165             final AugmentationTarget parentAug, final DataSchemaNode child) {
166         for (final AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
167             final Optional<DataSchemaNode> potential = aug.findDataChildByName(child.getQName());
168             if (potential.isPresent()) {
169                 return new InstanceIdToCompositeNodes.AugmentationNormalization(aug, parent);
170             }
171         }
172         return fromDataSchemaNode(child);
173     }
174
175     static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
176         if (potential instanceof ContainerSchemaNode) {
177             return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
178         } else if (potential instanceof ListSchemaNode) {
179             return fromListSchemaNode((ListSchemaNode) potential);
180         } else if (potential instanceof LeafSchemaNode) {
181             return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
182         } else if (potential instanceof ChoiceSchemaNode) {
183             return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
184         } else if (potential instanceof LeafListSchemaNode) {
185             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
186         } else if (potential instanceof AnyXmlSchemaNode) {
187             return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
188         }
189         return null;
190     }
191
192     private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
193         final List<QName> keyDefinition = potential.getKeyDefinition();
194         if (keyDefinition == null || keyDefinition.isEmpty()) {
195             return new UnkeyedListMixinNormalization(potential);
196         }
197         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential)
198                 : new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
199     }
200
201     private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
202         return potential.isUserOrdered() ? new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential)
203                 : new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);
204     }
205 }