5988554428c6806fbfd8e0f49fe724cc40bd0015
[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.data.impl.schema.transform.base.SchemaUtils;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
17
18 import com.google.common.base.Preconditions;
19 import com.google.common.base.Supplier;
20
21 public class ImmutableChoiceNodeSchemaAwareBuilder extends ImmutableChoiceNodeBuilder {
22
23     private final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema;
24     private ChoiceCaseNode detectedCase;
25     private DataNodeContainerValidator validator;
26
27     protected ImmutableChoiceNodeSchemaAwareBuilder(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
28         this.schema = Preconditions.checkNotNull(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(final DataContainerChild<?, ?> child) {
39         if(detectedCase == null) {
40             detectedCase = SchemaUtils.detectCase(schema, child).or(new Supplier<ChoiceCaseNode>() {
41                 @Override
42                 public ChoiceCaseNode get() {
43                     throw new IllegalArgumentException(String.format("Unknown child node: %s, for choice: %s", child.getNodeType(),
44                             schema.getQName()));
45                 }
46             });
47             validator = new DataNodeContainerValidator(detectedCase);
48         }
49
50         validator.validateChild(child.getIdentifier());
51
52         return super.withChild(child);
53     }
54
55     @Override
56     public ChoiceNode build() {
57         // TODO validate when statement
58         return super.build();
59     }
60
61     public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ChoiceNode> create(org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
62         return new ImmutableChoiceNodeSchemaAwareBuilder(schema);
63     }
64 }