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;
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;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.Sets;
24 final class ChoiceNodeModification extends
25 AbstractContainerNodeModification<ChoiceNode, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> {
28 protected QName getQName(ChoiceNode schema) {
29 return schema.getQName();
33 protected Object findSchemaForChild(ChoiceNode schema, QName nodeType) {
34 return SchemaUtils.findSchemaForChild(schema, nodeType);
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,
45 if (modification.isPresent() == false) {
46 return childrenToProcess;
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);
54 if(detectedCaseForChild.isPresent() == false) {
55 DataModificationException.IllegalChoiceValuesException.throwUnknownChild(schema.getQName(),
59 if (detectedCase != null && detectedCase.equals(detectedCaseForChild.get()) == false) {
60 DataModificationException.IllegalChoiceValuesException.throwMultipleCasesReferenced(schema.getQName(),
61 modification.get(), detectedCase.getQName(), detectedCaseForChild.get().getQName());
63 detectedCase = detectedCaseForChild.get();
66 if (detectedCase == null)
67 return childrenToProcess;
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) {
78 } else if (belongsToCase(detectedCase, childToProcess) == false) {
82 childrenToProcessFiltered.add(childToProcess);
85 return childrenToProcessFiltered;
88 private boolean belongsToCase(ChoiceCaseNode detectedCase, YangInstanceIdentifier.PathArgument childToProcess) {
89 return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
93 protected Object findSchemaForAugment(ChoiceNode schema, YangInstanceIdentifier.AugmentationIdentifier childToProcessId) {
94 return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
98 protected DataContainerNodeBuilder<?, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> getBuilder(
100 return Builders.choiceBuilder(schema);