Implement edit-config operations for NormalizedNode yang-data-api
[yangtools.git] / yang / yang-data-operations / src / main / java / org / opendaylight / yangtools / yang / data / operations / ChoiceNodeModification.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.util.Set;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.SchemaUtils;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Sets;
24
25 final class ChoiceNodeModification extends
26         AbstractContainerNodeModification<ChoiceNode, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> {
27
28     @Override
29     protected QName getQName(ChoiceNode schema) {
30         return schema.getQName();
31     }
32
33     @Override
34     protected Object findSchemaForChild(ChoiceNode schema, QName nodeType) {
35         return SchemaUtils.findSchemaForChild(schema, nodeType);
36     }
37
38     @Override
39     protected Set<InstanceIdentifier.PathArgument> getChildrenToProcess(ChoiceNode schema,
40             Optional<org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> actual,
41             Optional<org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> modification)
42             throws DataModificationException {
43         Set<InstanceIdentifier.PathArgument> childrenToProcess = super.getChildrenToProcess(schema, actual,
44                 modification);
45
46         if (modification.isPresent() == false) {
47             return childrenToProcess;
48         }
49
50         // Detect case node from modification
51         ChoiceCaseNode detectedCase = null;
52         for (DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> child : modification.get().getValue()) {
53             Optional<ChoiceCaseNode> detectedCaseForChild = SchemaUtils.detectCase(schema, child);
54
55             if(detectedCaseForChild.isPresent() == false) {
56                 DataModificationException.IllegalChoiceValuesException.throwUnknownChild(schema.getQName(),
57                         child.getNodeType());
58             }
59
60             if (detectedCase != null && detectedCase.equals(detectedCaseForChild.get()) == false) {
61                 DataModificationException.IllegalChoiceValuesException.throwMultipleCasesReferenced(schema.getQName(),
62                         modification.get(), detectedCase.getQName(), detectedCaseForChild.get().getQName());
63             }
64             detectedCase = detectedCaseForChild.get();
65         }
66
67         if (detectedCase == null)
68             return childrenToProcess;
69
70         // Filter out child nodes that do not belong to detected case =
71         // Nodes from other cases present in actual
72         Set<InstanceIdentifier.PathArgument> childrenToProcessFiltered = Sets.newLinkedHashSet();
73         for (InstanceIdentifier.PathArgument childToProcess : childrenToProcess) {
74             // child from other cases, skip
75             if (childToProcess instanceof AugmentationNode
76                     && SchemaUtils.belongsToCaseAugment(detectedCase,
77                             (InstanceIdentifier.AugmentationIdentifier) childToProcess) == false) {
78                 continue;
79             } else if (belongsToCase(detectedCase, childToProcess) == false) {
80                 continue;
81             }
82
83             childrenToProcessFiltered.add(childToProcess);
84         }
85
86         return childrenToProcessFiltered;
87     }
88
89     private boolean belongsToCase(ChoiceCaseNode detectedCase, InstanceIdentifier.PathArgument childToProcess) {
90         return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
91     }
92
93     @Override
94     protected Object findSchemaForAugment(ChoiceNode schema, InstanceIdentifier.AugmentationIdentifier childToProcessId) {
95         return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
96     }
97
98     @Override
99     protected DataContainerNodeBuilder<?, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> getBuilder(
100             ChoiceNode schema) {
101         return Builders.choiceBuilder(schema);
102     }
103 }