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 com.google.common.collect.ForwardingObject;
15 import java.io.IOException;
16 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
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 {
36 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
37 private DataTreeModification delegate;
38 private final SchemaContext schemaContext;
39 private final DataTree dataTree;
41 public PruningDataTreeModification(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
42 this.delegate = Preconditions.checkNotNull(delegate);
43 this.dataTree = Preconditions.checkNotNull(dataTree);
44 this.schemaContext = Preconditions.checkNotNull(schemaContext);
48 public DataTreeModification delegate() {
53 public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
55 delegate.delete(yangInstanceIdentifier);
56 } catch (SchemaValidationFailedException e) {
57 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
62 public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
64 if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
65 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
67 delegate.merge(yangInstanceIdentifier, normalizedNode);
69 } catch (SchemaValidationFailedException e) {
70 LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
71 yangInstanceIdentifier, e.getMessage());
73 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
78 private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
79 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
82 delegate.merge(yangInstanceIdentifier, pruned);
87 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
89 if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
90 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
92 delegate.write(yangInstanceIdentifier, normalizedNode);
94 } catch (SchemaValidationFailedException e) {
95 LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
96 yangInstanceIdentifier, e.getMessage());
98 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
102 private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
103 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
105 if (pruned != null) {
106 delegate.write(yangInstanceIdentifier, pruned);
111 public void ready() {
114 } catch (SchemaValidationFailedException e) {
115 DataTreeModification newModification = dataTree.takeSnapshot().newModification();
116 delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
118 delegate = newModification;
124 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
125 delegate.applyToCursor(dataTreeModificationCursor);
129 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
130 return delegate.readNode(yangInstanceIdentifier);
134 public DataTreeModification newModification() {
135 return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
139 NormalizedNode<?, ?> pruneNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?,?> input) {
140 NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
142 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
143 } catch (IOException ioe) {
144 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
147 return pruner.normalizedNode();
150 private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
151 private final DataTreeModification toModification;
152 private final PruningDataTreeModification pruningModification;
154 PruningDataTreeModificationCursor(DataTreeModification toModification,
155 PruningDataTreeModification pruningModification) {
156 this.toModification = toModification;
157 this.pruningModification = pruningModification;
161 public void write(PathArgument child, NormalizedNode<?, ?> data) {
162 YangInstanceIdentifier path = current().node(child);
163 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
164 if (prunedNode != null) {
165 toModification.write(path, prunedNode);
170 public void merge(PathArgument child, NormalizedNode<?, ?> data) {
171 YangInstanceIdentifier path = current().node(child);
172 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
173 if (prunedNode != null) {
174 toModification.merge(path, prunedNode);
179 public void delete(PathArgument child) {
181 toModification.delete(current().node(child));
182 } catch (SchemaValidationFailedException e) {
183 // Ignoring since we would've already logged this in the call to the original modification.