Implement edit-config operations for NormalizedNode yang-data-api
[yangtools.git] / yang / yang-data-operations / src / main / java / org / opendaylight / yangtools / yang / data / operations / OperationStack.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.yangtools.yang.data.operations;
9
10 import java.net.URI;
11 import java.util.Deque;
12 import java.util.LinkedList;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23
24 /**
25  * Tracks netconf operations on nested nodes.
26  */
27 final class OperationStack {
28
29     private static final Logger logger = LoggerFactory.getLogger(OperationStack.class);
30     private static final QName OPERATION_NAME = new QName(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "operation");
31
32     private final Deque<ModifyAction> operations = new LinkedList<>();
33
34     public OperationStack(ModifyAction operation) {
35         operations.add(operation);
36     }
37
38     public void enteringNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
39         if (modificationNode.isPresent() == false) {
40             return;
41         }
42
43         NormalizedNode<?, ?> modification = modificationNode.get();
44
45         enteringNode(modification);
46     }
47
48     public void enteringNode(NormalizedNode<?, ?> modificationNode) {
49         ModifyAction operation = getOperation(modificationNode);
50         if (operation == null) {
51             return;
52         }
53
54         addOperation(operation);
55     }
56
57     private ModifyAction getOperation(NormalizedNode<?, ?> modificationNode) {
58         if (modificationNode instanceof AttributesContainer == false)
59             return null;
60
61         String operationString = ((AttributesContainer) modificationNode).getAttributes().get(OPERATION_NAME);
62
63         return operationString == null ? null : ModifyAction.fromXmlValue(operationString);
64     }
65
66     private void addOperation(ModifyAction operation) {
67         // Add check for permitted operation
68         operations.add(operation);
69         logger.trace("Operation added {}, {}", operation, this);
70     }
71
72     public ModifyAction getCurrentOperation() {
73         return operations.getLast();
74     }
75
76     public void exitingNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
77         if (modificationNode.isPresent() == false) {
78             return;
79         }
80
81         NormalizedNode<?, ?> modification = modificationNode.get();
82
83         exitingNode(modification);
84     }
85
86     public void exitingNode(NormalizedNode<?, ?> modification) {
87         ModifyAction operation = getOperation(modification);
88         if (operation == null) {
89             return;
90         }
91
92         Preconditions.checkState(operations.size() > 1);
93         Preconditions.checkState(operations.peekLast().equals(operation), "Operations mismatch %s, %s",
94                 operations.peekLast(), operation);
95
96         ModifyAction removed = operations.removeLast();
97         logger.trace("Operation removed {}, {}", removed, this);
98     }
99
100     @Override
101         public String toString() {
102         return operations.toString();
103     }
104
105 }