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.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
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.builder.api.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Sets;
25 final class ChoiceNodeModification extends
26 AbstractContainerNodeModification<ChoiceNode, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> {
29 protected QName getQName(ChoiceNode schema) {
30 return schema.getQName();
34 protected Object findSchemaForChild(ChoiceNode schema, QName nodeType) {
35 return SchemaUtils.findSchemaForChild(schema, nodeType);
39 protected Set<InstanceIdentifier.PathArgument> getChildrenToProcess(ChoiceNode schema,
40 Optional<org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> actual,
41 Optional<org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> modification)
42 throws DataModificationException {
43 Set<InstanceIdentifier.PathArgument> childrenToProcess = super.getChildrenToProcess(schema, actual,
46 if (modification.isPresent() == false) {
47 return childrenToProcess;
50 // Detect case node from modification
51 ChoiceCaseNode detectedCase = null;
52 for (DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> child : modification.get().getValue()) {
53 Optional<ChoiceCaseNode> detectedCaseForChild = SchemaUtils.detectCase(schema, child);
55 if(detectedCaseForChild.isPresent() == false) {
56 DataModificationException.IllegalChoiceValuesException.throwUnknownChild(schema.getQName(),
60 if (detectedCase != null && detectedCase.equals(detectedCaseForChild.get()) == false) {
61 DataModificationException.IllegalChoiceValuesException.throwMultipleCasesReferenced(schema.getQName(),
62 modification.get(), detectedCase.getQName(), detectedCaseForChild.get().getQName());
64 detectedCase = detectedCaseForChild.get();
67 if (detectedCase == null)
68 return childrenToProcess;
70 // Filter out child nodes that do not belong to detected case =
71 // Nodes from other cases present in actual
72 Set<InstanceIdentifier.PathArgument> childrenToProcessFiltered = Sets.newLinkedHashSet();
73 for (InstanceIdentifier.PathArgument childToProcess : childrenToProcess) {
74 // child from other cases, skip
75 if (childToProcess instanceof AugmentationNode
76 && SchemaUtils.belongsToCaseAugment(detectedCase,
77 (InstanceIdentifier.AugmentationIdentifier) childToProcess) == false) {
79 } else if (belongsToCase(detectedCase, childToProcess) == false) {
83 childrenToProcessFiltered.add(childToProcess);
86 return childrenToProcessFiltered;
89 private boolean belongsToCase(ChoiceCaseNode detectedCase, InstanceIdentifier.PathArgument childToProcess) {
90 return detectedCase.getDataChildByName(childToProcess.getNodeType()) != null;
94 protected Object findSchemaForAugment(ChoiceNode schema, InstanceIdentifier.AugmentationIdentifier childToProcessId) {
95 return SchemaUtils.findSchemaForAugment(schema, childToProcessId.getPossibleChildNames());
99 protected DataContainerNodeBuilder<?, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode> getBuilder(
101 return Builders.choiceBuilder(schema);