BUG-2682: switch sal-binding-dom-it to sal test models
[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 /**
42  * @deprecated This class provides compatibility between {@link CompositeNode} and {@link NormalizedNode}.
43  *             Users of this class should use {@link NormalizedNode}s directly.
44  */
45 @Deprecated
46 public class DataNormalizer {
47
48     private final DataNormalizationOperation<?> operation;
49
50     public DataNormalizer(final SchemaContext ctx) {
51         operation = DataNormalizationOperation.from(ctx);
52     }
53
54     public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) {
55         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
56
57         DataNormalizationOperation<?> currentOp = operation;
58         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
59
60         try {
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());
70                 }
71                 normalizedArgs.add(legacyArg);
72             }
73         } catch (DataNormalizationException e) {
74             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
75         }
76
77         return YangInstanceIdentifier.create(normalizedArgs.build());
78     }
79
80     public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException {
81         DataNormalizationOperation<?> currentOp = operation;
82         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
83
84         while (arguments.hasNext()) {
85             currentOp = currentOp.getChild(arguments.next());
86         }
87         return currentOp;
88     }
89
90     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
91             final Map.Entry<YangInstanceIdentifier, CompositeNode> legacy) {
92         return toNormalized(legacy.getKey(), legacy.getValue());
93     }
94
95     public Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final YangInstanceIdentifier legacyPath,
96             final CompositeNode legacyData) {
97
98         YangInstanceIdentifier normalizedPath = toNormalized(legacyPath);
99
100         DataNormalizationOperation<?> currentOp = operation;
101         for (PathArgument arg : normalizedPath.getPathArguments()) {
102             try {
103                 currentOp = currentOp.getChild(arg);
104             } catch (DataNormalizationException e) {
105                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
106                         normalizedPath), e);
107             }
108         }
109
110         // Write Augmentation data resolution
111         if (legacyData.getValue().size() == 1) {
112             final DataNormalizationOperation<?> potentialOp;
113
114             try {
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);
119             }
120
121             if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
122                 currentOp = potentialOp;
123                 normalizedPath = normalizedPath.node(potentialOp.getIdentifier());
124             }
125         }
126
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));
131     }
132
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);
140             }
141         }
142         return YangInstanceIdentifier.create(legacyArgs.build());
143     }
144
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;
153         }
154         return null;
155     }
156
157     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
158         if (node instanceof MixinNode) {
159             /**
160              * Direct reading of MixinNodes is not supported, since it is not
161              * possible in legacy APIs create pointer to Mixin Nodes.
162              *
163              */
164             return null;
165         }
166
167         if (node instanceof DataContainerNode<?>) {
168             return toLegacyFromDataContainer((DataContainerNode<?>) node);
169         } else if (node instanceof AnyXmlNode) {
170             return ((AnyXmlNode) node).getValue();
171         }
172         return toLegacySimple(node);
173
174     }
175
176     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
177         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
178     }
179
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));
189             } else {
190                 addToBuilder(builder, toLegacy(child));
191             }
192         }
193         return builder.toInstance();
194     }
195
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));
200         }
201         return FluentIterable.from(ret).filter(Predicates.notNull());
202     }
203
204     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
205         if (legacy != null) {
206             builder.add(legacy);
207         }
208     }
209
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));
217             } else {
218                 ret.add(toLegacy(child));
219             }
220         }
221         return FluentIterable.from(ret).filter(Predicates.notNull());
222     }
223
224     public DataNormalizationOperation<?> getRootOperation() {
225         return operation;
226     }
227
228 }