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