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 java.util.Collection;
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.model.api.ListSchemaNode;
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Maps;
24 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder{
26 private final ListSchemaNode schema;
27 private final DataNodeContainerValidator validator;
29 protected ImmutableMapEntryNodeSchemaAwareBuilder(ListSchemaNode schema) {
31 this.validator = new DataNodeContainerValidator(schema);
35 public ImmutableMapEntryNodeBuilder withNodeIdentifier(InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
36 throw new UnsupportedOperationException("Node identifier created from schema");
40 public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(DataContainerChild<?, ?> child) {
41 validator.validateChild(child.getIdentifier());
42 return super.withChild(child);
46 public MapEntryNode build() {
47 super.withNodeIdentifier(constructNodeIdentifier());
52 * Build map entry node identifier from schema, and provided children
54 private InstanceIdentifier.NodeIdentifierWithPredicates constructNodeIdentifier() {
55 Collection<QName> keys = schema.getKeyDefinition();
57 // If no keys defined, add all child elements as key
58 // FIXME should be all PRESENT child nodes, not all from schema
60 keys = childrenQNamesToPaths.keySet();
63 Map<QName, Object> keysToValues = Maps.newHashMap();
64 for (QName key : keys) {
65 // TODO two maps ? find better solution
66 DataContainerChild<?, ?> valueForKey = value.get(childrenQNamesToPaths.get(key));
67 Preconditions.checkState(valueForKey != null, "Key value: %s cannot be empty for: %s", key, schema.getQName());
68 keysToValues.put(key, valueForKey.getValue());
71 return new InstanceIdentifier.NodeIdentifierWithPredicates(schema.getQName(), keysToValues);
74 public static ImmutableMapEntryNodeSchemaAwareBuilder create(ListSchemaNode schema) {
75 return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);