Merge "Fix for Bug 511 (model + integration-test)"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableContainerNodeSchemaAwareBuilder.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.ContainerNode;
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.ContainerSchemaNode;
16
17 public final class ImmutableContainerNodeSchemaAwareBuilder extends ImmutableContainerNodeBuilder {
18
19     private final DataNodeContainerValidator validator;
20
21     // TODO remove schema aware builders, replace by validator called at build()
22
23     private ImmutableContainerNodeSchemaAwareBuilder(ContainerSchemaNode schema) {
24         super();
25         this.validator = new DataNodeContainerValidator(schema);
26         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
27     }
28
29     public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> create(ContainerSchemaNode schema) {
30         return new ImmutableContainerNodeSchemaAwareBuilder(schema);
31     }
32
33     @Override
34     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
35         throw new UnsupportedOperationException("Node identifier created from schema");
36     }
37
38     @Override
39     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> withChild(DataContainerChild<?, ?> child) {
40         validator.validateChild(child.getIdentifier());
41         return super.withChild(child);
42     }
43
44     @Override
45     public ContainerNode build() {
46         // TODO check when statements... somewhere
47         return super.build();
48     }
49 }