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.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;
23 final class ChoiceNodeModification extends AbstractContainerNodeModification<ChoiceSchemaNode, ChoiceNode> {
26 protected QName getQName(final ChoiceSchemaNode schema) {
27 return schema.getQName();
31 protected Object findSchemaForChild(final ChoiceSchemaNode schema, final QName nodeType) {
32 return SchemaUtils.findSchemaForChild(schema, nodeType);
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,
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;
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))) {
77 } else if (!belongsToCase(detectedCase, childToProcess)) {
81 childrenToProcessFiltered.add(childToProcess);
84 return childrenToProcessFiltered;
87 private boolean belongsToCase(final ChoiceCaseNode detectedCase, final YangInstanceIdentifier.PathArgument childToProcess) {
88 return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
92 protected Object findSchemaForAugment(final ChoiceSchemaNode schema, final YangInstanceIdentifier.AugmentationIdentifier childToProcessId) {
93 return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
97 protected DataContainerNodeBuilder<?, ChoiceNode> getBuilder(
98 final ChoiceSchemaNode schema) {
99 return Builders.choiceBuilder(schema);