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