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