Reduce the use of AttrBuilders
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / NormalizedDataPrunner.java
1 /*
2  * Copyright (c) 2014 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.netconf.sal.restconf.impl;
9
10 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
34
35 class NormalizedDataPrunner {
36
37     public DataContainerChild<?, ?> pruneDataAtDepth(final DataContainerChild<?, ?> node, final Integer depth) {
38         if (depth == null) {
39             return node;
40         }
41
42         if (node instanceof LeafNode || node instanceof LeafSetNode || node instanceof AnyXmlNode
43                 || node instanceof OrderedLeafSetNode) {
44             return node;
45         } else if (node instanceof MixinNode) {
46             return processMixinNode(node, depth);
47         } else if (node instanceof DataContainerNode) {
48             return processContainerNode(node, depth);
49         }
50         throw new IllegalStateException("Unexpected Mixin node occured why pruning data to requested depth");
51     }
52
53     private DataContainerChild<?, ?> processMixinNode(final NormalizedNode<?, ?> node, final Integer depth) {
54         if (node instanceof AugmentationNode) {
55             return processAugmentationNode(node, depth);
56         } else if (node instanceof ChoiceNode) {
57             return processChoiceNode(node, depth);
58         } else if (node instanceof OrderedMapNode) {
59             return processOrderedMapNode(node, depth);
60         } else if (node instanceof MapNode) {
61             return processMapNode(node, depth);
62         } else if (node instanceof UnkeyedListNode) {
63             return processUnkeyedListNode(node, depth);
64         }
65         throw new IllegalStateException("Unexpected Mixin node occured why pruning data to requested depth");
66     }
67
68     private DataContainerChild<?, ?> processContainerNode(final NormalizedNode<?, ?> node, final Integer depth) {
69         final ContainerNode containerNode = (ContainerNode) node;
70         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> newContainerBuilder = Builders.containerBuilder()
71                 .withNodeIdentifier(containerNode.getIdentifier());
72         if (depth > 1) {
73             processDataContainerChild((DataContainerNode<?>) node, depth, newContainerBuilder);
74         }
75         return newContainerBuilder.build();
76     }
77
78     private DataContainerChild<?, ?> processChoiceNode(final NormalizedNode<?, ?> node, final Integer depth) {
79         final ChoiceNode choiceNode = (ChoiceNode) node;
80         DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> newChoiceBuilder = Builders.choiceBuilder()
81                 .withNodeIdentifier(choiceNode.getIdentifier());
82
83         processDataContainerChild((DataContainerNode<?>) node, depth, newChoiceBuilder);
84
85         return newChoiceBuilder.build();
86     }
87
88     private DataContainerChild<?, ?> processAugmentationNode(final NormalizedNode<?, ?> node, final Integer depth) {
89         final AugmentationNode augmentationNode = (AugmentationNode) node;
90         DataContainerNodeBuilder<AugmentationIdentifier, ? extends DataContainerChild<?, ?>> newAugmentationBuilder =
91                 Builders.augmentationBuilder().withNodeIdentifier(augmentationNode.getIdentifier());
92
93         processDataContainerChild((DataContainerNode<?>) node, depth, newAugmentationBuilder);
94
95         return newAugmentationBuilder.build();
96     }
97
98     private void processDataContainerChild(
99             final DataContainerNode<?> node,
100             final Integer depth,
101             final DataContainerNodeBuilder<? extends YangInstanceIdentifier.PathArgument,
102                     ? extends DataContainerNode<?>> newBuilder) {
103
104         for (DataContainerChild<? extends PathArgument, ?> nodeValue : node.getValue()) {
105             newBuilder.withChild(pruneDataAtDepth(nodeValue, depth - 1));
106         }
107
108     }
109
110     private DataContainerChild<?, ?> processUnkeyedListNode(final NormalizedNode<?, ?> node, final Integer depth) {
111         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> newUnkeyedListBuilder = Builders
112                 .unkeyedListBuilder();
113         if (depth > 1) {
114             for (UnkeyedListEntryNode oldUnkeyedListEntry : ((UnkeyedListNode) node).getValue()) {
115                 DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> newUnkeyedListEntry = Builders
116                         .unkeyedListEntryBuilder().withNodeIdentifier(oldUnkeyedListEntry.getIdentifier());
117                 for (DataContainerChild<? extends PathArgument, ?> oldUnkeyedListEntryValue : oldUnkeyedListEntry
118                         .getValue()) {
119                     newUnkeyedListEntry.withChild(pruneDataAtDepth(oldUnkeyedListEntryValue, depth - 1));
120                 }
121                 newUnkeyedListBuilder.addChild(newUnkeyedListEntry.build());
122             }
123         }
124         return newUnkeyedListBuilder.build();
125     }
126
127     private DataContainerChild<?, ?> processOrderedMapNode(final NormalizedNode<?, ?> node, final Integer depth) {
128         CollectionNodeBuilder<MapEntryNode, OrderedMapNode> newOrderedMapNodeBuilder = Builders.orderedMapBuilder();
129         processMapEntries(node, depth, newOrderedMapNodeBuilder);
130         return newOrderedMapNodeBuilder.build();
131     }
132
133     private DataContainerChild<?, ?> processMapNode(final NormalizedNode<?, ?> node, final Integer depth) {
134         CollectionNodeBuilder<MapEntryNode, MapNode> newMapNodeBuilder = Builders.mapBuilder();
135         processMapEntries(node, depth, newMapNodeBuilder);
136         return newMapNodeBuilder.build();
137     }
138
139     private void processMapEntries(final NormalizedNode<?, ?> node, final Integer depth,
140             final CollectionNodeBuilder<MapEntryNode, ? extends MapNode> newOrderedMapNodeBuilder) {
141         if (depth > 1) {
142             for (MapEntryNode oldMapEntryNode : ((MapNode) node).getValue()) {
143                 DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> newMapEntryNodeBuilder =
144                         Builders.mapEntryBuilder().withNodeIdentifier(oldMapEntryNode.getIdentifier());
145                 for (DataContainerChild<? extends PathArgument, ?> mapEntryNodeValue : oldMapEntryNode.getValue()) {
146                     newMapEntryNodeBuilder.withChild(pruneDataAtDepth(mapEntryNodeValue, depth - 1));
147                 }
148                 newOrderedMapNodeBuilder.withChild(newMapEntryNodeBuilder.build());
149             }
150         }
151     }
152 }