Migrate users of AnyXml node to DOMSource
[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 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;
33
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;
39
40 /**
41  * @deprecated This class provides compatibility between {@link CompositeNode} and {@link NormalizedNode}.
42  *             Users of this class should use {@link NormalizedNode}s directly.
43  */
44 @Deprecated
45 public class DataNormalizer {
46
47     private final DataNormalizationOperation<?> operation;
48
49     public DataNormalizer(final SchemaContext ctx) {
50         operation = DataNormalizationOperation.from(ctx);
51     }
52
53     public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
54         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
55
56         DataNormalizationOperation<?> currentOp = operation;
57         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
58
59         try {
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());
69                 }
70                 normalizedArgs.add(legacyArg);
71             }
72         } catch (DataNormalizationException e) {
73             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
74         }
75
76         return YangInstanceIdentifier.create(normalizedArgs.build());
77     }
78
79     public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
80         DataNormalizationOperation<?> currentOp = operation;
81         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
82
83         while (arguments.hasNext()) {
84             currentOp = currentOp.getChild(arguments.next());
85         }
86         return currentOp;
87     }
88
89     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
90             final Map.Entry<YangInstanceIdentifier, CompositeNode> legacy) {
91         return toNormalized(legacy.getKey(), legacy.getValue());
92     }
93
94     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacyPath,
95             final CompositeNode legacyData) {
96
97         YangInstanceIdentifier normalizedPath = toNormalized(legacyPath);
98
99         DataNormalizationOperation<?> currentOp = operation;
100         for (PathArgument arg : normalizedPath.getPathArguments()) {
101             try {
102                 currentOp = currentOp.getChild(arg);
103             } catch (DataNormalizationException e) {
104                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
105                         normalizedPath), e);
106             }
107         }
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));
112     }
113
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);
121             }
122         }
123         return YangInstanceIdentifier.create(legacyArgs.build());
124     }
125
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;
134         }
135         return null;
136     }
137
138     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
139         if (node instanceof MixinNode) {
140             /**
141              * Direct reading of MixinNodes is not supported, since it is not
142              * possible in legacy APIs create pointer to Mixin Nodes.
143              *
144              */
145             return null;
146         }
147
148         if (node instanceof DataContainerNode<?>) {
149             return toLegacyFromDataContainer((DataContainerNode<?>) node);
150         } else if (node instanceof AnyXmlNode) {
151             return null;
152         }
153         return toLegacySimple(node);
154
155     }
156
157     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
158         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
159     }
160
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));
170             } else {
171                 addToBuilder(builder, toLegacy(child));
172             }
173         }
174         return builder.toInstance();
175     }
176
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));
181         }
182         return FluentIterable.from(ret).filter(Predicates.notNull());
183     }
184
185     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
186         if (legacy != null) {
187             builder.add(legacy);
188         }
189     }
190
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));
198             } else {
199                 ret.add(toLegacy(child));
200             }
201         }
202         return FluentIterable.from(ret).filter(Predicates.notNull());
203     }
204
205     public DataNormalizationOperation<?> getRootOperation() {
206         return operation;
207     }
208
209 }