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