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.common.Empty;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
28 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
30 InstanceIdToSimpleNodes(final T identifier) {
35 final NormalizedNode create(final PathArgument first, final Iterator<PathArgument> others,
36 final Optional<NormalizedNode> deepestChild) {
37 final NormalizedNodeBuilder<? extends PathArgument, Object, ? extends NormalizedNode> builder =
40 if (deepestChild.isPresent()) {
41 builder.withValue(deepestChild.orElseThrow().body());
44 return builder.build();
48 final InstanceIdToNodes<?> getChild(final PathArgument child) {
53 final boolean isMixin() {
57 abstract NormalizedNodeBuilder<? extends PathArgument, Object, ? extends NormalizedNode> getBuilder(
60 static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
61 LeafNormalization(final LeafSchemaNode potential) {
62 super(new NodeIdentifier(potential.getQName()));
66 NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
67 return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
71 static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
72 LeafListEntryNormalization(final LeafListSchemaNode potential) {
73 // We are fudging a value here
74 super(new NodeWithValue<>(potential.getQName(), Empty.getInstance()));
78 NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final PathArgument node) {
79 checkArgument(node instanceof NodeWithValue);
80 return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue<?>) node)
81 .withValue(((NodeWithValue<?>) node).getValue());