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