0e97cd958804ce632d1b45af74f7f6b4192f36c7
[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.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;
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(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
42         this.delegate = Preconditions.checkNotNull(delegate);
43         this.dataTree = Preconditions.checkNotNull(dataTree);
44         this.schemaContext = Preconditions.checkNotNull(schemaContext);
45     }
46
47     @Override
48     public DataTreeModification delegate() {
49         return delegate;
50     }
51
52     @Override
53     public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
54         try {
55             delegate.delete(yangInstanceIdentifier);
56         } catch (SchemaValidationFailedException e) {
57             LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
58         }
59     }
60
61     @Override
62     public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
63         try {
64             if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
65                 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
66             } else {
67                 delegate.merge(yangInstanceIdentifier, normalizedNode);
68             }
69         } catch (SchemaValidationFailedException e) {
70             LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
71                     yangInstanceIdentifier, e.getMessage());
72
73             pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
74         }
75
76     }
77
78     private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
79         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
80
81         if (pruned != null) {
82             delegate.merge(yangInstanceIdentifier, pruned);
83         }
84     }
85
86     @Override
87     public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
88         try {
89             if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
90                 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
91             } else {
92                 delegate.write(yangInstanceIdentifier, normalizedNode);
93             }
94         } catch (SchemaValidationFailedException e) {
95             LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
96                     yangInstanceIdentifier, e.getMessage());
97
98             pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
99         }
100     }
101
102     private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
103         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
104
105         if (pruned != null) {
106             delegate.write(yangInstanceIdentifier, pruned);
107         }
108     }
109
110     @Override
111     public void ready() {
112         try {
113             delegate.ready();
114         } catch (SchemaValidationFailedException e) {
115             DataTreeModification newModification = dataTree.takeSnapshot().newModification();
116             delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
117
118             delegate = newModification;
119             delegate.ready();
120         }
121     }
122
123     @Override
124     public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
125         delegate.applyToCursor(dataTreeModificationCursor);
126     }
127
128     @Override
129     public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
130         return delegate.readNode(yangInstanceIdentifier);
131     }
132
133     @Override
134     public DataTreeModification newModification() {
135         return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
136     }
137
138     @VisibleForTesting
139     NormalizedNode<?, ?> pruneNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?,?> input) {
140         NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
141         try {
142             NormalizedNodeWriter.forStreamWriter(pruner).write(input);
143         } catch (IOException ioe) {
144             LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
145         }
146
147         return pruner.normalizedNode();
148     }
149
150     private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
151         private final DataTreeModification toModification;
152         private final PruningDataTreeModification pruningModification;
153
154         PruningDataTreeModificationCursor(DataTreeModification toModification,
155                 PruningDataTreeModification pruningModification) {
156             this.toModification = toModification;
157             this.pruningModification = pruningModification;
158         }
159
160         @Override
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);
166             }
167         }
168
169         @Override
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);
175             }
176         }
177
178         @Override
179         public void delete(PathArgument child) {
180             try {
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.
184             }
185         }
186     }
187 }

©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.