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.SchemaContext;
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 private interface WriterMethod<T extends PathArgument> {
50 void apply(ReusableImmutableNormalizedNodeStreamWriter writer, T name) throws IOException;
54 private 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 State state = State.UNITIALIZED;
70 // FIXME: package-private to support unguarded NormalizedNodePruner access
71 NormalizedNode<?, ?> normalizedNode;
73 AbstractNormalizedNodePruner(final DataSchemaContextTree tree) {
74 this.tree = requireNonNull(tree);
77 AbstractNormalizedNodePruner(final SchemaContext schemaContext) {
78 this(DataSchemaContextTree.from(schemaContext));
81 final DataSchemaContextTree getTree() {
85 final void initialize(final YangInstanceIdentifier nodePath) {
86 nodePathSchemaNode = tree.findChild(nodePath).orElse(null);
88 normalizedNode = null;
95 public void startLeafNode(final NodeIdentifier name) throws IOException {
96 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafNode, name);
100 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
101 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafSet, name, childSizeHint);
105 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
106 enter(ReusableImmutableNormalizedNodeStreamWriter::startOrderedLeafSet, name, childSizeHint);
110 public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
111 enter(ReusableImmutableNormalizedNodeStreamWriter::startLeafSetEntryNode, name);
115 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
116 enter(ReusableImmutableNormalizedNodeStreamWriter::startContainerNode, name, childSizeHint);
120 public void startYangModeledAnyXmlNode(final NodeIdentifier nodeIdentifier, final int count) {
121 // FIXME: implement this
122 throw new UnsupportedOperationException("Not implemented yet");
126 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
127 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedList, name, childSizeHint);
131 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
132 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedListItem, name, childSizeHint);
136 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
137 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapNode, name, childSizeHint);
141 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
143 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapEntryNode, identifier, childSizeHint);
147 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
148 enter(ReusableImmutableNormalizedNodeStreamWriter::startOrderedMapNode, name, childSizeHint);
152 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
153 enter(ReusableImmutableNormalizedNodeStreamWriter::startChoiceNode, name, childSizeHint);
157 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
158 enter(ReusableImmutableNormalizedNodeStreamWriter::startAugmentationNode, identifier);
162 public boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
164 verify(delegate.startAnyxmlNode(name, objectModel),
165 "Unexpected failure to stream DOMSource node %s model %s", name, objectModel);
171 public boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
172 // FIXME: we do not support anydata nodes yet
177 public void domSourceValue(final DOMSource value) throws IOException {
180 delegate.domSourceValue(value);
185 public void scalarValue(final Object value) throws IOException {
188 delegate.scalarValue(translateScalar(stack.peek(), value));
192 Object translateScalar(final DataSchemaContextNode<?> context, final Object value) throws IOException {
193 // Default is pass-through
198 public void endNode() throws IOException {
204 } catch (NoSuchElementException e) {
205 throw new IllegalStateException("endNode called on an empty stack", e);
211 // Still at unknown, do not attempt to create result
216 if (stack.isEmpty()) {
217 normalizedNode = delegate.getResult();
218 state = State.CLOSED;
223 public void close() throws IOException {
224 state = State.CLOSED;
230 public void flush() throws IOException {
235 * Return the resulting normalized node.
237 * @return Resulting node for the path, if it was not pruned
238 * @throws IllegalStateException if this pruner has not been closed
240 public final Optional<NormalizedNode<?, ?>> getResult() {
241 checkState(state == State.CLOSED, "Cannot get result in state %s", state);
242 return Optional.ofNullable(normalizedNode);
245 private void checkNotSealed() {
246 checkState(state == State.OPEN, "Illegal operation in state %s", state);
249 private boolean enter(final PathArgument name) {
253 LOG.debug("Skipping child {} in unknown subtree", name);
258 final DataSchemaContextNode<?> schema;
259 final DataSchemaContextNode<?> parent = stack.peek();
260 if (parent != null) {
261 schema = parent.getChild(name);
263 schema = nodePathSchemaNode;
266 if (schema == null) {
267 LOG.debug("Schema not found for {}", name);
273 final DataSchemaNode dataSchema = schema.getDataSchemaNode();
274 if (dataSchema != null) {
275 delegate.nextDataSchemaNode(dataSchema);
280 private <A extends PathArgument> void enter(final WriterMethod<A> method, final A name) throws IOException {
282 method.apply(delegate, name);
286 private <A extends PathArgument> void enter(final SizedWriterMethod<A> method, final A name, final int size)
289 method.apply(delegate, name, size);