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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.io.IOException;
15 import java.util.ArrayDeque;
16 import java.util.Deque;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
26 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
33 * before delegating it to the actual DataTreeModification
35 public class PruningDataTreeModification implements DataTreeModification {
37 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
38 private DataTreeModification delegate;
39 private final SchemaContext schemaContext;
40 private final DataTree dataTree;
42 public PruningDataTreeModification(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
43 this.delegate = delegate;
44 this.dataTree = dataTree;
45 this.schemaContext = schemaContext;
49 public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
51 delegate.delete(yangInstanceIdentifier);
52 } catch(SchemaValidationFailedException e){
53 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
58 public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
60 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
61 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
63 delegate.merge(yangInstanceIdentifier, normalizedNode);
65 } catch (SchemaValidationFailedException e){
66 LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
67 yangInstanceIdentifier, e.getMessage());
69 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
74 private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
75 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
78 delegate.merge(yangInstanceIdentifier, pruned);
83 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
85 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
86 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
88 delegate.write(yangInstanceIdentifier, normalizedNode);
90 } catch (SchemaValidationFailedException e){
91 LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
92 yangInstanceIdentifier, e.getMessage());
94 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
98 private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
99 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
102 delegate.write(yangInstanceIdentifier, pruned);
107 public void ready() {
110 } catch (SchemaValidationFailedException e) {
111 DataTreeModification newModification = dataTree.takeSnapshot().newModification();
112 delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
114 delegate = newModification;
120 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
121 delegate.applyToCursor(dataTreeModificationCursor);
125 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
126 return delegate.readNode(yangInstanceIdentifier);
130 public DataTreeModification newModification() {
131 return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
135 NormalizedNode<?, ?> pruneNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?,?> input) {
136 NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
138 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
139 } catch (IOException ioe) {
140 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
143 return pruner.normalizedNode();
146 public DataTreeModification getResultingModification(){
150 private static class PruningDataTreeModificationCursor implements DataTreeModificationCursor {
151 private final Deque<YangInstanceIdentifier> stack = new ArrayDeque<>();
152 private final DataTreeModification toModification;
153 private final PruningDataTreeModification pruningModification;
155 PruningDataTreeModificationCursor(DataTreeModification toModification,
156 PruningDataTreeModification pruningModification) {
157 this.toModification = toModification;
158 this.pruningModification = pruningModification;
159 stack.push(YangInstanceIdentifier.EMPTY);
163 public void write(PathArgument child, NormalizedNode<?, ?> data) {
164 YangInstanceIdentifier path = stack.peek().node(child);
165 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
166 if(prunedNode != null) {
167 toModification.write(path, prunedNode);
172 public void merge(PathArgument child, NormalizedNode<?, ?> data) {
173 YangInstanceIdentifier path = stack.peek().node(child);
174 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
175 if(prunedNode != null) {
176 toModification.merge(path, prunedNode);
181 public void delete(PathArgument child) {
183 toModification.delete(stack.peek().node(child));
184 } catch(SchemaValidationFailedException e) {
185 // Ignoring since we would've already logged this in the call to the original modification.
190 public void enter(@Nonnull final PathArgument child) {
191 stack.push(stack.peek().node(child));
195 public void enter(@Nonnull final PathArgument... path) {
196 for (PathArgument arg : path) {
202 public void enter(@Nonnull final Iterable<PathArgument> path) {
203 for (PathArgument arg : path) {
214 public void exit(final int depth) {
215 Preconditions.checkArgument(depth < stack.size(), "Stack holds only %s elements, cannot exit %s levels", stack.size(), depth);
216 for (int i = 0; i < depth; ++i) {
222 public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
223 throw new UnsupportedOperationException("Not implemented");
227 public void close() {