Use callbacks while parsing to NormalizedNodes.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / ops / DataTreeChangeTracker.java
1 /*
2  * Copyright (c) 2015 Cisco 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
10
11 import com.google.common.collect.Lists;
12 import java.util.ArrayDeque;
13 import java.util.ArrayList;
14 import java.util.Deque;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 public class DataTreeChangeTracker {
21
22     private final ModifyAction defaultAction;
23
24     private final Deque<ModifyAction> actions;
25     private final Deque<PathArgument> currentPath;
26     private final ArrayList<DataTreeChange> dataTreeChanges;
27     private int deleteOperationTracker = 0;
28     private int removeOperationTracker = 0;
29
30     public DataTreeChangeTracker(final ModifyAction defaultAction) {
31         this.defaultAction = defaultAction;
32         this.currentPath = new ArrayDeque<>();
33         this.actions = new ArrayDeque<>();
34         this.dataTreeChanges = new ArrayList<>();
35     }
36
37     public void pushAction(final ModifyAction action) {
38         if (ModifyAction.DELETE.equals(action)) {
39             deleteOperationTracker++;
40         }
41
42         if (ModifyAction.REMOVE.equals(action)) {
43             removeOperationTracker++;
44         }
45         this.actions.push(action);
46     }
47
48     public ModifyAction peekAction() {
49         return this.actions.peekFirst();
50     }
51
52     public ModifyAction popAction() {
53         final ModifyAction popResult = actions.pop();
54         if (ModifyAction.DELETE.equals(popResult)) {
55             deleteOperationTracker--;
56         }
57
58         if (ModifyAction.REMOVE.equals(popResult)) {
59             removeOperationTracker--;
60         }
61         return popResult;
62     }
63
64     public int getDeleteOperationTracker() {
65         return deleteOperationTracker;
66     }
67
68     public int getRemoveOperationTracker() {
69         return removeOperationTracker;
70     }
71
72     public void addDataTreeChange(final DataTreeChange change) {
73         dataTreeChanges.add(change);
74     }
75
76     public ArrayList<DataTreeChange> getDataTreeChanges() {
77         return dataTreeChanges;
78     }
79
80     public ModifyAction getDefaultAction() {
81         return defaultAction;
82     }
83
84     public void pushPath(final PathArgument pathArgument) {
85         currentPath.push(pathArgument);
86     }
87
88     public PathArgument popPath() {
89         return currentPath.pop();
90     }
91
92     public Deque<PathArgument> getCurrentPath() {
93         return currentPath;
94     }
95
96
97     public static final class DataTreeChange {
98
99         private final NormalizedNode<?, ?> changeRoot;
100         private final ModifyAction action;
101         private final List<PathArgument> path;
102
103         public DataTreeChange(final NormalizedNode<?, ?> changeRoot, final ModifyAction action, final ArrayList<PathArgument> path) {
104             this.changeRoot = changeRoot;
105             this.action = action;
106             this.path = Lists.reverse(path);
107         }
108
109         public NormalizedNode<?, ?> getChangeRoot() {
110             return changeRoot;
111         }
112
113         public ModifyAction getAction() {
114             return action;
115         }
116
117         public List<PathArgument> getPath() {
118             return path;
119         }
120     }
121 }