313adbcd530d81cad7c3603b252d0666993283ca
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / SchemaTracker.java
1 /*
2  * Copyright (c) 2014 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.codec;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Predicate;
14 import com.google.common.collect.Iterables;
15 import java.io.IOException;
16 import java.util.ArrayDeque;
17 import java.util.Deque;
18 import java.util.HashSet;
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.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
25 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
43 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Utility class for tracking the underlying state of the underlying
49  * schema node.
50  */
51 @Beta
52 public final class SchemaTracker {
53     private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
54     private final Deque<Object> schemaStack = new ArrayDeque<>();
55     private final DataNodeContainer root;
56
57     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
58         SchemaNode current = SchemaUtils.findParentSchemaOnPath(context, path);
59         Preconditions.checkArgument(current instanceof DataNodeContainer,"Schema path must point to container or list or an rpc input/output. Supplied path %s pointed to: %s",path,current);
60         root = (DataNodeContainer) current;
61     }
62
63     /**
64      * Create a new writer with the specified context as its root.
65      *
66      * @param context Associated {@link SchemaContext}.
67      * @return A new {@link NormalizedNodeStreamWriter}
68      */
69     public static SchemaTracker create(final SchemaContext context) {
70         return create(context, SchemaPath.ROOT);
71     }
72
73     /**
74      * Create a new writer with the specified context and rooted in the specified schema path
75      *
76      * @param context Associated {@link SchemaContext}
77      * @param path schema path
78      *
79      * @return A new {@link NormalizedNodeStreamWriter}
80      */
81     public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
82         return new SchemaTracker(context, path);
83     }
84
85     public Object getParent() {
86         if (schemaStack.isEmpty()) {
87             return root;
88         }
89         return schemaStack.peek();
90     }
91
92     private SchemaNode getSchema(final PathArgument name) {
93         final Object parent = getParent();
94         SchemaNode schema = null;
95         final QName qname = name.getNodeType();
96         if(parent instanceof DataNodeContainer) {
97             schema = ((DataNodeContainer)parent).getDataChildByName(qname);
98
99             if(schema == null && parent instanceof GroupingDefinition) {
100                 schema = ((GroupingDefinition) parent);
101             }
102
103             if(schema == null && parent instanceof NotificationDefinition) {
104                 schema = ((NotificationDefinition) parent);
105             }
106         } else if(parent instanceof ChoiceSchemaNode) {
107             schema = findChildInCases((ChoiceSchemaNode) parent, qname);
108         } else {
109             throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
110         }
111         Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
112         return schema;
113     }
114
115     private static SchemaNode findChildInCases(final ChoiceSchemaNode parent, final QName qname) {
116         DataSchemaNode schema = null;
117         for(final ChoiceCaseNode caze : parent.getCases()) {
118             final DataSchemaNode potential = caze.getDataChildByName(qname);
119             if(potential != null) {
120                 schema = potential;
121                 break;
122             }
123         }
124         return schema;
125     }
126
127     private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
128         DataSchemaNode schema = null;
129         for(final ChoiceCaseNode caze : parent.getCases()) {
130             final DataSchemaNode potential = caze.getDataChildByName(qname);
131             if(potential != null) {
132                 schema = caze;
133                 break;
134             }
135         }
136         return schema;
137     }
138
139     public void startList(final PathArgument name) {
140         final SchemaNode schema = getSchema(name);
141         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
142         schemaStack.push(schema);
143     }
144
145     public void startListItem(final PathArgument name) throws IOException {
146         final Object schema = getParent();
147         Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
148         schemaStack.push(schema);
149     }
150
151     public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
152         final SchemaNode schema = getSchema(name);
153
154         Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
155         return (LeafSchemaNode) schema;
156     }
157
158     public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
159         final SchemaNode schema = getSchema(name);
160
161         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
162         schemaStack.push(schema);
163         return (LeafListSchemaNode)schema;
164     }
165
166     public LeafListSchemaNode leafSetEntryNode() {
167         final Object parent = getParent();
168
169         Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
170         return (LeafListSchemaNode) parent;
171     }
172
173     public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
174         LOG.debug("Enter choice {}", name);
175         final SchemaNode schema = getSchema(name);
176
177         Preconditions.checkArgument(schema instanceof ChoiceSchemaNode, "Node %s is not a choice", schema.getPath());
178         schemaStack.push(schema);
179         return (ChoiceSchemaNode)schema;
180     }
181
182     public SchemaNode startContainerNode(final NodeIdentifier name) {
183         LOG.debug("Enter container {}", name);
184         final SchemaNode schema = getSchema(name);
185
186         boolean isAllowed = schema instanceof ContainerSchemaNode;
187         isAllowed |= schema instanceof NotificationDefinition;
188
189         Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
190         schemaStack.push(schema);
191
192         return schema;
193     }
194
195     public SchemaNode startYangModeledAnyXmlNode(final NodeIdentifier name) {
196         LOG.debug("Enter yang modeled anyXml {}", name);
197         final SchemaNode schema = getSchema(name);
198
199         Preconditions.checkArgument(schema instanceof YangModeledAnyXmlSchemaNode,
200                 "Node %s is not an yang modeled anyXml.", schema.getPath());
201
202         schemaStack.push(((YangModeledAnyXmlSchemaNode) schema).getSchemaOfAnyXmlData());
203
204         return schema;
205     }
206
207     public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
208         LOG.debug("Enter augmentation {}", identifier);
209         Object parent = getParent();
210
211         Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
212         if(parent instanceof ChoiceSchemaNode) {
213             final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
214             parent = findCaseByChild((ChoiceSchemaNode) parent, name);
215         }
216         Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
217         final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
218         final HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
219         for(final DataSchemaNode child : schema.getChildNodes()) {
220             realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
221         }
222         final AugmentationSchema resolvedSchema = new EffectiveAugmentationSchema(schema, realChildSchemas);
223         schemaStack.push(resolvedSchema);
224         return resolvedSchema;
225     }
226
227     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
228         final SchemaNode schema = getSchema(name);
229
230         Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
231         return (AnyXmlSchemaNode)schema;
232     }
233
234     public Object endNode() {
235         return schemaStack.pop();
236     }
237
238 }