Merge "Improve TypeProviderImpl.sortTypeDefinitionAccordingDepth()"
[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.Preconditions;
12
13 import java.io.IOException;
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16
17 import javax.xml.stream.XMLStreamWriter;
18
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.ChoiceNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Utility class for tracking the underlying state of the underlying
43  * schema node.
44  */
45 @Beta
46 public final class SchemaTracker {
47     private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
48     private final Deque<Object> schemaStack = new ArrayDeque<>();
49     private final DataNodeContainer root;
50
51     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
52         DataNodeContainer current = Preconditions.checkNotNull(context);
53         for (QName qname : path.getPathFromRoot()) {
54             final DataSchemaNode child = current.getDataChildByName(qname);
55             Preconditions.checkArgument(child instanceof DataNodeContainer);
56             current = (DataNodeContainer) child;
57         }
58
59         this.root = current;
60     }
61
62     /**
63      * Create a new writer with the specified context as its root.
64      *
65      * @param writer Output {@link XMLStreamWriter}
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 writer Output {@link XMLStreamWriter}
77      * @param context Associated {@link SchemaContext}.
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         Preconditions.checkState(parent instanceof DataNodeContainer);
95
96         final QName qname = name.getNodeType();
97         final SchemaNode schema = ((DataNodeContainer)parent).getDataChildByName(qname);
98         Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
99         return schema;
100     }
101
102     public void startList(final NodeIdentifier name) {
103         final SchemaNode schema = getSchema(name);
104         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
105         schemaStack.push(schema);
106     }
107
108     public void startListItem(final PathArgument name) throws IOException {
109         final Object schema = getParent();
110         Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
111         schemaStack.push(schema);
112     }
113
114     public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
115         final SchemaNode schema = getSchema(name);
116
117         Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
118         return (LeafSchemaNode) schema;
119     }
120
121     public SchemaNode startLeafSet(final NodeIdentifier name) {
122         final SchemaNode schema = getSchema(name);
123
124         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
125         schemaStack.push(schema);
126         return schema;
127     }
128
129     public LeafListSchemaNode leafSetEntryNode() {
130         final Object parent = getParent();
131
132         Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
133         return (LeafListSchemaNode) parent;
134     }
135
136     public SchemaNode startChoiceNode(final NodeIdentifier name) {
137         LOG.debug("Enter choice {}", name);
138         final SchemaNode schema = getSchema(name);
139
140         Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
141         schemaStack.push(schema);
142         return schema;
143     }
144
145     public SchemaNode startContainerNode(final NodeIdentifier name) {
146         LOG.debug("Enter container {}", name);
147         final SchemaNode schema = getSchema(name);
148
149         Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
150         schemaStack.push(schema);
151         return schema;
152     }
153
154     public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
155         LOG.debug("Enter augmentation {}", identifier);
156         final Object parent = getParent();
157
158         Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
159         final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
160         schemaStack.push(schema);
161         return schema;
162     }
163
164     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
165         final SchemaNode schema = getSchema(name);
166
167         Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
168         return (AnyXmlSchemaNode)schema;
169     }
170
171     public Object endNode() {
172         return schemaStack.pop();
173     }
174 }