Cleanup use of Guava library
[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 import static com.google.common.base.Preconditions.checkNotNull;
12
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;
27
28 /**
29 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
30 */
31 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
32
33     protected InstanceIdToSimpleNodes(final T identifier) {
34         super(identifier);
35     }
36
37     @Override
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);
44
45         if (deepestChild.isPresent()) {
46             builder.withValue(deepestChild.get().getValue());
47         }
48
49         addModifyOpIfPresent(operation, builder);
50         return builder.build();
51     }
52
53     protected abstract NormalizedNodeAttrBuilder<? extends PathArgument, Object,
54             ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
55
56     @Override
57     public InstanceIdToNodes<?> getChild(final PathArgument child) {
58         return null;
59     }
60
61     static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
62         LeafNormalization(final LeafSchemaNode potential) {
63             super(new NodeIdentifier(potential.getQName()));
64         }
65
66         @Override
67         protected NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(
68                 final PathArgument node) {
69             return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
70         }
71
72         @Override
73         boolean isMixin() {
74             return false;
75         }
76     }
77
78     static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
79         LeafListEntryNormalization(final LeafListSchemaNode potential) {
80             super(new NodeWithValue<>(potential.getQName(), null));
81         }
82
83         @Override
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());
89         }
90
91         @Override
92         boolean isMixin() {
93             return false;
94         }
95     }
96 }