2 * Copyright (c) 2015 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;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
25 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
27 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
29 InstanceIdToSimpleNodes(final T identifier) {
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);
39 if (deepestChild.isPresent()) {
40 builder.withValue(deepestChild.get().getValue());
43 return builder.build();
47 final InstanceIdToNodes<?> getChild(final PathArgument child) {
52 final boolean isMixin() {
56 abstract NormalizedNodeBuilder<? extends PathArgument, Object,
57 ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
59 static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
60 LeafNormalization(final LeafSchemaNode potential) {
61 super(new NodeIdentifier(potential.getQName()));
65 NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
66 return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
70 static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
71 LeafListEntryNormalization(final LeafListSchemaNode potential) {
72 super(new NodeWithValue<>(potential.getQName(), null));
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());