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.utils;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ForwardingObject;
14 import java.io.IOException;
15 import java.util.Optional;
16 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.ReusableNormalizedNodePruner;
17 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
25 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
32 * before delegating it to the actual DataTreeModification.
34 public class PruningDataTreeModification extends ForwardingObject implements DataTreeModification {
35 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
37 private final ReusableNormalizedNodePruner pruner;
38 private final DataTree dataTree;
40 private DataTreeModification delegate;
42 public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
43 final ReusableNormalizedNodePruner pruner) {
44 this.delegate = requireNonNull(delegate);
45 this.dataTree = requireNonNull(dataTree);
46 this.pruner = requireNonNull(pruner);
50 protected DataTreeModification delegate() {
55 public SchemaContext getSchemaContext() {
56 return delegate.getSchemaContext();
60 public void delete(final YangInstanceIdentifier path) {
62 delegate.delete(path);
63 } catch (SchemaValidationFailedException e) {
64 LOG.warn("Node at path : {} does not exist ignoring delete", path);
69 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
71 pruneAndMergeNode(path, data);
76 delegate.merge(path, data);
77 } catch (SchemaValidationFailedException e) {
78 LOG.warn("Node at path {} was pruned during merge due to validation error: {}", path, e.getMessage());
79 pruneAndMergeNode(path, data);
83 private void pruneAndMergeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
84 final NormalizedNode<?, ?> pruned = pruneNormalizedNode(path, data);
86 delegate.merge(path, pruned);
91 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
93 pruneAndWriteNode(path, data);
98 delegate.write(path, data);
99 } catch (SchemaValidationFailedException e) {
100 LOG.warn("Node at path : {} was pruned during write due to validation error: {}", path, e.getMessage());
101 pruneAndWriteNode(path, data);
105 private void pruneAndWriteNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
106 final NormalizedNode<?, ?> pruned = pruneNormalizedNode(path, data);
107 if (pruned != null) {
108 delegate.write(path, pruned);
113 public void ready() {
116 } catch (SchemaValidationFailedException e) {
117 DataTreeModification newModification = dataTree.takeSnapshot().newModification();
118 delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
120 delegate = newModification;
126 public void applyToCursor(final DataTreeModificationCursor dataTreeModificationCursor) {
127 delegate.applyToCursor(dataTreeModificationCursor);
131 public Optional<NormalizedNode<?, ?>> readNode(final YangInstanceIdentifier yangInstanceIdentifier) {
132 return delegate.readNode(yangInstanceIdentifier);
136 public DataTreeModification newModification() {
137 return new PruningDataTreeModification(delegate.newModification(), dataTree, pruner.duplicate());
141 NormalizedNode<?, ?> pruneNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> input) {
142 pruner.initializeForPath(path);
144 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
145 } catch (IOException ioe) {
146 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
150 return pruner.getResult().orElse(null);
153 private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
154 private final DataTreeModification toModification;
155 private final PruningDataTreeModification pruningModification;
157 PruningDataTreeModificationCursor(final DataTreeModification toModification,
158 final PruningDataTreeModification pruningModification) {
159 this.toModification = toModification;
160 this.pruningModification = pruningModification;
164 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
165 final YangInstanceIdentifier path = current().node(child);
166 final NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
167 if (prunedNode != null) {
168 toModification.write(path, prunedNode);
173 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
174 final YangInstanceIdentifier path = current().node(child);
175 final NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
176 if (prunedNode != null) {
177 toModification.merge(path, prunedNode);
182 public void delete(final PathArgument child) {
184 toModification.delete(current().node(child));
185 } catch (SchemaValidationFailedException e) {
186 // Ignoring since we would've already logged this in the call to the original modification.