Immutable implementation of new yang-data-api
[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 java.util.Collection;
11 import java.util.Map;
12
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;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Maps;
23
24 public final class ImmutableMapEntryNodeSchemaAwareBuilder extends ImmutableMapEntryNodeBuilder{
25
26     private final ListSchemaNode schema;
27     private final DataNodeContainerValidator validator;
28
29     protected ImmutableMapEntryNodeSchemaAwareBuilder(ListSchemaNode schema) {
30         this.schema = schema;
31         this.validator = new DataNodeContainerValidator(schema);
32     }
33
34     @Override
35     public ImmutableMapEntryNodeBuilder withNodeIdentifier(InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
36         throw new UnsupportedOperationException("Node identifier created from schema");
37     }
38
39     @Override
40     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(DataContainerChild<?, ?> child) {
41         validator.validateChild(child.getIdentifier());
42         return super.withChild(child);
43     }
44
45     @Override
46     public MapEntryNode build() {
47         super.withNodeIdentifier(constructNodeIdentifier());
48         return super.build();
49     }
50
51     /**
52      * Build map entry node identifier from schema, and provided children
53      */
54     private InstanceIdentifier.NodeIdentifierWithPredicates constructNodeIdentifier() {
55         Collection<QName> keys = schema.getKeyDefinition();
56
57         // If no keys defined, add all child elements as key
58         // FIXME should be all PRESENT child nodes, not all from schema
59         if(keys.isEmpty()) {
60             keys = childrenQNamesToPaths.keySet();
61         }
62
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());
69         }
70
71         return new InstanceIdentifier.NodeIdentifierWithPredicates(schema.getQName(), keysToValues);
72     }
73
74     public static ImmutableMapEntryNodeSchemaAwareBuilder create(ListSchemaNode schema) {
75         return new ImmutableMapEntryNodeSchemaAwareBuilder(schema);
76     }
77
78 }