BUG 2970 : Recovery fails with SchemaValidationException when removing modules
[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 java.net.URI;
15 import java.util.Set;
16 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
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.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
28  * before delegating it to the actual DataTreeModification
29  */
30 public class PruningDataTreeModification implements DataTreeModification {
31
32     private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
33     private final DataTreeModification delegate;
34     private final Set<URI> validNamespaces;
35
36     public PruningDataTreeModification(DataTreeModification delegate, Set<URI> validNamespaces) {
37         this.delegate = delegate;
38         this.validNamespaces = validNamespaces;
39     }
40
41     @Override
42     public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
43         try {
44             delegate.delete(yangInstanceIdentifier);
45         } catch(SchemaValidationFailedException e){
46             LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
47         }
48     }
49
50     @Override
51     public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
52         try {
53             if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
54                 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
55             } else {
56                 delegate.merge(yangInstanceIdentifier, normalizedNode);
57             }
58         } catch (SchemaValidationFailedException e){
59             if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
60                 LOG.warn("Invalid node identifier {} ignoring merge", yangInstanceIdentifier);
61                 return;
62             }
63
64             pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
65         }
66
67     }
68
69     private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
70         LOG.warn("Node at path : {} was pruned during merge", yangInstanceIdentifier);
71
72         NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
73
74         if(pruned != null) {
75             delegate.merge(yangInstanceIdentifier, pruned);
76         }
77     }
78
79     @Override
80     public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
81         try {
82             if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
83                 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
84             } else {
85                 delegate.write(yangInstanceIdentifier, normalizedNode);
86             }
87         } catch (SchemaValidationFailedException e){
88             if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
89                 LOG.warn("Invalid node identifier {} ignoring write", yangInstanceIdentifier);
90                 return;
91             }
92
93             pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
94         }
95     }
96
97     private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
98         LOG.warn("Node at path : {} was pruned during write", yangInstanceIdentifier);
99
100         NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
101
102         if(pruned != null) {
103             delegate.write(yangInstanceIdentifier, pruned);
104         }
105     }
106
107     @Override
108     public void ready() {
109         delegate.ready();
110     }
111
112     @Override
113     public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
114         delegate.applyToCursor(dataTreeModificationCursor);
115     }
116
117     @Override
118     public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
119         return delegate.readNode(yangInstanceIdentifier);
120     }
121
122     @Override
123     public DataTreeModification newModification() {
124         return new PruningDataTreeModification(delegate.newModification(), validNamespaces);
125     }
126
127     @VisibleForTesting
128     NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?,?> input){
129         NormalizedNodePruner pruner = new NormalizedNodePruner(validNamespaces);
130         try {
131             NormalizedNodeWriter.forStreamWriter(pruner).write(input);
132         } catch (IOException ioe) {
133             LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
134         }
135
136         return pruner.normalizedNode();
137     }
138
139     public DataTreeModification getDelegate(){
140         return delegate;
141     }
142
143     private boolean isValidYangInstanceIdentifier(YangInstanceIdentifier instanceIdentifier){
144         for(YangInstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()){
145             if(!validNamespaces.contains(pathArgument.getNodeType().getNamespace())){
146                 return false;
147             }
148         }
149
150         return true;
151     }
152
153 }