39990a57769248891013d996f82eeb9557e8a525
[yangtools.git] / yang / yang-data-operations / src / main / java / org / opendaylight / yangtools / yang / data / operations / LeafNodeModification.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 org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
11 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
12
13 import com.google.common.base.Optional;
14
15 final class LeafNodeModification implements Modification<LeafSchemaNode, LeafNode<?>> {
16
17     @Override
18     public Optional<LeafNode<?>> modify(LeafSchemaNode schema, Optional<LeafNode<?>> actualNode,
19                                     Optional<LeafNode<?>> modificationNode, OperationStack operationStack) throws DataModificationException {
20
21         operationStack.enteringNode(modificationNode);
22
23         Optional<LeafNode<?>> result;
24
25         // Returns either actual node, modification node or empty in case of removal
26         switch (operationStack.getCurrentOperation()) {
27             case MERGE: {
28                 result = modificationNode.isPresent() ? modificationNode : actualNode;
29                 break;
30             }
31             case CREATE: {
32                 DataModificationException.DataExistsException.check(schema.getQName(), actualNode, null);
33             }
34             case REPLACE: {
35                 result = modificationNode;
36                 break;
37             }
38             case DELETE: {
39                 DataModificationException.DataMissingException.check(schema.getQName(), actualNode);
40             }
41             case REMOVE: {
42                 result = Optional.absent();
43                 break;
44             }
45             case NONE: {
46                 result = actualNode;
47                 break;
48             }
49             default:
50                 throw new UnsupportedOperationException(String.format("Unable to perform operation: %s on: %s, unknown", operationStack.getCurrentOperation(), schema));
51
52         }
53
54         operationStack.exitingNode(modificationNode);
55
56         return result;
57     }
58 }