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 delegate.merge(yangInstanceIdentifier, normalizedNode);
54 } catch (SchemaValidationFailedException e){
55 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
56 LOG.warn("Invalid node identifier {} ignoring merge", yangInstanceIdentifier);
60 LOG.warn("Node at path : {} was pruned during merge", yangInstanceIdentifier);
62 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
65 delegate.merge(yangInstanceIdentifier, pruned);
72 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
74 delegate.write(yangInstanceIdentifier, normalizedNode);
75 } catch (SchemaValidationFailedException e){
76 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
77 LOG.warn("Invalid node identifier {} ignoring write", yangInstanceIdentifier);
81 LOG.warn("Node at path : {} was pruned during write", yangInstanceIdentifier);
83 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
86 delegate.write(yangInstanceIdentifier, pruned);
97 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
98 delegate.applyToCursor(dataTreeModificationCursor);
102 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
103 return delegate.readNode(yangInstanceIdentifier);
107 public DataTreeModification newModification() {
108 return new PruningDataTreeModification(delegate.newModification(), validNamespaces);
112 NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?,?> input){
113 NormalizedNodePruner pruner = new NormalizedNodePruner(validNamespaces);
115 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
116 } catch (IOException ioe) {
117 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
120 return pruner.normalizedNode();
123 public DataTreeModification getDelegate(){
127 private boolean isValidYangInstanceIdentifier(YangInstanceIdentifier instanceIdentifier){
128 for(YangInstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()){
129 if(!validNamespaces.contains(pathArgument.getNodeType().getNamespace())){