90e36f736a11cb1b3c4e546b67a3aef1f4f4c522
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapEntryNodeBuilder.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.List;
11 import java.util.Map;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
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.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
19 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Maps;
23
24 public class ImmutableMapEntryNodeBuilder
25         extends AbstractImmutableDataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> {
26
27     protected final Map<QName, InstanceIdentifier.PathArgument> childrenQNamesToPaths;
28
29     protected ImmutableMapEntryNodeBuilder() {
30         this.childrenQNamesToPaths = Maps.newLinkedHashMap();
31     }
32
33     public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create() {
34         return new ImmutableMapEntryNodeBuilder();
35     }
36
37     // FIXME, find better solution than 2 maps (map from QName to Child ?)
38
39     @Override
40     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withValue(List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
41         for (DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> childId : value) {
42             this.childrenQNamesToPaths.put(childId.getNodeType(), childId.getIdentifier());
43         }
44         return super.withValue(value);
45     }
46
47     @Override
48     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(DataContainerChild<?, ?> child) {
49         childrenQNamesToPaths.put(child.getNodeType(), child.getIdentifier());
50         return super.withChild(child);
51     }
52
53     public MapEntryNode build() {
54         checkKeys();
55         return new ImmutableMapEntryNode(nodeIdentifier, value);
56     }
57
58     private void checkKeys() {
59         for (QName keyQName : nodeIdentifier.getKeyValues().keySet()) {
60
61             InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
62             DataContainerChild<?, ?> childNode = value.get(childNodePath);
63
64             Preconditions.checkNotNull(childNode, "Key child node: %s, not present", keyQName);
65
66             Object actualValue = nodeIdentifier.getKeyValues().get(keyQName);
67             Object expectedValue = childNode.getValue();
68             Preconditions.checkArgument(expectedValue.equals(actualValue),
69                     "Key child node with unexpected value, is: %s, should be: %s", actualValue, expectedValue);
70         }
71     }
72
73     static final class ImmutableMapEntryNode extends AbstractImmutableDataContainerNode<InstanceIdentifier.NodeIdentifierWithPredicates> implements MapEntryNode {
74
75         ImmutableMapEntryNode(InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier,
76                               Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children) {
77             super(children, nodeIdentifier);
78         }
79     }
80 }