7c50a74d544c7594060cd49057c6287959556e9a
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21
22 public class ImmutableChoiceNodeSchemaAwareBuilder extends ImmutableChoiceNodeBuilder {
23
24     private final ChoiceSchemaNode schema;
25     private DataNodeContainerValidator validator;
26
27     protected ImmutableChoiceNodeSchemaAwareBuilder(final ChoiceSchemaNode schema) {
28         this.schema = Preconditions.checkNotNull(schema, "Schema was null");
29         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
30     }
31
32     @Override
33     public DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> withNodeIdentifier(
34             final NodeIdentifier nodeIdentifier) {
35         throw new UnsupportedOperationException("Node identifier created from schema");
36     }
37
38     @Override
39     public DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> withChild(final DataContainerChild<?, ?> child) {
40         if (validator == null) {
41             Optional<ChoiceCaseNode> detectedCaseOpt = SchemaUtils.detectCase(schema, child);
42             DataValidationException.checkLegalChild(detectedCaseOpt.isPresent(), child.getIdentifier(), schema);
43             validator = new DataNodeContainerValidator(detectedCaseOpt.get());
44         }
45
46         return super.withChild(validator.validateChild(child));
47     }
48
49     @Override
50     public ChoiceNode build() {
51         // TODO validate when statement
52         return super.build();
53     }
54
55     public static DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> create(final ChoiceSchemaNode schema) {
56         return new ImmutableChoiceNodeSchemaAwareBuilder(schema);
57     }
58 }