Merge branch 'master' of ../controller
[yangtools.git] / yang / 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 java.util.Optional;
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.impl.schema.builder.api.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             final Optional<NormalizedNode<?, ?>> deepestChild) {
36         final NormalizedNodeBuilder<? extends PathArgument, Object,
37                 ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(first);
38
39         if (deepestChild.isPresent()) {
40             builder.withValue(deepestChild.get().getValue());
41         }
42
43         return builder.build();
44     }
45
46     @Override
47     final InstanceIdToNodes<?> getChild(final PathArgument child) {
48         return null;
49     }
50
51     @Override
52     final boolean isMixin() {
53         return false;
54     }
55
56     abstract NormalizedNodeBuilder<? extends PathArgument, Object,
57             ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
58
59     static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
60         LeafNormalization(final LeafSchemaNode potential) {
61             super(new NodeIdentifier(potential.getQName()));
62         }
63
64         @Override
65         NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
66             return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
67         }
68     }
69
70     static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
71         LeafListEntryNormalization(final LeafListSchemaNode potential) {
72             super(new NodeWithValue<>(potential.getQName(), null));
73         }
74
75         @Override
76         NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final PathArgument node) {
77             checkArgument(node instanceof NodeWithValue);
78             return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue<?>) node)
79                     .withValue(((NodeWithValue<?>) node).getValue());
80         }
81     }
82 }