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;
11 import java.util.Deque;
12 import java.util.LinkedList;
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;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
25 * Tracks netconf operations on nested nodes.
27 final class OperationStack {
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");
32 private final Deque<ModifyAction> operations = new LinkedList<>();
34 public OperationStack(ModifyAction operation) {
35 operations.add(operation);
38 public void enteringNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
39 if (modificationNode.isPresent() == false) {
43 NormalizedNode<?, ?> modification = modificationNode.get();
45 enteringNode(modification);
48 public void enteringNode(NormalizedNode<?, ?> modificationNode) {
49 ModifyAction operation = getOperation(modificationNode);
50 if (operation == null) {
54 addOperation(operation);
57 private ModifyAction getOperation(NormalizedNode<?, ?> modificationNode) {
58 if (modificationNode instanceof AttributesContainer == false)
61 String operationString = ((AttributesContainer) modificationNode).getAttributes().get(OPERATION_NAME);
63 return operationString == null ? null : ModifyAction.fromXmlValue(operationString);
66 private void addOperation(ModifyAction operation) {
67 // Add check for permitted operation
68 operations.add(operation);
69 logger.trace("Operation added {}, {}", operation, this);
72 public ModifyAction getCurrentOperation() {
73 return operations.getLast();
76 public void exitingNode(Optional<? extends NormalizedNode<?, ?>> modificationNode) {
77 if (modificationNode.isPresent() == false) {
81 NormalizedNode<?, ?> modification = modificationNode.get();
83 exitingNode(modification);
86 public void exitingNode(NormalizedNode<?, ?> modification) {
87 ModifyAction operation = getOperation(modification);
88 if (operation == null) {
92 Preconditions.checkState(operations.size() > 1);
93 Preconditions.checkState(operations.peekLast().equals(operation), "Operations mismatch %s, %s",
94 operations.peekLast(), operation);
96 ModifyAction removed = operations.removeLast();
97 logger.trace("Operation removed {}, {}", removed, this);
101 public String toString() {
102 return operations.toString();