Merge "Fix for Bug 495."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableChoiceNodeSchemaAwareBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
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;
17
18 import com.google.common.base.Preconditions;
19
20 public class ImmutableChoiceNodeSchemaAwareBuilder extends ImmutableChoiceNodeBuilder {
21
22     private final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema;
23     private ChoiceCaseNode detectedCase;
24     private DataNodeContainerValidator validator;
25
26     protected ImmutableChoiceNodeSchemaAwareBuilder(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
27         super();
28         this.schema = schema;
29         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
30     }
31
32     @Override
33     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
34         throw new UnsupportedOperationException("Node identifier created from schema");
35     }
36
37     @Override
38     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> withChild(DataContainerChild<?, ?> child) {
39         if(detectedCase == null) {
40             detectedCase = detectCase(child);
41             validator = new DataNodeContainerValidator(detectedCase);
42         }
43
44         validator.validateChild(child.getIdentifier());
45
46         return super.withChild(child);
47     }
48
49     @Override
50     public ChoiceNode build() {
51         // TODO validate when statement
52         return super.build();
53     }
54
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;
60                 }
61             }
62         }
63
64         throw new IllegalArgumentException(String.format("Unknown child node: %s, for choice: %s", child.getNodeType(),
65                 schema.getQName()));
66     }
67
68     public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> create(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
69         return new ImmutableChoiceNodeSchemaAwareBuilder(schema);
70     }
71 }