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;
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;
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
30 public class PruningDataTreeModification implements DataTreeModification {
32 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
33 private final DataTreeModification delegate;
34 private final Set<URI> validNamespaces;
36 public PruningDataTreeModification(DataTreeModification delegate, Set<URI> validNamespaces) {
37 this.delegate = delegate;
38 this.validNamespaces = validNamespaces;
42 public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
44 delegate.delete(yangInstanceIdentifier);
45 } catch(SchemaValidationFailedException e){
46 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
51 public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
53 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
54 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
56 delegate.merge(yangInstanceIdentifier, normalizedNode);
58 } catch (SchemaValidationFailedException e){
59 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
60 LOG.warn("Invalid node identifier {} ignoring merge", yangInstanceIdentifier);
64 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
69 private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
70 LOG.warn("Node at path : {} was pruned during merge", yangInstanceIdentifier);
72 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
75 delegate.merge(yangInstanceIdentifier, pruned);
80 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
82 if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
83 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
85 delegate.write(yangInstanceIdentifier, normalizedNode);
87 } catch (SchemaValidationFailedException e){
88 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
89 LOG.warn("Invalid node identifier {} ignoring write", yangInstanceIdentifier);
93 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
97 private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
98 LOG.warn("Node at path : {} was pruned during write", yangInstanceIdentifier);
100 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
103 delegate.write(yangInstanceIdentifier, pruned);
108 public void ready() {
113 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
114 delegate.applyToCursor(dataTreeModificationCursor);
118 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
119 return delegate.readNode(yangInstanceIdentifier);
123 public DataTreeModification newModification() {
124 return new PruningDataTreeModification(delegate.newModification(), validNamespaces);
128 NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?,?> input){
129 NormalizedNodePruner pruner = new NormalizedNodePruner(validNamespaces);
131 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
132 } catch (IOException ioe) {
133 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
136 return pruner.normalizedNode();
139 public DataTreeModification getDelegate(){
143 private boolean isValidYangInstanceIdentifier(YangInstanceIdentifier instanceIdentifier){
144 for(YangInstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()){
145 if(!validNamespaces.contains(pathArgument.getNodeType().getNamespace())){