bf4018617ca8855c39960d6a31875167cf79b3da
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToSimpleNodes.java
1 /*
2  * Copyright (c) 2015 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;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.Iterator;
13 import org.opendaylight.yangtools.yang.common.Empty;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23
24 /**
25 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
26 */
27 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
28
29     InstanceIdToSimpleNodes(final T identifier) {
30         super(identifier);
31     }
32
33     @Override
34     final NormalizedNode create(final PathArgument first, final Iterator<PathArgument> others) {
35         return getBuilder(first).build();
36     }
37
38     @Override
39     final InstanceIdToNodes<?> getChild(final PathArgument child) {
40         return null;
41     }
42
43     @Override
44     final boolean isMixin() {
45         return false;
46     }
47
48     abstract NormalizedNodeBuilder<? extends PathArgument, Object, ? extends NormalizedNode> getBuilder(
49         PathArgument node);
50
51     static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
52         LeafNormalization(final LeafSchemaNode potential) {
53             super(new NodeIdentifier(potential.getQName()));
54         }
55
56         @Override
57         NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
58             return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
59         }
60     }
61
62     static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
63         LeafListEntryNormalization(final LeafListSchemaNode potential) {
64             // We are fudging a value here
65             super(new NodeWithValue<>(potential.getQName(), Empty.value()));
66         }
67
68         @Override
69         NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final PathArgument node) {
70             checkArgument(node instanceof NodeWithValue);
71             return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue<?>) node)
72                     .withValue(((NodeWithValue<?>) node).getValue());
73         }
74     }
75 }