2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
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;
29 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder {
31 private final ListSchemaNode schema;
32 private final DataNodeContainerValidator validator;
34 ImmutableMapEntryNodeSchemaAwareBuilder(final ListSchemaNode schema) {
35 this.schema = requireNonNull(schema);
36 this.validator = new DataNodeContainerValidator(schema);
40 public ImmutableMapEntryNodeBuilder withNodeIdentifier(final NodeIdentifierWithPredicates withNodeIdentifier) {
41 throw new UnsupportedOperationException("Node identifier created from schema");
45 public DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> withChild(
46 final DataContainerChild child) {
47 validator.validateChild(child.getIdentifier());
48 return super.withChild(child);
52 public MapEntryNode build() {
53 super.withNodeIdentifier(constructNodeIdentifier());
57 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> create(
58 final ListSchemaNode schema) {
59 return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
63 * Build map entry node identifier from schema and provided children.
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();
73 predicates = ImmutableMap.of();
75 return NodeIdentifierWithPredicates.of(schema.getQName(), predicates);
78 private Map<QName, Object> childrenToPredicates() {
79 final Object[] values = new Object[childrenQNamesToPaths.size()];
81 for (Entry<QName, PathArgument> entry : childrenQNamesToPaths.entrySet()) {
82 values[offset++] = nonnullKeyValue(entry.getKey(), getChild(entry.getValue())).body();
84 return ImmutableMapTemplate.ordered(childrenQNamesToPaths.keySet()).instantiateWithValues(values);
87 private Map<QName, Object> keyDefToPredicates(final Collection<QName> keys) {
88 final Object[] values = new Object[keys.size()];
90 for (QName key : keys) {
91 values[offset++] = nonnullKeyValue(key, getChild(childrenQNamesToPaths.get(key))).body();
93 return ImmutableMapTemplate.ordered(keys).instantiateWithValues(values);
96 private DataContainerChild nonnullKeyValue(final QName key, final @Nullable DataContainerChild value) {
100 throw new IllegalListKeyException("Key value not present for key: %s, in: %s values %s", key, schema.getQName(),