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 yangInstanceIdentifier) {
62 delegate.delete(yangInstanceIdentifier);
63 } catch (SchemaValidationFailedException e) {
64 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
69 public void merge(final YangInstanceIdentifier yangInstanceIdentifier, final NormalizedNode<?, ?> normalizedNode) {
71 if (YangInstanceIdentifier.empty().equals(yangInstanceIdentifier)) {
72 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
74 delegate.merge(yangInstanceIdentifier, normalizedNode);
76 } catch (SchemaValidationFailedException e) {
77 LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
78 yangInstanceIdentifier, e.getMessage());
80 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
85 private void pruneAndMergeNode(final YangInstanceIdentifier yangInstanceIdentifier,
86 final NormalizedNode<?, ?> normalizedNode) {
87 NormalizedNode<?, ?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
90 delegate.merge(yangInstanceIdentifier, pruned);
95 public void write(final YangInstanceIdentifier yangInstanceIdentifier, final NormalizedNode<?, ?> normalizedNode) {
97 if (YangInstanceIdentifier.empty().equals(yangInstanceIdentifier)) {
98 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
100 delegate.write(yangInstanceIdentifier, normalizedNode);
102 } catch (SchemaValidationFailedException e) {
103 LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
104 yangInstanceIdentifier, e.getMessage());
106 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
110 private void pruneAndWriteNode(final YangInstanceIdentifier yangInstanceIdentifier,
111 final NormalizedNode<?, ?> normalizedNode) {
112 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
114 if (pruned != null) {
115 delegate.write(yangInstanceIdentifier, pruned);
120 public void ready() {
123 } catch (SchemaValidationFailedException e) {
124 DataTreeModification newModification = dataTree.takeSnapshot().newModification();
125 delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
127 delegate = newModification;
133 public void applyToCursor(final DataTreeModificationCursor dataTreeModificationCursor) {
134 delegate.applyToCursor(dataTreeModificationCursor);
138 public Optional<NormalizedNode<?, ?>> readNode(final YangInstanceIdentifier yangInstanceIdentifier) {
139 return delegate.readNode(yangInstanceIdentifier);
143 public DataTreeModification newModification() {
144 return new PruningDataTreeModification(delegate.newModification(), dataTree, pruner.duplicate());
148 NormalizedNode<?, ?> pruneNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> input) {
149 pruner.initializeForPath(path);
151 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
152 } catch (IOException ioe) {
153 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
157 return pruner.getResult().orElse(null);
160 private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
161 private final DataTreeModification toModification;
162 private final PruningDataTreeModification pruningModification;
164 PruningDataTreeModificationCursor(final DataTreeModification toModification,
165 final PruningDataTreeModification pruningModification) {
166 this.toModification = toModification;
167 this.pruningModification = pruningModification;
171 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
172 YangInstanceIdentifier path = current().node(child);
173 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
174 if (prunedNode != null) {
175 toModification.write(path, prunedNode);
180 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
181 YangInstanceIdentifier path = current().node(child);
182 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
183 if (prunedNode != null) {
184 toModification.merge(path, prunedNode);
189 public void delete(final PathArgument child) {
191 toModification.delete(current().node(child));
192 } catch (SchemaValidationFailedException e) {
193 // Ignoring since we would've already logged this in the call to the original modification.