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 com.google.common.base.Preconditions;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.FluentIterable;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.AbstractMap;
18 import java.util.ArrayList;
19 import java.util.Iterator;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.Node;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
33 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
34 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
35 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
36 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class DataNormalizer {
41 private final DataNormalizationOperation<?> operation;
43 public DataNormalizer(final SchemaContext ctx) {
44 operation = DataNormalizationOperation.from(ctx);
47 public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
48 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
50 DataNormalizationOperation<?> currentOp = operation;
51 Iterator<PathArgument> arguments = legacy.getPath().iterator();
54 while (arguments.hasNext()) {
55 PathArgument legacyArg = arguments.next();
56 currentOp = currentOp.getChild(legacyArg);
57 checkArgument(currentOp != null,
58 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
59 legacy, normalizedArgs.build());
60 while (currentOp.isMixin()) {
61 normalizedArgs.add(currentOp.getIdentifier());
62 currentOp = currentOp.getChild(legacyArg.getNodeType());
64 normalizedArgs.add(legacyArg);
66 } catch (DataNormalizationException e) {
67 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
70 return new InstanceIdentifier(normalizedArgs.build());
73 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
74 final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
75 return toNormalized(legacy.getKey(), legacy.getValue());
78 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
79 final CompositeNode legacyData) {
81 InstanceIdentifier normalizedPath = toNormalized(legacyPath);
83 DataNormalizationOperation<?> currentOp = operation;
84 for (PathArgument arg : normalizedPath.getPath()) {
86 currentOp = currentOp.getChild(arg);
87 } catch (DataNormalizationException e) {
88 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
93 // Write Augmentation data resolution
94 if (legacyData.getValue().size() == 1) {
95 final DataNormalizationOperation<?> potentialOp;
98 final QName childType = legacyData.getValue().get(0).getNodeType();
99 potentialOp = currentOp.getChild(childType);
100 } catch (DataNormalizationException e) {
101 throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
104 if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
105 currentOp = potentialOp;
106 ArrayList<PathArgument> reworkedArgs = new ArrayList<>(normalizedPath.getPath());
107 reworkedArgs.add(potentialOp.getIdentifier());
108 normalizedPath = new InstanceIdentifier(reworkedArgs);
112 Preconditions.checkArgument(currentOp != null,
113 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
114 return new AbstractMap.SimpleEntry<InstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
115 currentOp.normalize(legacyData));
118 public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) throws DataNormalizationException {
119 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
120 PathArgument previous = null;
121 DataNormalizationOperation<?> currentOp = operation;
122 for (PathArgument normalizedArg : normalized.getPath()) {
123 currentOp = currentOp.getChild(normalizedArg);
124 if(!currentOp.isMixin()) {
125 legacyArgs.add(normalizedArg);
128 return new InstanceIdentifier(legacyArgs.build());
131 public CompositeNode toLegacy(final InstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
132 // Preconditions.checkArgument(normalizedData instanceof
133 // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
134 if (normalizedData instanceof DataContainerNode<?>) {
135 return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
136 } else if (normalizedData instanceof AnyXmlNode) {
137 Node<?> value = ((AnyXmlNode) normalizedData).getValue();
138 return value instanceof CompositeNode ? (CompositeNode)value : null;
143 public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
144 if (node instanceof MixinNode) {
146 * Direct reading of MixinNodes is not supported, since it is not
147 * possible in legacy APIs create pointer to Mixin Nodes.
153 if (node instanceof DataContainerNode<?>) {
154 return toLegacyFromDataContainer((DataContainerNode<?>) node);
155 } else if (node instanceof AnyXmlNode) {
156 return ((AnyXmlNode) node).getValue();
158 return toLegacySimple(node);
162 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
163 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
166 @SuppressWarnings({ "unchecked", "rawtypes" })
167 private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
168 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
169 builder.setQName(node.getNodeType());
170 for (NormalizedNode<?, ?> child : node.getValue()) {
171 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
172 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
173 } else if( child instanceof UnkeyedListNode) {
174 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
176 addToBuilder(builder, toLegacy(child));
179 return builder.toInstance();
182 private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
183 ArrayList<Node<?>> ret = new ArrayList<>();
184 for (NormalizedNode<?, ?> child : mixin.getValue()) {
185 ret.add(toLegacy(child));
187 return FluentIterable.from(ret).filter(Predicates.notNull());
190 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
191 if (legacy != null) {
196 @SuppressWarnings({ "rawtypes", "unchecked" })
197 private static Iterable<Node<?>> toLegacyNodesFromMixin(
198 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
199 ArrayList<Node<?>> ret = new ArrayList<>();
200 for (NormalizedNode<?, ?> child : mixin.getValue()) {
201 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
202 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
204 ret.add(toLegacy(child));
207 return FluentIterable.from(ret).filter(Predicates.notNull());
210 public DataNormalizationOperation<?> getRootOperation() {