Merge "Bug 1354: Added rpcReplyToDomNodes method."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapEntryNodeSchemaAwareBuilder.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.Collection;
11 import java.util.Map;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Maps;
24
25 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder{
26
27     private final ListSchemaNode schema;
28     private final DataNodeContainerValidator validator;
29
30     protected ImmutableMapEntryNodeSchemaAwareBuilder(final ListSchemaNode schema) {
31         this.schema = Preconditions.checkNotNull(schema);
32         this.validator = new DataNodeContainerValidator(schema);
33     }
34
35     @Override
36     public ImmutableMapEntryNodeBuilder withNodeIdentifier(final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
37         throw new UnsupportedOperationException("Node identifier created from schema");
38     }
39
40     @Override
41     public DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(final DataContainerChild<?, ?> child) {
42         validator.validateChild(child.getIdentifier());
43         return super.withChild(child);
44     }
45
46     @Override
47     public MapEntryNode build() {
48         super.withNodeIdentifier(constructNodeIdentifier());
49         return super.build();
50     }
51
52     /**
53      * Build map entry node identifier from schema, and provided children
54      */
55     private YangInstanceIdentifier.NodeIdentifierWithPredicates constructNodeIdentifier() {
56         Collection<QName> keys = schema.getKeyDefinition();
57
58         if(keys.isEmpty()) {
59             keys = childrenQNamesToPaths.keySet();
60         }
61
62         final Map<QName, Object> keysToValues = Maps.newHashMap();
63         for (QName key : keys) {
64         final DataContainerChild<?, ?> valueForKey = getChild(childrenQNamesToPaths.get(key));
65             DataValidationException.checkListKey(valueForKey, key, new YangInstanceIdentifier.NodeIdentifierWithPredicates(
66                     schema.getQName(), keysToValues));
67             keysToValues.put(key, valueForKey.getValue());
68         }
69
70         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(schema.getQName(), keysToValues);
71     }
72
73     public static DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create(final ListSchemaNode schema) {
74         return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
75     }
76
77 }