1f07eb555302dc14e1d6b7803bc26e559e5df269
[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.base.Preconditions;
22 import com.google.common.collect.Sets;
23
24 public class ImmutableAugmentationNodeSchemaAwareBuilder extends ImmutableAugmentationNodeBuilder {
25
26     private final DataNodeContainerValidator validator;
27
28     protected ImmutableAugmentationNodeSchemaAwareBuilder(AugmentationSchema schema) {
29         super();
30         this.validator = new DataNodeContainerValidator(schema);
31         // TODO no QName for augmentation
32         super.withNodeIdentifier(new InstanceIdentifier.AugmentationIdentifier(null, getChildQNames(schema)));
33     }
34
35     // TODO move somewhere to UTIL
36     public static Set<QName> getChildQNames(AugmentationSchema schema) {
37         Set<QName> qnames = Sets.newHashSet();
38
39         for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
40             qnames.add(dataSchemaNode.getQName());
41         }
42
43         return qnames;
44     }
45
46     @Override
47     public DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> withNodeIdentifier(InstanceIdentifier.AugmentationIdentifier nodeIdentifier) {
48         throw new UnsupportedOperationException("Node identifier created from schema");
49     }
50
51     @Override
52     public DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> withChild(DataContainerChild<?, ?> child) {
53         validator.validateChild(child.getIdentifier());
54         return super.withChild(child);
55     }
56
57     public static DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> create(AugmentationSchema schema) {
58         return new ImmutableAugmentationNodeSchemaAwareBuilder(schema);
59     }
60
61
62 }