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