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.opendaylight.yangtools.util.ImmutableMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException.IllegalListKeyException;
25 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder {
29 private final ListSchemaNode schema;
30 private final DataNodeContainerValidator validator;
32 ImmutableMapEntryNodeSchemaAwareBuilder(final ListSchemaNode schema) {
33 this.schema = requireNonNull(schema);
34 this.validator = new DataNodeContainerValidator(schema);
38 public ImmutableMapEntryNodeBuilder withNodeIdentifier(final NodeIdentifierWithPredicates withNodeIdentifier) {
39 throw new UnsupportedOperationException("Node identifier created from schema");
43 public DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> withChild(
44 final DataContainerChild<?, ?> child) {
45 validator.validateChild(child.getIdentifier());
46 return super.withChild(child);
50 public MapEntryNode build() {
51 super.withNodeIdentifier(constructNodeIdentifier());
55 public static DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> create(
56 final ListSchemaNode schema) {
57 return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
61 * Build map entry node identifier from schema and provided children.
63 private NodeIdentifierWithPredicates constructNodeIdentifier() {
64 final Map<QName, Object> predicates;
65 final Collection<QName> keys = schema.getKeyDefinition();
66 if (!keys.isEmpty()) {
67 predicates = keyDefToPredicates(keys);
68 } else if (!childrenQNamesToPaths.isEmpty()) {
69 predicates = childrenToPredicates();
71 predicates = ImmutableMap.of();
73 return new NodeIdentifierWithPredicates(schema.getQName(), predicates);
76 private Map<QName, Object> childrenToPredicates() {
77 final Object[] values = new Object[childrenQNamesToPaths.size()];
79 for (Entry<QName, PathArgument> entry : childrenQNamesToPaths.entrySet()) {
80 values[offset++] = nonnullKeyValue(entry.getKey(), getChild(entry.getValue())).getValue();
82 return ImmutableMapTemplate.ordered(childrenQNamesToPaths.keySet()).instantiateWithValues(values);
85 private Map<QName, Object> keyDefToPredicates(final Collection<QName> keys) {
86 final Object[] values = new Object[keys.size()];
88 for (QName key : keys) {
89 values[offset++] = nonnullKeyValue(key, getChild(childrenQNamesToPaths.get(key))).getValue();
91 return ImmutableMapTemplate.ordered(keys).instantiateWithValues(values);
94 private DataContainerChild<?, ?> nonnullKeyValue(final QName key, final DataContainerChild<?, ?> value) {
98 throw new IllegalListKeyException("Key value not present for key: %s, in: %s values %s", key, schema.getQName(),