Merge "Bug 1478 - Autoboxing support"
[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 import java.io.IOException;
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import java.util.HashSet;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
23 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
24 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Utility class for tracking the underlying state of the underlying
42  * schema node.
43  */
44 @Beta
45 public final class SchemaTracker {
46     private static final Logger LOG = LoggerFactory.getLogger(SchemaTracker.class);
47     private final Deque<Object> schemaStack = new ArrayDeque<>();
48     private final DataNodeContainer root;
49
50     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
51         DataNodeContainer current = Preconditions.checkNotNull(context);
52         for (QName qname : path.getPathFromRoot()) {
53             final DataSchemaNode child = current.getDataChildByName(qname);
54             Preconditions.checkArgument(child instanceof DataNodeContainer);
55             current = (DataNodeContainer) child;
56         }
57
58         this.root = current;
59     }
60
61     /**
62      * Create a new writer with the specified context as its root.
63      *
64      * @param writer Output {@link XMLStreamWriter}
65      * @param context Associated {@link SchemaContext}.
66      * @return A new {@link NormalizedNodeStreamWriter}
67      */
68     public static SchemaTracker create(final SchemaContext context) {
69         return create(context, SchemaPath.ROOT);
70     }
71
72     /**
73      * Create a new writer with the specified context and rooted in the specified schema path
74      *
75      * @param writer Output {@link XMLStreamWriter}
76      * @param context Associated {@link SchemaContext}.
77      *
78      * @return A new {@link NormalizedNodeStreamWriter}
79      */
80     public static SchemaTracker create(final SchemaContext context, final SchemaPath path) {
81         return new SchemaTracker(context, path);
82     }
83
84     public Object getParent() {
85         if (schemaStack.isEmpty()) {
86             return root;
87         }
88         return schemaStack.peek();
89     }
90
91     private SchemaNode getSchema(final PathArgument name) {
92         final Object parent = getParent();
93         Preconditions.checkState(parent instanceof DataNodeContainer);
94
95         final QName qname = name.getNodeType();
96         final SchemaNode schema = ((DataNodeContainer)parent).getDataChildByName(qname);
97         Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
98         return schema;
99     }
100
101     public void startList(final NodeIdentifier name) {
102         final SchemaNode schema = getSchema(name);
103         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
104         schemaStack.push(schema);
105     }
106
107     public void startListItem(final PathArgument name) throws IOException {
108         final Object schema = getParent();
109         Preconditions.checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
110         schemaStack.push(schema);
111     }
112
113     public LeafSchemaNode leafNode(final NodeIdentifier name) throws IOException {
114         final SchemaNode schema = getSchema(name);
115
116         Preconditions.checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema.getPath());
117         return (LeafSchemaNode) schema;
118     }
119
120     public SchemaNode startLeafSet(final NodeIdentifier name) {
121         final SchemaNode schema = getSchema(name);
122
123         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
124         schemaStack.push(schema);
125         return schema;
126     }
127
128     public LeafListSchemaNode leafSetEntryNode() {
129         final Object parent = getParent();
130
131         Preconditions.checkArgument(parent instanceof LeafListSchemaNode, "Not currently in a leaf-list");
132         return (LeafListSchemaNode) parent;
133     }
134
135     public SchemaNode startChoiceNode(final NodeIdentifier name) {
136         LOG.debug("Enter choice {}", name);
137         final SchemaNode schema = getSchema(name);
138
139         Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
140         schemaStack.push(schema);
141         return schema;
142     }
143
144     public SchemaNode startContainerNode(final NodeIdentifier name) {
145         LOG.debug("Enter container {}", name);
146         final SchemaNode schema = getSchema(name);
147
148         Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
149         schemaStack.push(schema);
150         return schema;
151     }
152
153     public AugmentationSchema startAugmentationNode(final AugmentationIdentifier identifier) {
154         LOG.debug("Enter augmentation {}", identifier);
155         final Object parent = getParent();
156
157         Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
158         Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
159         final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
160         HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
161         for(DataSchemaNode child : schema.getChildNodes()) {
162             realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
163         }
164         AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
165         schemaStack.push(resolvedSchema);
166         return resolvedSchema;
167     }
168
169     public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
170         final SchemaNode schema = getSchema(name);
171
172         Preconditions.checkArgument(schema instanceof AnyXmlSchemaNode, "Node %s is not anyxml", schema.getPath());
173         return (AnyXmlSchemaNode)schema;
174     }
175
176     public Object endNode() {
177         return schemaStack.pop();
178     }
179 }