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;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
31 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
33 protected InstanceIdToSimpleNodes(final T identifier) {
38 public NormalizedNode<?, ?> create(final YangInstanceIdentifier instanceId,
39 final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<Entry<QName, ModifyAction>> operation) {
40 checkNotNull(instanceId);
41 final PathArgument pathArgument = instanceId.getPathArguments().get(0);
42 final NormalizedNodeAttrBuilder<? extends PathArgument, Object,
43 ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(pathArgument);
45 if (deepestChild.isPresent()) {
46 builder.withValue(deepestChild.get().getValue());
49 addModifyOpIfPresent(operation, builder);
50 return builder.build();
53 protected abstract NormalizedNodeAttrBuilder<? extends PathArgument, Object,
54 ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
57 public InstanceIdToNodes<?> getChild(final PathArgument child) {
61 static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
62 LeafNormalization(final LeafSchemaNode potential) {
63 super(new NodeIdentifier(potential.getQName()));
67 protected NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(
68 final PathArgument node) {
69 return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
78 static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
79 LeafListEntryNormalization(final LeafListSchemaNode potential) {
80 super(new NodeWithValue<>(potential.getQName(), null));
84 protected NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(
85 final PathArgument node) {
86 checkArgument(node instanceof NodeWithValue);
87 return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue<?>) node)
88 .withValue(((NodeWithValue<?>) node).getValue());