2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.operations;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
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;
23 * Tracks netconf operations on nested nodes.
25 final class OperationStack {
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");
30 private final Deque<ModifyAction> operations = new LinkedList<>();
32 public OperationStack(ModifyAction operation) {
33 operations.add(operation);
36 public void enteringNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
37 if (!modificationNode.isPresent()) {
41 NormalizedNode<?, ?> modification = modificationNode.get();
43 enteringNode(modification);
46 public void enteringNode(NormalizedNode<?, ?> modificationNode) {
47 ModifyAction operation = getOperation(modificationNode);
48 if (operation == null) {
52 addOperation(operation);
55 private ModifyAction getOperation(NormalizedNode<?, ?> modificationNode) {
56 if (!(modificationNode instanceof AttributesContainer))
59 String operationString = ((AttributesContainer) modificationNode).getAttributes().get(OPERATION_NAME);
61 return operationString == null ? null : ModifyAction.fromXmlValue(operationString);
64 private void addOperation(ModifyAction operation) {
65 // Add check for permitted operation
66 operations.add(operation);
67 logger.trace("Operation added {}, {}", operation, this);
70 public ModifyAction getCurrentOperation() {
71 return operations.getLast();
74 public void exitingNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
75 if (!modificationNode.isPresent()) {
79 NormalizedNode<?, ?> modification = modificationNode.get();
81 exitingNode(modification);
84 public void exitingNode(NormalizedNode<?, ?> modification) {
85 ModifyAction operation = getOperation(modification);
86 if (operation == null) {
90 Preconditions.checkState(operations.size() > 1);
91 Preconditions.checkState(operations.peekLast().equals(operation), "Operations mismatch %s, %s",
92 operations.peekLast(), operation);
94 ModifyAction removed = operations.removeLast();
95 logger.trace("Operation removed {}, {}", removed, this);
99 public String toString() {
100 return operations.toString();