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 java.io.IOException;
14 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
22 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
29 * before delegating it to the actual DataTreeModification
31 public class PruningDataTreeModification implements DataTreeModification {
33 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
34 private DataTreeModification delegate;
35 private final SchemaContext schemaContext;
36 private final DataTree dataTree;
38 public PruningDataTreeModification(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
39 this.delegate = delegate;
40 this.dataTree = dataTree;
41 this.schemaContext = schemaContext;
45 public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
47 delegate.delete(yangInstanceIdentifier);
48 } catch(SchemaValidationFailedException e){
49 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
54 public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
56 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
57 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
59 delegate.merge(yangInstanceIdentifier, normalizedNode);
61 } catch (SchemaValidationFailedException e){
62 LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
63 yangInstanceIdentifier, e.getMessage());
65 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
70 private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
71 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
74 delegate.merge(yangInstanceIdentifier, pruned);
79 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
81 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
82 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
84 delegate.write(yangInstanceIdentifier, normalizedNode);
86 } catch (SchemaValidationFailedException e){
87 LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
88 yangInstanceIdentifier, e.getMessage());
90 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
94 private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
95 NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
98 delegate.write(yangInstanceIdentifier, pruned);
103 public void ready() {
106 } catch (SchemaValidationFailedException e) {
107 DataTreeModification newModification = dataTree.takeSnapshot().newModification();
108 delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
110 delegate = newModification;
116 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
117 delegate.applyToCursor(dataTreeModificationCursor);
121 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
122 return delegate.readNode(yangInstanceIdentifier);
126 public DataTreeModification newModification() {
127 return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
131 NormalizedNode<?, ?> pruneNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?,?> input) {
132 NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
134 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
135 } catch (IOException ioe) {
136 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
139 return pruner.normalizedNode();
142 public DataTreeModification getResultingModification(){
146 private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
147 private final DataTreeModification toModification;
148 private final PruningDataTreeModification pruningModification;
150 PruningDataTreeModificationCursor(DataTreeModification toModification,
151 PruningDataTreeModification pruningModification) {
152 this.toModification = toModification;
153 this.pruningModification = pruningModification;
157 public void write(PathArgument child, NormalizedNode<?, ?> data) {
158 YangInstanceIdentifier path = next(child);
159 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
160 if(prunedNode != null) {
161 toModification.write(path, prunedNode);
166 public void merge(PathArgument child, NormalizedNode<?, ?> data) {
167 YangInstanceIdentifier path = next(child);
168 NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
169 if(prunedNode != null) {
170 toModification.merge(path, prunedNode);
175 public void delete(PathArgument child) {
177 toModification.delete(next(child));
178 } catch(SchemaValidationFailedException e) {
179 // Ignoring since we would've already logged this in the call to the original modification.