f5cb003f05947bfcc92f095e499ee4a7a0362e1c
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException.IllegalListKeyException;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28
29 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder {
30
31     private final ListSchemaNode schema;
32     private final DataNodeContainerValidator validator;
33
34     ImmutableMapEntryNodeSchemaAwareBuilder(final ListSchemaNode schema) {
35         this.schema = requireNonNull(schema);
36         this.validator = new DataNodeContainerValidator(schema);
37     }
38
39     @Override
40     public ImmutableMapEntryNodeBuilder withNodeIdentifier(final NodeIdentifierWithPredicates withNodeIdentifier) {
41         throw new UnsupportedOperationException("Node identifier created from schema");
42     }
43
44     @Override
45     public DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> withChild(
46             final DataContainerChild child) {
47         validator.validateChild(child.getIdentifier());
48         return super.withChild(child);
49     }
50
51     @Override
52     public MapEntryNode build() {
53         super.withNodeIdentifier(constructNodeIdentifier());
54         return super.build();
55     }
56
57     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> create(
58             final ListSchemaNode schema) {
59         return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
60     }
61
62     /**
63      * Build map entry node identifier from schema and provided children.
64      */
65     private NodeIdentifierWithPredicates constructNodeIdentifier() {
66         final Map<QName, Object> predicates;
67         final Collection<QName> keys = schema.getKeyDefinition();
68         if (!keys.isEmpty()) {
69             predicates = keyDefToPredicates(keys);
70         } else if (!childrenQNamesToPaths.isEmpty()) {
71             predicates = childrenToPredicates();
72         } else {
73             predicates = ImmutableMap.of();
74         }
75         return NodeIdentifierWithPredicates.of(schema.getQName(), predicates);
76     }
77
78     private Map<QName, Object> childrenToPredicates() {
79         final Object[] values = new Object[childrenQNamesToPaths.size()];
80         int offset = 0;
81         for (Entry<QName, PathArgument> entry : childrenQNamesToPaths.entrySet()) {
82             values[offset++] = nonnullKeyValue(entry.getKey(), getChild(entry.getValue())).body();
83         }
84         return ImmutableMapTemplate.ordered(childrenQNamesToPaths.keySet()).instantiateWithValues(values);
85     }
86
87     private Map<QName, Object> keyDefToPredicates(final Collection<QName> keys) {
88         final Object[] values = new Object[keys.size()];
89         int offset = 0;
90         for (QName key : keys) {
91             values[offset++] = nonnullKeyValue(key, getChild(childrenQNamesToPaths.get(key))).body();
92         }
93         return ImmutableMapTemplate.ordered(keys).instantiateWithValues(values);
94     }
95
96     private DataContainerChild nonnullKeyValue(final QName key, final @Nullable DataContainerChild value) {
97         if (value != null) {
98             return value;
99         }
100         throw new IllegalListKeyException("Key value not present for key: %s, in: %s values %s", key, schema.getQName(),
101             buildValue());
102     }
103 }