Write failed node data on recovery to a file
[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 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;
26
27 /**
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
30  */
31 public class PruningDataTreeModification implements DataTreeModification {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
34     private DataTreeModification delegate;
35     private final SchemaContext schemaContext;
36     private final DataTree dataTree;
37
38     public PruningDataTreeModification(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
39         this.delegate = delegate;
40         this.dataTree = dataTree;
41         this.schemaContext = schemaContext;
42     }
43
44     @Override
45     public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
46         try {
47             delegate.delete(yangInstanceIdentifier);
48         } catch(SchemaValidationFailedException e){
49             LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
50         }
51     }
52
53     @Override
54     public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
55         try {
56             if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
57                 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
58             } else {
59                 delegate.merge(yangInstanceIdentifier, normalizedNode);
60             }
61         } catch (SchemaValidationFailedException e){
62             LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
63                     yangInstanceIdentifier, e.getMessage());
64
65             pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
66         }
67
68     }
69
70     private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
71         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
72
73         if(pruned != null) {
74             delegate.merge(yangInstanceIdentifier, pruned);
75         }
76     }
77
78     @Override
79     public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
80         try {
81             if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
82                 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
83             } else {
84                 delegate.write(yangInstanceIdentifier, normalizedNode);
85             }
86         } catch (SchemaValidationFailedException e){
87             LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
88                     yangInstanceIdentifier, e.getMessage());
89
90             pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
91         }
92     }
93
94     private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
95         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
96
97         if(pruned != null) {
98             delegate.write(yangInstanceIdentifier, pruned);
99         }
100     }
101
102     @Override
103     public void ready() {
104         try {
105             delegate.ready();
106         } catch (SchemaValidationFailedException e) {
107             DataTreeModification newModification = dataTree.takeSnapshot().newModification();
108             delegate.applyToCursor(new PruningDataTreeModificationCursor(newModification, this));
109
110             delegate = newModification;
111             delegate.ready();
112         }
113     }
114
115     @Override
116     public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
117         delegate.applyToCursor(dataTreeModificationCursor);
118     }
119
120     @Override
121     public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
122         return delegate.readNode(yangInstanceIdentifier);
123     }
124
125     @Override
126     public DataTreeModification newModification() {
127         return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
128     }
129
130     @VisibleForTesting
131     NormalizedNode<?, ?> pruneNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?,?> input) {
132         NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
133         try {
134             NormalizedNodeWriter.forStreamWriter(pruner).write(input);
135         } catch (IOException ioe) {
136             LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
137         }
138
139         return pruner.normalizedNode();
140     }
141
142     public DataTreeModification getResultingModification(){
143         return delegate;
144     }
145
146     private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
147         private final DataTreeModification toModification;
148         private final PruningDataTreeModification pruningModification;
149
150         PruningDataTreeModificationCursor(DataTreeModification toModification,
151                 PruningDataTreeModification pruningModification) {
152             this.toModification = toModification;
153             this.pruningModification = pruningModification;
154         }
155
156         @Override
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);
162             }
163         }
164
165         @Override
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);
171             }
172         }
173
174         @Override
175         public void delete(PathArgument child) {
176             try {
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.
180             }
181         }
182     }
183 }