Cleanup use of Guava library
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Iterables;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.Optional;
16 import javax.xml.transform.dom.DOMSource;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.AttributesBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41
42 /**
43  * Base strategy for converting an instance identifier into a normalized node structure.
44  * Use provided static methods for generic YangInstanceIdentifier -> NormalizedNode translation in ImmutableNodes.
45  */
46 abstract class InstanceIdToNodes<T extends PathArgument> implements Identifiable<T> {
47
48     private final T identifier;
49
50     @Override
51     public final T getIdentifier() {
52         return identifier;
53     }
54
55     protected InstanceIdToNodes(final T identifier) {
56         this.identifier = identifier;
57     }
58
59     /**
60      * Build a strategy for the next path argument.
61      *
62      * @param child child identifier
63      * @return transformation strategy for a specific child
64      */
65     abstract InstanceIdToNodes<?> getChild(PathArgument child);
66
67     /**
68      * Convert instance identifier into a NormalizedNode structure.
69      *
70      * @param instanceId Instance identifier to transform into NormalizedNodes
71      * @param deepestChild Optional normalized node to be inserted as the last child
72      * @param operation Optional modify operation to be set on the last child
73      * @return NormalizedNode structure corresponding to submitted instance ID
74      */
75     abstract NormalizedNode<?, ?> create(YangInstanceIdentifier instanceId, Optional<NormalizedNode<?, ?>> deepestChild,
76             Optional<Entry<QName,ModifyAction>> operation);
77
78     abstract boolean isMixin();
79
80     public void addModifyOpIfPresent(final Optional<Entry<QName,ModifyAction>> operation,
81             final AttributesBuilder<?> builder) {
82         if (operation.isPresent()) {
83             builder.withAttributes(Collections.singletonMap(operation.get().getKey(),
84                 modifyOperationToXmlString(operation.get().getValue())));
85         }
86     }
87
88     public static String modifyOperationToXmlString(final ModifyAction operation) {
89         return operation.name().toLowerCase();
90     }
91
92     private static final class UnkeyedListMixinNormalization extends InstanceIdToCompositeNodes<NodeIdentifier> {
93         private final UnkeyedListItemNormalization innerNode;
94
95         UnkeyedListMixinNormalization(final ListSchemaNode list) {
96             super(NodeIdentifier.create(list.getQName()));
97             this.innerNode = new UnkeyedListItemNormalization(list);
98         }
99
100         @Override
101         protected CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> createBuilder(
102                 final PathArgument compositeNode) {
103             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
104         }
105
106         @Override
107         public InstanceIdToNodes<?> getChild(final PathArgument child) {
108             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
109                 return innerNode;
110             }
111             return null;
112         }
113
114         @Override
115         boolean isMixin() {
116             return true;
117         }
118     }
119
120     private static class AnyXmlNormalization extends InstanceIdToNodes<NodeIdentifier> {
121         AnyXmlNormalization(final AnyXmlSchemaNode schema) {
122             super(NodeIdentifier.create(schema.getQName()));
123         }
124
125         @Override
126         public InstanceIdToNodes<?> getChild(final PathArgument child) {
127             return null;
128         }
129
130         @Override
131         public NormalizedNode<?, ?> create(final YangInstanceIdentifier instanceId,
132                 final Optional<NormalizedNode<?, ?>> deepestChild,
133                 final Optional<Entry<QName,ModifyAction>> operation) {
134             if (deepestChild.isPresent()) {
135                 final NormalizedNode<?, ?> child = deepestChild.get();
136                 Preconditions.checkState(child instanceof AnyXmlNode);
137                 final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder =
138                         Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier()).withValue(
139                             ((AnyXmlNode) child).getValue());
140                 addModifyOpIfPresent(operation, anyXmlBuilder);
141                 return anyXmlBuilder.build();
142             }
143
144             final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder =
145                     Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier());
146             addModifyOpIfPresent(operation, builder);
147             return builder.build();
148         }
149
150         @Override
151         boolean isMixin() {
152             return false;
153         }
154     }
155
156     private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
157         DataSchemaNode potential = parent.getDataChildByName(child);
158         if (potential == null) {
159             potential = findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child);
160         }
161         return Optional.ofNullable(potential);
162     }
163
164     static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
165         final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
166         Preconditions.checkArgument(potential.isPresent(),
167                 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
168                 schema.getChildNodes());
169
170         final DataSchemaNode result = potential.get();
171         // We try to look up if this node was added by augmentation
172         if (schema instanceof DataSchemaNode && result.isAugmenting()) {
173             return fromAugmentation(schema, (AugmentationTarget) schema, result);
174         }
175         return fromDataSchemaNode(result);
176     }
177
178     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
179         ChoiceSchemaNode foundChoice = null;
180         choiceLoop:
181         for (final ChoiceSchemaNode choice : choices) {
182             for (final ChoiceCaseNode caze : choice.getCases()) {
183                 if (findChildSchemaNode(caze, child).isPresent()) {
184                     foundChoice = choice;
185                     break choiceLoop;
186                 }
187             }
188         }
189         return foundChoice;
190     }
191
192     /**
193      * Returns a SchemaPathUtil for provided child node
194      * <p/>
195      * If supplied child is added by Augmentation this operation returns
196      * a SchemaPathUtil for augmentation,
197      * otherwise returns a SchemaPathUtil for child as
198      * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
199      */
200     private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
201             final AugmentationTarget parentAug, final DataSchemaNode child) {
202         AugmentationSchema augmentation = null;
203         for (final AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
204             final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
205             if (potential != null) {
206                 augmentation = aug;
207                 break;
208             }
209
210         }
211         if (augmentation != null) {
212             return new InstanceIdToCompositeNodes.AugmentationNormalization(augmentation, parent);
213         }
214         return fromDataSchemaNode(child);
215     }
216
217     static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
218         if (potential instanceof ContainerSchemaNode) {
219             return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
220         } else if (potential instanceof ListSchemaNode) {
221             return fromListSchemaNode((ListSchemaNode) potential);
222         } else if (potential instanceof LeafSchemaNode) {
223             return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
224         } else if (potential instanceof ChoiceSchemaNode) {
225             return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
226         } else if (potential instanceof LeafListSchemaNode) {
227             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
228         } else if (potential instanceof AnyXmlSchemaNode) {
229             return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
230         }
231         return null;
232     }
233
234     private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
235         final List<QName> keyDefinition = potential.getKeyDefinition();
236         if (keyDefinition == null || keyDefinition.isEmpty()) {
237             return new UnkeyedListMixinNormalization(potential);
238         }
239         if (potential.isUserOrdered()) {
240             return new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential);
241         }
242         return new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
243     }
244
245     private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
246         if (potential.isUserOrdered()) {
247             return new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential);
248         }
249         return new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);
250     }
251 }