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.impl.schema.builder.impl;
10 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
13 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import com.google.common.base.Preconditions;
20 public class ImmutableChoiceNodeSchemaAwareBuilder extends ImmutableChoiceNodeBuilder {
22 private final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema;
23 private ChoiceCaseNode detectedCase;
24 private DataNodeContainerValidator validator;
26 protected ImmutableChoiceNodeSchemaAwareBuilder(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
29 super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
33 public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
34 throw new UnsupportedOperationException("Node identifier created from schema");
38 public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> withChild(DataContainerChild<?, ?> child) {
39 if(detectedCase == null) {
40 detectedCase = detectCase(child);
41 validator = new DataNodeContainerValidator(detectedCase);
44 validator.validateChild(child.getIdentifier());
46 return super.withChild(child);
50 public ChoiceNode build() {
51 // TODO validate when statement
55 private ChoiceCaseNode detectCase(DataContainerChild<?, ?> child) {
56 for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
57 for (DataSchemaNode childFromCase : choiceCaseNode.getChildNodes()) {
58 if (childFromCase.getQName().equals(child.getNodeType())) {
59 return choiceCaseNode;
64 throw new IllegalArgumentException(String.format("Unknown child node: %s, for choice: %s", child.getNodeType(),
68 public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> create(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
69 return new ImmutableChoiceNodeSchemaAwareBuilder(schema);