Fix forwarding in Shard.handleBatchedModifications
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / AbstractBatchedModificationsCursor.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
16 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
17 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
18 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
23
24 /**
25  * Base class for a DataTreeModificationCursor that publishes to BatchedModifications instance(s).
26  *
27  * @author Thomas Pantelis
28  */
29 public abstract class AbstractBatchedModificationsCursor implements DataTreeModificationCursor {
30     private final Deque<YangInstanceIdentifier> stack = new ArrayDeque<>();
31
32     protected AbstractBatchedModificationsCursor() {
33         stack.push(YangInstanceIdentifier.EMPTY);
34     }
35
36     protected abstract BatchedModifications getModifications();
37
38     @Override
39     public void delete(final PathArgument child) {
40         getModifications().addModification(new DeleteModification(stack.peek().node(child)));
41     }
42
43     @Override
44     public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
45         getModifications().addModification(new MergeModification(stack.peek().node(child), data));
46     }
47
48     @Override
49     public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
50         getModifications().addModification(new WriteModification(stack.peek().node(child), data));
51     }
52
53     @Override
54     public void enter(@Nonnull final PathArgument child) {
55         stack.push(stack.peek().node(child));
56     }
57
58     @Override
59     public void enter(@Nonnull final PathArgument... path) {
60         for (PathArgument arg : path) {
61             enter(arg);
62         }
63     }
64
65     @Override
66     public void enter(@Nonnull final Iterable<PathArgument> path) {
67         for (PathArgument arg : path) {
68             enter(arg);
69         }
70     }
71
72     @Override
73     public void exit() {
74         stack.pop();
75     }
76
77     @Override
78     public void exit(final int depth) {
79         Preconditions.checkArgument(depth < stack.size(), "Stack holds only %s elements, cannot exit %s levels", stack.size(), depth);
80         for (int i = 0; i < depth; ++i) {
81             stack.pop();
82         }
83     }
84
85     @Override
86     public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
87         throw new UnsupportedOperationException("Not implemented");
88     }
89
90     @Override
91     public void close() {
92         // No-op
93     }
94 }