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