Merge "Bug 447 - Yang documentation generator improvements"
[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.LinkedHashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerAttrNode;
21
22 import com.google.common.base.Preconditions;
23
24 public class ImmutableMapEntryNodeBuilder
25         extends AbstractImmutableDataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> {
26
27     protected final Map<QName, InstanceIdentifier.PathArgument> childrenQNamesToPaths;
28
29     protected ImmutableMapEntryNodeBuilder() {
30         this.childrenQNamesToPaths = new LinkedHashMap<>();
31     }
32
33     protected ImmutableMapEntryNodeBuilder(final ImmutableMapEntryNode node) {
34         super(node);
35         this.childrenQNamesToPaths = new LinkedHashMap<>();
36         fillQnames(node.getValue(), childrenQNamesToPaths);
37     }
38
39     public static DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create() {
40         return new ImmutableMapEntryNodeBuilder();
41     }
42
43     public static DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create(final MapEntryNode node) {
44         if (!(node instanceof ImmutableMapEntryNode)) {
45             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
46         }
47
48         return new ImmutableMapEntryNodeBuilder((ImmutableMapEntryNode)node);
49     }
50
51     private static void fillQnames(final Iterable<DataContainerChild<? extends PathArgument, ?>> iterable, final Map<QName, PathArgument> out) {
52         for (final DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> childId : iterable) {
53             final InstanceIdentifier.PathArgument identifier = childId.getIdentifier();
54
55             // Augmentation nodes cannot be keys, and do not have to be present in childrenQNamesToPaths map
56             if(identifier instanceof InstanceIdentifier.AugmentationIdentifier) {
57                 continue;
58             }
59
60             out.put(childId.getNodeType(), identifier);
61         }
62     }
63
64     // FIXME, find better solution than 2 maps (map from QName to Child ?)
65
66     @Override
67     public DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withValue(final List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
68         fillQnames(value, childrenQNamesToPaths);
69         return super.withValue(value);
70     }
71
72     @Override
73     public DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(final DataContainerChild<?, ?> child) {
74         // Augmentation nodes cannot be keys, and do not have to be present in childrenQNamesToPaths map
75         if(child.getIdentifier() instanceof InstanceIdentifier.AugmentationIdentifier == false) {
76             childrenQNamesToPaths.put(child.getNodeType(), child.getIdentifier());
77         }
78         return super.withChild(child);
79     }
80
81     @Override
82     public MapEntryNode build() {
83         checkKeys();
84         return new ImmutableMapEntryNode(getNodeIdentifier(), buildValue(), getAttributes());
85     }
86
87     private void checkKeys() {
88         for (final QName keyQName : getNodeIdentifier().getKeyValues().keySet()) {
89
90             final InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
91             final DataContainerChild<?, ?> childNode = getChild(childNodePath);
92
93             Preconditions.checkNotNull(childNode, "Key child node: %s, not present", keyQName);
94
95             final Object actualValue = getNodeIdentifier().getKeyValues().get(keyQName);
96             final Object expectedValue = childNode.getValue();
97             Preconditions.checkArgument(expectedValue.equals(actualValue),
98                     "Key child node with unexpected value, is: %s, should be: %s", actualValue, expectedValue);
99         }
100     }
101
102     private static final class ImmutableMapEntryNode extends AbstractImmutableDataContainerAttrNode<InstanceIdentifier.NodeIdentifierWithPredicates> implements MapEntryNode {
103
104         ImmutableMapEntryNode(final InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier,
105                               final Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children, final Map<QName, String> attributes) {
106             super(children, nodeIdentifier, attributes);
107         }
108     }
109 }