BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / util / compat / DataNormalizer.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.controller.md.sal.common.impl.util.compat;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.AbstractMap;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.Map;
16
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
29 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
30 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
31 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
32 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 import com.google.common.base.Preconditions;
36 import com.google.common.base.Predicates;
37 import com.google.common.collect.FluentIterable;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.Iterables;
40
41 public class DataNormalizer {
42
43     private final DataNormalizationOperation<?> operation;
44
45     public DataNormalizer(final SchemaContext ctx) {
46         operation = DataNormalizationOperation.from(ctx);
47     }
48
49     public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
50         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
51
52         DataNormalizationOperation<?> currentOp = operation;
53         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
54
55         try {
56             while (arguments.hasNext()) {
57                 PathArgument legacyArg = arguments.next();
58                 currentOp = currentOp.getChild(legacyArg);
59                 checkArgument(currentOp != null,
60                         "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
61                         legacy, normalizedArgs.build());
62                 while (currentOp.isMixin()) {
63                     normalizedArgs.add(currentOp.getIdentifier());
64                     currentOp = currentOp.getChild(legacyArg.getNodeType());
65                 }
66                 normalizedArgs.add(legacyArg);
67             }
68         } catch (DataNormalizationException e) {
69             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
70         }
71
72         return YangInstanceIdentifier.create(normalizedArgs.build());
73     }
74
75     public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
76         DataNormalizationOperation<?> currentOp = operation;
77         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
78
79         while (arguments.hasNext()) {
80             currentOp = currentOp.getChild(arguments.next());
81         }
82         return currentOp;
83     }
84
85     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
86             final Map.Entry<YangInstanceIdentifier, CompositeNode> legacy) {
87         return toNormalized(legacy.getKey(), legacy.getValue());
88     }
89
90     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacyPath,
91             final CompositeNode legacyData) {
92
93         YangInstanceIdentifier normalizedPath = toNormalized(legacyPath);
94
95         DataNormalizationOperation<?> currentOp = operation;
96         for (PathArgument arg : normalizedPath.getPathArguments()) {
97             try {
98                 currentOp = currentOp.getChild(arg);
99             } catch (DataNormalizationException e) {
100                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
101                         normalizedPath), e);
102             }
103         }
104
105         // Write Augmentation data resolution
106         if (legacyData.getValue().size() == 1) {
107             final DataNormalizationOperation<?> potentialOp;
108
109             try {
110                 final QName childType = legacyData.getValue().get(0).getNodeType();
111                 potentialOp = currentOp.getChild(childType);
112             } catch (DataNormalizationException e) {
113                 throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
114             }
115
116             if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
117                 currentOp = potentialOp;
118                 normalizedPath = normalizedPath.node(potentialOp.getIdentifier());
119             }
120         }
121
122         Preconditions.checkArgument(currentOp != null,
123                 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
124         return new AbstractMap.SimpleEntry<YangInstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
125                 currentOp.normalize(legacyData));
126     }
127
128     public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
129         ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
130         DataNormalizationOperation<?> currentOp = operation;
131         for (PathArgument normalizedArg : normalized.getPathArguments()) {
132             currentOp = currentOp.getChild(normalizedArg);
133             if (!currentOp.isMixin()) {
134                 legacyArgs.add(normalizedArg);
135             }
136         }
137         return YangInstanceIdentifier.create(legacyArgs.build());
138     }
139
140     public CompositeNode toLegacy(final YangInstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
141         // Preconditions.checkArgument(normalizedData instanceof
142         // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
143         if (normalizedData instanceof DataContainerNode<?>) {
144             return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
145         } else if (normalizedData instanceof AnyXmlNode) {
146             Node<?> value = ((AnyXmlNode) normalizedData).getValue();
147             return value instanceof CompositeNode ? (CompositeNode) value : null;
148         }
149         return null;
150     }
151
152     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
153         if (node instanceof MixinNode) {
154             /**
155              * Direct reading of MixinNodes is not supported, since it is not
156              * possible in legacy APIs create pointer to Mixin Nodes.
157              *
158              */
159             return null;
160         }
161
162         if (node instanceof DataContainerNode<?>) {
163             return toLegacyFromDataContainer((DataContainerNode<?>) node);
164         } else if (node instanceof AnyXmlNode) {
165             return ((AnyXmlNode) node).getValue();
166         }
167         return toLegacySimple(node);
168
169     }
170
171     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
172         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
173     }
174
175     @SuppressWarnings({ "unchecked", "rawtypes" })
176     private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
177         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
178         builder.setQName(node.getNodeType());
179         for (NormalizedNode<?, ?> child : node.getValue()) {
180             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
181                 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
182             } else if (child instanceof UnkeyedListNode) {
183                 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
184             } else {
185                 addToBuilder(builder, toLegacy(child));
186             }
187         }
188         return builder.toInstance();
189     }
190
191     private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
192         ArrayList<Node<?>> ret = new ArrayList<>();
193         for (NormalizedNode<?, ?> child : mixin.getValue()) {
194             ret.add(toLegacy(child));
195         }
196         return FluentIterable.from(ret).filter(Predicates.notNull());
197     }
198
199     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
200         if (legacy != null) {
201             builder.add(legacy);
202         }
203     }
204
205     @SuppressWarnings({ "rawtypes", "unchecked" })
206     private static Iterable<Node<?>> toLegacyNodesFromMixin(
207             final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
208         ArrayList<Node<?>> ret = new ArrayList<>();
209         for (NormalizedNode<?, ?> child : mixin.getValue()) {
210             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
211                 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
212             } else {
213                 ret.add(toLegacy(child));
214             }
215         }
216         return FluentIterable.from(ret).filter(Predicates.notNull());
217     }
218
219     public DataNormalizationOperation<?> getRootOperation() {
220         return operation;
221     }
222
223 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.