2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.operations;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Sets;
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;
22 final class ChoiceNodeModification extends
23 AbstractContainerNodeModification<ChoiceNode, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> {
26 protected QName getQName(ChoiceNode schema) {
27 return schema.getQName();
31 protected Object findSchemaForChild(ChoiceNode schema, QName nodeType) {
32 return SchemaUtils.findSchemaForChild(schema, nodeType);
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,
43 if (!modification.isPresent()) {
44 return childrenToProcess;
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);
52 if(!detectedCaseForChild.isPresent()) {
53 DataModificationException.IllegalChoiceValuesException.throwUnknownChild(schema.getQName(),
57 if (detectedCase != null && (!detectedCase.equals(detectedCaseForChild.get()))) {
58 DataModificationException.IllegalChoiceValuesException.throwMultipleCasesReferenced(schema.getQName(),
59 modification.get(), detectedCase.getQName(), detectedCaseForChild.get().getQName());
61 detectedCase = detectedCaseForChild.get();
64 if (detectedCase == null)
65 return childrenToProcess;
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))) {
76 } else if (!belongsToCase(detectedCase, childToProcess)) {
80 childrenToProcessFiltered.add(childToProcess);
83 return childrenToProcessFiltered;
86 private boolean belongsToCase(ChoiceCaseNode detectedCase, YangInstanceIdentifier.PathArgument childToProcess) {
87 return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
91 protected Object findSchemaForAugment(ChoiceNode schema, YangInstanceIdentifier.AugmentationIdentifier childToProcessId) {
92 return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
96 protected DataContainerNodeBuilder<?, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> getBuilder(
98 return Builders.choiceBuilder(schema);