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.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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;
41 public class DataNormalizer {
43 private final DataNormalizationOperation<?> operation;
45 public DataNormalizer(final SchemaContext ctx) {
46 operation = DataNormalizationOperation.from(ctx);
49 public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
50 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
52 DataNormalizationOperation<?> currentOp = operation;
53 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
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());
66 normalizedArgs.add(legacyArg);
68 } catch (DataNormalizationException e) {
69 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
72 return InstanceIdentifier.create(normalizedArgs.build());
75 public DataNormalizationOperation<?> getOperation(final InstanceIdentifier legacy) throws DataNormalizationException {
76 DataNormalizationOperation<?> currentOp = operation;
77 Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
79 while (arguments.hasNext()) {
80 currentOp = currentOp.getChild(arguments.next());
85 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
86 final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
87 return toNormalized(legacy.getKey(), legacy.getValue());
90 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
91 final CompositeNode legacyData) {
93 InstanceIdentifier normalizedPath = toNormalized(legacyPath);
95 DataNormalizationOperation<?> currentOp = operation;
96 for (PathArgument arg : normalizedPath.getPathArguments()) {
98 currentOp = currentOp.getChild(arg);
99 } catch (DataNormalizationException e) {
100 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
105 // Write Augmentation data resolution
106 if (legacyData.getValue().size() == 1) {
107 final DataNormalizationOperation<?> potentialOp;
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);
116 if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
117 currentOp = potentialOp;
118 normalizedPath = normalizedPath.node(potentialOp.getIdentifier());
122 Preconditions.checkArgument(currentOp != null,
123 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
124 return new AbstractMap.SimpleEntry<InstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
125 currentOp.normalize(legacyData));
128 public InstanceIdentifier toLegacy(final InstanceIdentifier 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);
137 return InstanceIdentifier.create(legacyArgs.build());
140 public CompositeNode toLegacy(final InstanceIdentifier 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;
152 public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
153 if (node instanceof MixinNode) {
155 * Direct reading of MixinNodes is not supported, since it is not
156 * possible in legacy APIs create pointer to Mixin Nodes.
162 if (node instanceof DataContainerNode<?>) {
163 return toLegacyFromDataContainer((DataContainerNode<?>) node);
164 } else if (node instanceof AnyXmlNode) {
165 return ((AnyXmlNode) node).getValue();
167 return toLegacySimple(node);
171 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
172 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
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));
185 addToBuilder(builder, toLegacy(child));
188 return builder.toInstance();
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));
196 return FluentIterable.from(ret).filter(Predicates.notNull());
199 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
200 if (legacy != null) {
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));
213 ret.add(toLegacy(child));
216 return FluentIterable.from(ret).filter(Predicates.notNull());
219 public DataNormalizationOperation<?> getRootOperation() {