Merge "Introduced skeletons of Contributor and User guide."
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextNode.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.util;
9
10
11 import com.google.common.collect.FluentIterable;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import javax.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
36
37 /**
38  * Schema derived data providing necessary information for mapping
39  * between {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode}
40  * and serialization format defined in RFC6020, since the mapping
41  * is not one-to-one.
42  *
43  * @param <T> Path Argument type
44  *
45  */
46 public abstract class DataSchemaContextNode<T extends PathArgument> implements Identifiable<T> {
47
48     private final T identifier;
49     private final DataSchemaNode dataSchemaNode;
50
51     @Override
52     public T getIdentifier() {
53         return identifier;
54     };
55
56     protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
57         super();
58         this.identifier = identifier;
59         if (schema instanceof DataSchemaNode) {
60             this.dataSchemaNode = (DataSchemaNode) schema;
61         } else {
62             this.dataSchemaNode = null;
63         }
64     }
65
66     public boolean isMixin() {
67         return false;
68     }
69
70     public boolean isKeyedEntry() {
71         return false;
72     }
73
74     protected Set<QName> getQNameIdentifiers() {
75         return Collections.singleton(identifier.getNodeType());
76     }
77
78     public abstract @Nullable DataSchemaContextNode<?> getChild(final PathArgument child);
79
80     public abstract @Nullable DataSchemaContextNode<?> getChild(QName child);
81
82     public abstract boolean isLeaf();
83
84
85     public @Nullable DataSchemaNode getDataSchemaNode() {
86         return dataSchemaNode;
87     }
88
89     static final DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
90         DataSchemaNode potential = parent.getDataChildByName(child);
91         if (potential == null) {
92             Iterable<ChoiceSchemaNode> choices = FluentIterable.from(
93                     parent.getChildNodes()).filter(ChoiceSchemaNode.class);
94             potential = findChoice(choices, child);
95         }
96         return potential;
97     }
98
99     static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
100         DataSchemaNode result = findChildSchemaNode(schema, child);
101         // We try to look up if this node was added by augmentation
102         if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
103             return fromAugmentation(schema, (AugmentationTarget) schema, result);
104         }
105         return fromDataSchemaNode(result);
106     }
107
108     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
109         ChoiceSchemaNode foundChoice = null;
110         choiceLoop: for (ChoiceSchemaNode choice : choices) {
111             for (ChoiceCaseNode caze : choice.getCases()) {
112                 if (findChildSchemaNode(caze, child) != null) {
113                     foundChoice = choice;
114                     break choiceLoop;
115                 }
116             }
117         }
118         return foundChoice;
119     }
120
121     public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
122         ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
123         for (DataSchemaNode child : augmentation.getChildNodes()) {
124             potentialChildren.add(child.getQName());
125         }
126         return new AugmentationIdentifier(potentialChildren.build());
127     }
128
129     static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation,
130             final DataNodeContainer schema) {
131         Set<DataSchemaNode> children = new HashSet<>();
132         for (DataSchemaNode augNode : augmentation.getChildNodes()) {
133             children.add(schema.getDataChildByName(augNode.getQName()));
134         }
135         return new EffectiveAugmentationSchema(augmentation, children);
136     }
137
138     /**
139      * Returns a DataContextNodeOperation for provided child node
140      *
141      * If supplied child is added by Augmentation this operation returns a
142      * DataContextNodeOperation for augmentation, otherwise returns a
143      * DataContextNodeOperation for child as call for
144      * {@link #fromDataSchemaNode(DataSchemaNode)}.
145      *
146      *
147      * @param parent
148      * @param parentAug
149      * @param child
150      * @return
151      */
152     static @Nullable DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
153             final AugmentationTarget parentAug, final DataSchemaNode child) {
154         AugmentationSchema augmentation = null;
155         for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
156             DataSchemaNode potential = aug.getDataChildByName(child.getQName());
157             if (potential != null) {
158                 augmentation = aug;
159                 break;
160             }
161         }
162         if (augmentation != null) {
163             return new AugmentationContextNode(augmentation, parent);
164         }
165         return fromDataSchemaNode(child);
166     }
167
168     public static @Nullable DataSchemaContextNode<?> fromDataSchemaNode(final DataSchemaNode potential) {
169         if (potential instanceof ContainerSchemaNode) {
170             return new ContainerContextNode((ContainerSchemaNode) potential);
171         } else if (potential instanceof ListSchemaNode) {
172             return fromListSchemaNode((ListSchemaNode) potential);
173         } else if (potential instanceof LeafSchemaNode) {
174             return new LeafContextNode((LeafSchemaNode) potential);
175         } else if (potential instanceof ChoiceSchemaNode) {
176             return new ChoiceNodeContextNode((ChoiceSchemaNode) potential);
177         } else if (potential instanceof LeafListSchemaNode) {
178             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
179         } else if (potential instanceof AnyXmlSchemaNode) {
180             return new AnyXmlContextNode((AnyXmlSchemaNode) potential);
181         }
182         return null;
183     }
184
185     private static DataSchemaContextNode<?> fromListSchemaNode(final ListSchemaNode potential) {
186         List<QName> keyDefinition = potential.getKeyDefinition();
187         if (keyDefinition == null || keyDefinition.isEmpty()) {
188             return new UnkeyedListMixinContextNode(potential);
189         }
190         if (potential.isUserOrdered()) {
191             return new OrderedMapMixinContextNode(potential);
192         }
193         return new UnorderedMapMixinContextNode(potential);
194     }
195
196     private static DataSchemaContextNode<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
197         if (potential.isUserOrdered()) {
198             return new OrderedLeafListMixinContextNode(potential);
199         }
200         return new UnorderedLeafListMixinContextNode(potential);
201     }
202
203     public static DataSchemaContextNode<?> from(final SchemaContext ctx) {
204         return new ContainerContextNode(ctx);
205     }
206 }