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 startYangModeledAnyXmlNode(final NodeIdentifier nodeIdentifier, final int count) {
119 // FIXME: implement this
120 throw new UnsupportedOperationException("Not implemented yet");
124 public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
125 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedList, name, childSizeHint);
129 public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
130 enter(ReusableImmutableNormalizedNodeStreamWriter::startUnkeyedListItem, name, childSizeHint);
134 public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
135 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapNode, name, childSizeHint);
139 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
141 enter(ReusableImmutableNormalizedNodeStreamWriter::startMapEntryNode, identifier, childSizeHint);
145 public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
146 enter(ReusableImmutableNormalizedNodeStreamWriter::startOrderedMapNode, name, childSizeHint);
150 public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
151 enter(ReusableImmutableNormalizedNodeStreamWriter::startChoiceNode, name, childSizeHint);
155 public final void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
156 enter(ReusableImmutableNormalizedNodeStreamWriter::startAugmentationNode, identifier);
160 public final boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
162 verify(delegate.startAnyxmlNode(name, objectModel),
163 "Unexpected failure to stream DOMSource node %s model %s", name, objectModel);
169 public final boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
170 // FIXME: we do not support anydata nodes yet
175 public final void domSourceValue(final DOMSource value) throws IOException {
178 delegate.domSourceValue(value);
183 public final void scalarValue(final Object value) throws IOException {
186 delegate.scalarValue(translateScalar(currentSchema(), value));
190 Object translateScalar(final DataSchemaContextNode<?> context, final Object value) throws IOException {
191 // Default is pass-through
196 public final void endNode() throws IOException {
202 } catch (NoSuchElementException e) {
203 throw new IllegalStateException("endNode called on an empty stack", e);
209 // Still at unknown, do not attempt to create result
214 if (stack.isEmpty()) {
215 normalizedNode = delegate.getResult();
216 state = State.CLOSED;
221 public final void close() throws IOException {
222 state = State.CLOSED;
228 public final void flush() throws IOException {
233 * Return the resulting normalized node.
235 * @return Resulting node for the path, if it was not pruned
236 * @throws IllegalStateException if this pruner has not been closed
238 public final Optional<NormalizedNode<?, ?>> getResult() {
239 checkState(state == State.CLOSED, "Cannot get result in state %s", state);
240 return Optional.ofNullable(normalizedNode);
243 private void checkNotSealed() {
244 checkState(state == State.OPEN, "Illegal operation in state %s", state);
247 private boolean enter(final PathArgument name) {
251 LOG.debug("Skipping child {} in unknown subtree", name);
256 final DataSchemaContextNode<?> schema;
257 final DataSchemaContextNode<?> parent = currentSchema();
258 if (parent != null) {
259 schema = parent.getChild(name);
261 schema = nodePathSchemaNode;
264 if (schema == null) {
265 LOG.debug("Schema not found for {}", name);
271 final DataSchemaNode dataSchema = schema.getDataSchemaNode();
272 if (dataSchema != null) {
273 delegate.nextDataSchemaNode(dataSchema);
278 final <A extends PathArgument> void enter(final WriterMethod<A> method, final A name) throws IOException {
280 method.apply(delegate, name);
284 final <A extends PathArgument> void enter(final SizedWriterMethod<A> method, final A name, final int size)
287 method.apply(delegate, name, size);
291 final DataSchemaContextNode<?> currentSchema() {