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 javax.xml.transform.dom.DOMSource;
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.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.Node;
22 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
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.NormalizedNodeContainer;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
29 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
30 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
31 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import com.google.common.base.Preconditions;
35 import com.google.common.base.Predicates;
36 import com.google.common.collect.FluentIterable;
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.Iterables;
41 * @deprecated This class provides compatibility between {@link CompositeNode} and {@link NormalizedNode}.
42 * Users of this class should use {@link NormalizedNode}s directly.
45 public class DataNormalizer {
47 private final DataNormalizationOperation<?> operation;
49 public DataNormalizer(final SchemaContext ctx) {
50 operation = DataNormalizationOperation.from(ctx);
53 public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
54 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
56 DataNormalizationOperation<?> currentOp = operation;
57 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
60 while (arguments.hasNext()) {
61 PathArgument legacyArg = arguments.next();
62 currentOp = currentOp.getChild(legacyArg);
63 checkArgument(currentOp != null,
64 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
65 legacy, normalizedArgs.build());
66 while (currentOp.isMixin()) {
67 normalizedArgs.add(currentOp.getIdentifier());
68 currentOp = currentOp.getChild(legacyArg.getNodeType());
70 normalizedArgs.add(legacyArg);
72 } catch (DataNormalizationException e) {
73 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
76 return YangInstanceIdentifier.create(normalizedArgs.build());
79 public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
80 DataNormalizationOperation<?> currentOp = operation;
81 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
83 while (arguments.hasNext()) {
84 currentOp = currentOp.getChild(arguments.next());
89 public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
90 final Map.Entry<YangInstanceIdentifier, CompositeNode> legacy) {
91 return toNormalized(legacy.getKey(), legacy.getValue());
94 public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacyPath,
95 final CompositeNode legacyData) {
97 YangInstanceIdentifier normalizedPath = toNormalized(legacyPath);
99 DataNormalizationOperation<?> currentOp = operation;
100 for (PathArgument arg : normalizedPath.getPathArguments()) {
102 currentOp = currentOp.getChild(arg);
103 } catch (DataNormalizationException e) {
104 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
108 Preconditions.checkArgument(currentOp != null,
109 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
110 return new AbstractMap.SimpleEntry<YangInstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
111 currentOp.normalize(legacyData));
114 public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException {
115 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
116 DataNormalizationOperation<?> currentOp = operation;
117 for (PathArgument normalizedArg : normalized.getPathArguments()) {
118 currentOp = currentOp.getChild(normalizedArg);
119 if (!currentOp.isMixin()) {
120 legacyArgs.add(normalizedArg);
123 return YangInstanceIdentifier.create(legacyArgs.build());
126 public CompositeNode toLegacy(final YangInstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
127 // Preconditions.checkArgument(normalizedData instanceof
128 // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
129 if (normalizedData instanceof DataContainerNode<?>) {
130 return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
131 } else if (normalizedData instanceof AnyXmlNode) {
132 DOMSource value = ((AnyXmlNode) normalizedData).getValue();
133 return value instanceof CompositeNode ? (CompositeNode) value : null;
138 public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
139 if (node instanceof MixinNode) {
141 * Direct reading of MixinNodes is not supported, since it is not
142 * possible in legacy APIs create pointer to Mixin Nodes.
148 if (node instanceof DataContainerNode<?>) {
149 return toLegacyFromDataContainer((DataContainerNode<?>) node);
150 } else if (node instanceof AnyXmlNode) {
153 return toLegacySimple(node);
157 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
158 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
161 @SuppressWarnings({ "unchecked", "rawtypes" })
162 private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
163 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
164 builder.setQName(node.getNodeType());
165 for (NormalizedNode<?, ?> child : node.getValue()) {
166 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
167 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
168 } else if (child instanceof UnkeyedListNode) {
169 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
171 addToBuilder(builder, toLegacy(child));
174 return builder.toInstance();
177 private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
178 ArrayList<Node<?>> ret = new ArrayList<>();
179 for (NormalizedNode<?, ?> child : mixin.getValue()) {
180 ret.add(toLegacy(child));
182 return FluentIterable.from(ret).filter(Predicates.notNull());
185 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
186 if (legacy != null) {
191 @SuppressWarnings({ "rawtypes", "unchecked" })
192 private static Iterable<Node<?>> toLegacyNodesFromMixin(
193 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
194 ArrayList<Node<?>> ret = new ArrayList<>();
195 for (NormalizedNode<?, ?> child : mixin.getValue()) {
196 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
197 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
199 ret.add(toLegacy(child));
202 return FluentIterable.from(ret).filter(Predicates.notNull());
205 public DataNormalizationOperation<?> getRootOperation() {