2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.node.utils.transformer;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import java.io.IOException;
15 import java.util.ArrayDeque;
16 import java.util.Deque;
17 import java.util.NoSuchElementException;
18 import java.util.Optional;
19 import javax.xml.transform.dom.DOMSource;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The NormalizedNodePruner removes all nodes from the input NormalizedNode that do not have a corresponding
38 * schema element in the passed in SchemaContext.
40 abstract class AbstractNormalizedNodePruner implements NormalizedNodeStreamWriter {
48 interface WriterMethod<T extends PathArgument> {
50 void apply(ReusableImmutableNormalizedNodeStreamWriter writer, T name) throws IOException;
54 interface SizedWriterMethod<T extends PathArgument> {
56 void apply(ReusableImmutableNormalizedNodeStreamWriter writer, T name, int childSizeHint) throws IOException;
59 private static final Logger LOG = LoggerFactory.getLogger(AbstractNormalizedNodePruner.class);
61 private final Deque<DataSchemaContextNode<?>> stack = new ArrayDeque<>();
62 private final ReusableImmutableNormalizedNodeStreamWriter delegate =
63 ReusableImmutableNormalizedNodeStreamWriter.create();
64 private final DataSchemaContextTree tree;
66 private DataSchemaContextNode<?> nodePathSchemaNode;
67 private NormalizedNode normalizedNode;
68 private State state = State.UNITIALIZED;
71 AbstractNormalizedNodePruner(final DataSchemaContextTree tree) {
72 this.tree = requireNonNull(tree);
75 AbstractNormalizedNodePruner(final EffectiveModelContext schemaContext) {
76 this(DataSchemaContextTree.from(schemaContext));
79 final DataSchemaContextTree getTree() {
83 final void initialize(final YangInstanceIdentifier nodePath) {
84 nodePathSchemaNode = tree.findChild(nodePath).orElse(null);
86 normalizedNode = null;
93 public final void startLeafNode(final NodeIdentifier name) throws IOException {
94 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafNode, name);
98 public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
99 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafSet, name, childSizeHint);
103 public final void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
104 enter(ReusableImmutableNormalizedNodeStreamWriter::startOrderedLeafSet, name, childSizeHint);
108 public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
109 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafSetEntryNode, name);
113 public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
114 enter(ReusableImmutableNormalizedNodeStreamWriter::startContainerNode, name, childSizeHint);
118 public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
119 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedList, name, childSizeHint);
123 public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
124 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedListItem, name, childSizeHint);
128 public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
129 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapNode, name, childSizeHint);
133 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
135 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapEntryNode, identifier, childSizeHint);
139 public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
140 enter(ReusableImmutableNormalizedNodeStreamWriter::startOrderedMapNode, name, childSizeHint);
144 public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
145 enter(ReusableImmutableNormalizedNodeStreamWriter::startChoiceNode, name, childSizeHint);
149 public final void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
150 enter(ReusableImmutableNormalizedNodeStreamWriter::startAugmentationNode, identifier);
154 public final boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
156 verify(delegate.startAnyxmlNode(name, objectModel),
157 "Unexpected failure to stream DOMSource node %s model %s", name, objectModel);
163 public final boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
164 // FIXME: we do not support anydata nodes yet
169 public final void domSourceValue(final DOMSource value) throws IOException {
172 delegate.domSourceValue(value);
177 public final void scalarValue(final Object value) throws IOException {
180 delegate.scalarValue(translateScalar(currentSchema(), value));
184 Object translateScalar(final DataSchemaContextNode<?> context, final Object value) throws IOException {
185 // Default is pass-through
190 public final void endNode() throws IOException {
196 } catch (NoSuchElementException e) {
197 throw new IllegalStateException("endNode called on an empty stack", e);
203 // Still at unknown, do not attempt to create result
208 if (stack.isEmpty()) {
209 normalizedNode = delegate.getResult();
210 state = State.CLOSED;
215 public final void close() throws IOException {
216 state = State.CLOSED;
222 public final void flush() throws IOException {
227 * Return the resulting normalized node.
229 * @return Resulting node for the path, if it was not pruned
230 * @throws IllegalStateException if this pruner has not been closed
232 public final Optional<NormalizedNode> getResult() {
233 checkState(state == State.CLOSED, "Cannot get result in state %s", state);
234 return Optional.ofNullable(normalizedNode);
237 private void checkNotSealed() {
238 checkState(state == State.OPEN, "Illegal operation in state %s", state);
241 private boolean enter(final PathArgument name) {
245 LOG.debug("Skipping child {} in unknown subtree", name);
250 final DataSchemaContextNode<?> schema;
251 final DataSchemaContextNode<?> parent = currentSchema();
252 if (parent != null) {
253 schema = parent.getChild(name);
255 schema = nodePathSchemaNode;
258 if (schema == null) {
259 LOG.debug("Schema not found for {}", name);
265 final DataSchemaNode dataSchema = schema.getDataSchemaNode();
266 if (dataSchema != null) {
267 delegate.nextDataSchemaNode(dataSchema);
272 final <A extends PathArgument> void enter(final WriterMethod<A> method, final A name) throws IOException {
274 method.apply(delegate, name);
278 final <A extends PathArgument> void enter(final SizedWriterMethod<A> method, final A name, final int size)
281 method.apply(delegate, name, size);
285 final DataSchemaContextNode<?> currentSchema() {