Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModification.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.utils;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
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.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;
29
30 /**
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.
33  */
34 public class PruningDataTreeModification extends ForwardingObject implements DataTreeModification {
35
36     private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
37     private DataTreeModification delegate;
38     private final SchemaContext schemaContext;
39     private final DataTree dataTree;
40
41     public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
42             final SchemaContext schemaContext) {
43         this.delegate = Preconditions.checkNotNull(delegate);
44         this.dataTree = Preconditions.checkNotNull(dataTree);
45         this.schemaContext = Preconditions.checkNotNull(schemaContext);
46     }
47
48     @Override
49     public DataTreeModification delegate() {
50         return delegate;
51     }
52
53     @Override
54     public void delete(final YangInstanceIdentifier yangInstanceIdentifier) {
55         try {
56             delegate.delete(yangInstanceIdentifier);
57         } catch (SchemaValidationFailedException e) {
58             LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
59         }
60     }
61
62     @Override
63     public void merge(final YangInstanceIdentifier yangInstanceIdentifier, final NormalizedNode<?, ?> normalizedNode) {
64         try {
65             if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
66                 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
67             } else {
68                 delegate.merge(yangInstanceIdentifier, normalizedNode);
69             }
70         } catch (SchemaValidationFailedException e) {
71             LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
72                     yangInstanceIdentifier, e.getMessage());
73
74             pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
75         }
76
77     }
78
79     private void pruneAndMergeNode(final YangInstanceIdentifier yangInstanceIdentifier,
80             final NormalizedNode<?, ?> normalizedNode) {
81         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
82
83         if (pruned != null) {
84             delegate.merge(yangInstanceIdentifier, pruned);
85         }
86     }
87
88     @Override
89     public void write(final YangInstanceIdentifier yangInstanceIdentifier, final NormalizedNode<?, ?> normalizedNode) {
90         try {
91             if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
92                 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
93             } else {
94                 delegate.write(yangInstanceIdentifier, normalizedNode);
95             }
96         } catch (SchemaValidationFailedException e) {
97             LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
98                     yangInstanceIdentifier, e.getMessage());
99
100             pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
101         }
102     }
103
104     private void pruneAndWriteNode(final YangInstanceIdentifier yangInstanceIdentifier,
105             final NormalizedNode<?, ?> normalizedNode) {
106         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
107
108         if (pruned != null) {
109             delegate.write(yangInstanceIdentifier, pruned);
110         }
111     }
112
113     @Override
114     public void ready() {
115         try {
116             delegate.ready();
117         } catch (SchemaValidationFailedException e) {
118             DataTreeModification newModification = dataTree.takeSnapshot().newModification();
119             delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
120
121             delegate = newModification;
122             delegate.ready();
123         }
124     }
125
126     @Override
127     public void applyToCursor(final DataTreeModificationCursor dataTreeModificationCursor) {
128         delegate.applyToCursor(dataTreeModificationCursor);
129     }
130
131     @Override
132     public Optional<NormalizedNode<?, ?>> readNode(final YangInstanceIdentifier yangInstanceIdentifier) {
133         return delegate.readNode(yangInstanceIdentifier);
134     }
135
136     @Override
137     public DataTreeModification newModification() {
138         return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
139     }
140
141     @VisibleForTesting
142     NormalizedNode<?, ?> pruneNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?,?> input) {
143         NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
144         try {
145             NormalizedNodeWriter.forStreamWriter(pruner).write(input);
146         } catch (IOException ioe) {
147             LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
148         }
149
150         return pruner.normalizedNode();
151     }
152
153     private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
154         private final DataTreeModification toModification;
155         private final PruningDataTreeModification pruningModification;
156
157         PruningDataTreeModificationCursor(final DataTreeModification toModification,
158                 final PruningDataTreeModification pruningModification) {
159             this.toModification = toModification;
160             this.pruningModification = pruningModification;
161         }
162
163         @Override
164         public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
165             YangInstanceIdentifier path = current().node(child);
166             NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
167             if (prunedNode != null) {
168                 toModification.write(path, prunedNode);
169             }
170         }
171
172         @Override
173         public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
174             YangInstanceIdentifier path = current().node(child);
175             NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
176             if (prunedNode != null) {
177                 toModification.merge(path, prunedNode);
178             }
179         }
180
181         @Override
182         public void delete(final PathArgument child) {
183             try {
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.
187             }
188         }
189     }
190 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.