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