2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.common.impl.util.compat;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.util.AbstractMap;
13 import java.util.ArrayList;
14 import java.util.Iterator;
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;
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;
42 * @deprecated This class provides compatibility between {@link CompositeNode} and {@link NormalizedNode}.
43 * Users of this class should use {@link NormalizedNode}s directly.
46 public class DataNormalizer {
48 private final DataNormalizationOperation<?> operation;
50 public DataNormalizer(final SchemaContext ctx) {
51 operation = DataNormalizationOperation.from(ctx);
54 public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
55 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
57 DataNormalizationOperation<?> currentOp = operation;
58 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
61 while (arguments.hasNext()) {
62 PathArgument legacyArg = arguments.next();
63 currentOp = currentOp.getChild(legacyArg);
64 checkArgument(currentOp != null,
65 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
66 legacy, normalizedArgs.build());
67 while (currentOp.isMixin()) {
68 normalizedArgs.add(currentOp.getIdentifier());
69 currentOp = currentOp.getChild(legacyArg.getNodeType());
71 normalizedArgs.add(legacyArg);
73 } catch (DataNormalizationException e) {
74 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
77 return YangInstanceIdentifier.create(normalizedArgs.build());
80 public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
81 DataNormalizationOperation<?> currentOp = operation;
82 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
84 while (arguments.hasNext()) {
85 currentOp = currentOp.getChild(arguments.next());
90 public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
91 final Map.Entry<YangInstanceIdentifier, CompositeNode> legacy) {
92 return toNormalized(legacy.getKey(), legacy.getValue());
95 public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacyPath,
96 final CompositeNode legacyData) {
98 YangInstanceIdentifier normalizedPath = toNormalized(legacyPath);
100 DataNormalizationOperation<?> currentOp = operation;
101 for (PathArgument arg : normalizedPath.getPathArguments()) {
103 currentOp = currentOp.getChild(arg);
104 } catch (DataNormalizationException e) {
105 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
110 // Write Augmentation data resolution
111 if (legacyData.getValue().size() == 1) {
112 final DataNormalizationOperation<?> potentialOp;
115 final QName childType = legacyData.getValue().get(0).getNodeType();
116 potentialOp = currentOp.getChild(childType);
117 } catch (DataNormalizationException e) {
118 throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
121 if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
122 currentOp = potentialOp;
123 normalizedPath = normalizedPath.node(potentialOp.getIdentifier());
127 Preconditions.checkArgument(currentOp != null,
128 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
129 return new AbstractMap.SimpleEntry<YangInstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
130 currentOp.normalize(legacyData));
133 public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
134 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
135 DataNormalizationOperation<?> currentOp = operation;
136 for (PathArgument normalizedArg : normalized.getPathArguments()) {
137 currentOp = currentOp.getChild(normalizedArg);
138 if (!currentOp.isMixin()) {
139 legacyArgs.add(normalizedArg);
142 return YangInstanceIdentifier.create(legacyArgs.build());
145 public CompositeNode toLegacy(final YangInstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
146 // Preconditions.checkArgument(normalizedData instanceof
147 // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
148 if (normalizedData instanceof DataContainerNode<?>) {
149 return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
150 } else if (normalizedData instanceof AnyXmlNode) {
151 Node<?> value = ((AnyXmlNode) normalizedData).getValue();
152 return value instanceof CompositeNode ? (CompositeNode) value : null;
157 public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
158 if (node instanceof MixinNode) {
160 * Direct reading of MixinNodes is not supported, since it is not
161 * possible in legacy APIs create pointer to Mixin Nodes.
167 if (node instanceof DataContainerNode<?>) {
168 return toLegacyFromDataContainer((DataContainerNode<?>) node);
169 } else if (node instanceof AnyXmlNode) {
170 return ((AnyXmlNode) node).getValue();
172 return toLegacySimple(node);
176 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
177 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
180 @SuppressWarnings({ "unchecked", "rawtypes" })
181 private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
182 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
183 builder.setQName(node.getNodeType());
184 for (NormalizedNode<?, ?> child : node.getValue()) {
185 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
186 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
187 } else if (child instanceof UnkeyedListNode) {
188 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
190 addToBuilder(builder, toLegacy(child));
193 return builder.toInstance();
196 private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
197 ArrayList<Node<?>> ret = new ArrayList<>();
198 for (NormalizedNode<?, ?> child : mixin.getValue()) {
199 ret.add(toLegacy(child));
201 return FluentIterable.from(ret).filter(Predicates.notNull());
204 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
205 if (legacy != null) {
210 @SuppressWarnings({ "rawtypes", "unchecked" })
211 private static Iterable<Node<?>> toLegacyNodesFromMixin(
212 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
213 ArrayList<Node<?>> ret = new ArrayList<>();
214 for (NormalizedNode<?, ?> child : mixin.getValue()) {
215 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
216 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
218 ret.add(toLegacy(child));
221 return FluentIterable.from(ret).filter(Predicates.notNull());
224 public DataNormalizationOperation<?> getRootOperation() {