Merge changes Iaac9f7e2,I9c336a30
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableAugmentationNodeSchemaAwareBuilder.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 java.util.Set;
11
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20
21 import com.google.common.collect.Sets;
22
23 public class ImmutableAugmentationNodeSchemaAwareBuilder extends ImmutableAugmentationNodeBuilder {
24
25     private final DataNodeContainerValidator validator;
26
27     protected ImmutableAugmentationNodeSchemaAwareBuilder(AugmentationSchema schema) {
28         this.validator = new DataNodeContainerValidator(schema);
29         // TODO no QName for augmentation
30         super.withNodeIdentifier(new InstanceIdentifier.AugmentationIdentifier(null, getChildQNames(schema)));
31     }
32
33     // TODO move somewhere to UTIL
34     public static Set<QName> getChildQNames(AugmentationSchema schema) {
35         Set<QName> qnames = Sets.newHashSet();
36
37         for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
38             qnames.add(dataSchemaNode.getQName());
39         }
40
41         return qnames;
42     }
43
44     @Override
45     public DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> withNodeIdentifier(InstanceIdentifier.AugmentationIdentifier nodeIdentifier) {
46         throw new UnsupportedOperationException("Node identifier created from schema");
47     }
48
49     @Override
50     public DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> withChild(DataContainerChild<?, ?> child) {
51         validator.validateChild(child.getIdentifier());
52         return super.withChild(child);
53     }
54
55     public static DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> create(AugmentationSchema schema) {
56         return new ImmutableAugmentationNodeSchemaAwareBuilder(schema);
57     }
58
59
60 }