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