Write failed node data on recovery to a file
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / AbstractDataTreeModificationCursor.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
19
20 /**
21  * Base class for a DataTreeModificationCursor.
22  *
23  * @author Thomas Pantelis
24  */
25 public abstract class AbstractDataTreeModificationCursor implements DataTreeModificationCursor {
26     private final Deque<YangInstanceIdentifier> stack = new ArrayDeque<>();
27
28     protected AbstractDataTreeModificationCursor() {
29         stack.push(YangInstanceIdentifier.EMPTY);
30     }
31
32     protected YangInstanceIdentifier next(@Nonnull final PathArgument child) {
33         return stack.peek().node(child);
34     }
35
36     @Override
37     public void enter(@Nonnull final PathArgument child) {
38         stack.push(stack.peek().node(child));
39     }
40
41     @Override
42     public void enter(@Nonnull final PathArgument... path) {
43         for (PathArgument arg : path) {
44             enter(arg);
45         }
46     }
47
48     @Override
49     public void enter(@Nonnull final Iterable<PathArgument> path) {
50         for (PathArgument arg : path) {
51             enter(arg);
52         }
53     }
54
55     @Override
56     public void exit() {
57         stack.pop();
58     }
59
60     @Override
61     public void exit(final int depth) {
62         Preconditions.checkArgument(depth < stack.size(), "Stack holds only %s elements, cannot exit %s levels", stack.size(), depth);
63         for (int i = 0; i < depth; ++i) {
64             stack.pop();
65         }
66     }
67
68     @Override
69     public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
70         throw new UnsupportedOperationException("Not implemented");
71     }
72
73     @Override
74     public void close() {
75         // No-op
76     }
77 }