Resolved some sonar issues: Simplify Boolean Expression
[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.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
17 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21
22 final class ChoiceNodeModification extends
23         AbstractContainerNodeModification<ChoiceNode, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> {
24
25     @Override
26     protected QName getQName(ChoiceNode schema) {
27         return schema.getQName();
28     }
29
30     @Override
31     protected Object findSchemaForChild(ChoiceNode schema, QName nodeType) {
32         return SchemaUtils.findSchemaForChild(schema, nodeType);
33     }
34
35     @Override
36     protected Set<YangInstanceIdentifier.PathArgument> getChildrenToProcess(ChoiceNode schema,
37             Optional<org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> actual,
38             Optional<org.opendaylight.yangtools.yang.data.api.schema.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         // Filter out child nodes that do not belong to detected case =
68         // Nodes from other cases present in actual
69         Set<YangInstanceIdentifier.PathArgument> childrenToProcessFiltered = Sets.newLinkedHashSet();
70         for (YangInstanceIdentifier.PathArgument childToProcess : childrenToProcess) {
71             // child from other cases, skip
72             if (childToProcess instanceof YangInstanceIdentifier.AugmentationIdentifier
73                     && (!SchemaUtils.belongsToCaseAugment(detectedCase,
74                             (YangInstanceIdentifier.AugmentationIdentifier) childToProcess))) {
75                 continue;
76             } else if (!belongsToCase(detectedCase, childToProcess)) {
77                 continue;
78             }
79
80             childrenToProcessFiltered.add(childToProcess);
81         }
82
83         return childrenToProcessFiltered;
84     }
85
86     private boolean belongsToCase(ChoiceCaseNode detectedCase, YangInstanceIdentifier.PathArgument childToProcess) {
87         return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
88     }
89
90     @Override
91     protected Object findSchemaForAugment(ChoiceNode schema, YangInstanceIdentifier.AugmentationIdentifier childToProcessId) {
92         return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
93     }
94
95     @Override
96     protected DataContainerNodeBuilder<?, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> getBuilder(
97             ChoiceNode schema) {
98         return Builders.choiceBuilder(schema);
99     }
100 }