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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
27 * before delegating it to the actual DataTreeModification
29 public class PruningDataTreeModification implements DataTreeModification {
31 private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
32 private final DataTreeModification delegate;
33 private final Set<URI> validNamespaces;
35 public PruningDataTreeModification(DataTreeModification delegate, Set<URI> validNamespaces) {
36 this.delegate = delegate;
37 this.validNamespaces = validNamespaces;
41 public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
43 delegate.delete(yangInstanceIdentifier);
44 } catch(IllegalArgumentException e){
45 LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
50 public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
52 delegate.merge(yangInstanceIdentifier, normalizedNode);
53 } catch (IllegalArgumentException e){
54 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
55 LOG.warn("Invalid node identifier {} ignoring merge", yangInstanceIdentifier);
59 LOG.warn("Node at path : {} was pruned during merge", yangInstanceIdentifier);
61 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
64 delegate.merge(yangInstanceIdentifier, pruned);
71 public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
73 delegate.write(yangInstanceIdentifier, normalizedNode);
74 } catch (IllegalArgumentException e){
75 if(!isValidYangInstanceIdentifier(yangInstanceIdentifier)){
76 LOG.warn("Invalid node identifier {} ignoring write", yangInstanceIdentifier);
80 LOG.warn("Node at path : {} was pruned during write", yangInstanceIdentifier);
82 NormalizedNode<?,?> pruned = pruneNormalizedNode(normalizedNode);
85 delegate.write(yangInstanceIdentifier, pruned);
96 public void applyToCursor(DataTreeModificationCursor dataTreeModificationCursor) {
97 delegate.applyToCursor(dataTreeModificationCursor);
101 public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier yangInstanceIdentifier) {
102 return delegate.readNode(yangInstanceIdentifier);
106 public DataTreeModification newModification() {
107 return new PruningDataTreeModification(delegate.newModification(), validNamespaces);
111 NormalizedNode<?, ?> pruneNormalizedNode(NormalizedNode<?,?> input){
112 NormalizedNodePruner pruner = new NormalizedNodePruner(validNamespaces);
114 NormalizedNodeWriter.forStreamWriter(pruner).write(input);
115 } catch (IOException ioe) {
116 LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
119 return pruner.normalizedNode();
122 public DataTreeModification getDelegate(){
126 private boolean isValidYangInstanceIdentifier(YangInstanceIdentifier instanceIdentifier){
127 for(YangInstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()){
128 if(!validNamespaces.contains(pathArgument.getNodeType().getNamespace())){