54c64e93815a3408e49df5e0d251248f8f9c2065
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterables;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map.Entry;
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
138                 final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> anyXmlBuilder =
139                         Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier()).withValue(
140                             ((AnyXmlNode) child).getValue());
141                 addModifyOpIfPresent(operation, anyXmlBuilder);
142                 return anyXmlBuilder.build();
143             }
144
145             final NormalizedNodeAttrBuilder<NodeIdentifier, DOMSource, AnyXmlNode> builder =
146                     Builders.anyXmlBuilder().withNodeIdentifier(getIdentifier());
147             addModifyOpIfPresent(operation, builder);
148             return builder.build();
149         }
150
151         @Override
152         boolean isMixin() {
153             return false;
154         }
155     }
156
157     private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
158         DataSchemaNode potential = parent.getDataChildByName(child);
159         if (potential == null) {
160             potential = findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child);
161         }
162         return Optional.fromNullable(potential);
163     }
164
165     static InstanceIdToNodes<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
166         final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
167         Preconditions.checkArgument(potential.isPresent(),
168                 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
169                 schema.getChildNodes());
170
171         final DataSchemaNode result = potential.get();
172         // We try to look up if this node was added by augmentation
173         if (schema instanceof DataSchemaNode && result.isAugmenting()) {
174             return fromAugmentation(schema, (AugmentationTarget) schema, result);
175         }
176         return fromDataSchemaNode(result);
177     }
178
179     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
180         ChoiceSchemaNode foundChoice = null;
181         choiceLoop:
182         for (final ChoiceSchemaNode choice : choices) {
183             for (final ChoiceCaseNode caze : choice.getCases()) {
184                 if (findChildSchemaNode(caze, child).isPresent()) {
185                     foundChoice = choice;
186                     break choiceLoop;
187                 }
188             }
189         }
190         return foundChoice;
191     }
192
193     /**
194      * Returns a SchemaPathUtil for provided child node
195      * <p/>
196      * If supplied child is added by Augmentation this operation returns
197      * a SchemaPathUtil for augmentation,
198      * otherwise returns a SchemaPathUtil for child as
199      * call for {@link #fromDataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode)}.
200      */
201     private static InstanceIdToNodes<?> fromAugmentation(final DataNodeContainer parent,
202             final AugmentationTarget parentAug, final DataSchemaNode child) {
203         AugmentationSchema augmentation = null;
204         for (final AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
205             final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
206             if (potential != null) {
207                 augmentation = aug;
208                 break;
209             }
210
211         }
212         if (augmentation != null) {
213             return new InstanceIdToCompositeNodes.AugmentationNormalization(augmentation, parent);
214         }
215         return fromDataSchemaNode(child);
216     }
217
218     static InstanceIdToNodes<?> fromDataSchemaNode(final DataSchemaNode potential) {
219         if (potential instanceof ContainerSchemaNode) {
220             return new InstanceIdToCompositeNodes.ContainerTransformation((ContainerSchemaNode) potential);
221         } else if (potential instanceof ListSchemaNode) {
222             return fromListSchemaNode((ListSchemaNode) potential);
223         } else if (potential instanceof LeafSchemaNode) {
224             return new InstanceIdToSimpleNodes.LeafNormalization((LeafSchemaNode) potential);
225         } else if (potential instanceof ChoiceSchemaNode) {
226             return new InstanceIdToCompositeNodes.ChoiceNodeNormalization((ChoiceSchemaNode) potential);
227         } else if (potential instanceof LeafListSchemaNode) {
228             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
229         } else if (potential instanceof AnyXmlSchemaNode) {
230             return new AnyXmlNormalization((AnyXmlSchemaNode) potential);
231         }
232         return null;
233     }
234
235     private static InstanceIdToNodes<?> fromListSchemaNode(final ListSchemaNode potential) {
236         final List<QName> keyDefinition = potential.getKeyDefinition();
237         if (keyDefinition == null || keyDefinition.isEmpty()) {
238             return new UnkeyedListMixinNormalization(potential);
239         }
240         if (potential.isUserOrdered()) {
241             return new InstanceIdToCompositeNodes.OrderedMapMixinNormalization(potential);
242         }
243         return new InstanceIdToCompositeNodes.UnorderedMapMixinNormalization(potential);
244     }
245
246     private static InstanceIdToNodes<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
247         if (potential.isUserOrdered()) {
248             return new InstanceIdToCompositeNodes.OrderedLeafListMixinNormalization(potential);
249         }
250         return new InstanceIdToCompositeNodes.UnorderedLeafListMixinNormalization(potential);
251     }
252 }