Scripted update of if statements
[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.checkNotNull;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26
27 /**
28 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
29 */
30 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
31
32     protected InstanceIdToSimpleNodes(final T identifier) {
33         super(identifier);
34     }
35
36     @Override
37     public NormalizedNode<?, ?> create(final YangInstanceIdentifier instanceId, final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<Map.Entry<QName,ModifyAction>> operation) {
38         checkNotNull(instanceId);
39         final PathArgument pathArgument = instanceId.getPathArguments().get(0);
40         final NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(pathArgument);
41
42         if (deepestChild.isPresent()) {
43             builder.withValue(deepestChild.get().getValue());
44         }
45
46         addModifyOpIfPresent(operation, builder);
47         return builder.build();
48     }
49
50     protected abstract NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
51
52     @Override
53     public InstanceIdToNodes<?> getChild(final PathArgument child) {
54         return null;
55     }
56
57     static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
58
59         protected LeafNormalization(final LeafSchemaNode potential) {
60             super(new NodeIdentifier(potential.getQName()));
61         }
62
63         @Override
64         protected NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
65             return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
66         }
67
68         @Override
69         boolean isMixin() {
70             return false;
71         }
72     }
73
74     static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
75
76         public LeafListEntryNormalization(final LeafListSchemaNode potential) {
77             super(new YangInstanceIdentifier.NodeWithValue<>(potential.getQName(), null));
78         }
79
80         @Override
81         protected NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final YangInstanceIdentifier.PathArgument node) {
82             Preconditions.checkArgument(node instanceof YangInstanceIdentifier.NodeWithValue);
83             return Builders.leafSetEntryBuilder().withNodeIdentifier((YangInstanceIdentifier.NodeWithValue<?>) node).withValue(((YangInstanceIdentifier.NodeWithValue<?>) node).getValue());
84         }
85
86         @Override
87         boolean isMixin() {
88             return false;
89         }
90     }
91 }