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.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;
40 public class DataNormalizer {
42 private final DataNormalizationOperation<?> operation;
44 public DataNormalizer(final SchemaContext ctx) {
45 operation = DataNormalizationOperation.from(ctx);
48 public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
49 ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
51 DataNormalizationOperation<?> currentOp = operation;
52 Iterator<PathArgument> arguments = legacy.getPath().iterator();
55 while (arguments.hasNext()) {
56 PathArgument legacyArg = arguments.next();
57 currentOp = currentOp.getChild(legacyArg);
58 checkArgument(currentOp != null,
59 "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",
60 legacy, normalizedArgs.build());
61 while (currentOp.isMixin()) {
62 normalizedArgs.add(currentOp.getIdentifier());
63 currentOp = currentOp.getChild(legacyArg.getNodeType());
65 normalizedArgs.add(legacyArg);
67 } catch (DataNormalizationException e) {
68 throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
71 return new InstanceIdentifier(normalizedArgs.build());
74 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
75 final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
76 return toNormalized(legacy.getKey(), legacy.getValue());
79 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
80 final CompositeNode legacyData) {
82 InstanceIdentifier normalizedPath = toNormalized(legacyPath);
84 DataNormalizationOperation<?> currentOp = operation;
85 for (PathArgument arg : normalizedPath.getPath()) {
87 currentOp = currentOp.getChild(arg);
88 } catch (DataNormalizationException e) {
89 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
94 // Write Augmentation data resolution
95 if (legacyData.getChildren().size() == 1) {
96 final DataNormalizationOperation<?> potentialOp;
99 final QName childType = legacyData.getChildren().get(0).getNodeType();
100 potentialOp = currentOp.getChild(childType);
101 } catch (DataNormalizationException e) {
102 throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
105 if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
106 currentOp = potentialOp;
107 ArrayList<PathArgument> reworkedArgs = new ArrayList<>(normalizedPath.getPath());
108 reworkedArgs.add(potentialOp.getIdentifier());
109 normalizedPath = new InstanceIdentifier(reworkedArgs);
113 Preconditions.checkArgument(currentOp != null,
114 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
115 return new AbstractMap.SimpleEntry<InstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
116 currentOp.normalize(legacyData));
119 public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) throws DataNormalizationException {
120 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
121 PathArgument previous = null;
122 DataNormalizationOperation<?> currentOp = operation;
123 for (PathArgument normalizedArg : normalized.getPath()) {
124 currentOp = currentOp.getChild(normalizedArg);
125 if(!currentOp.isMixin()) {
126 legacyArgs.add(normalizedArg);
129 return new InstanceIdentifier(legacyArgs.build());
132 public CompositeNode toLegacy(final InstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
133 // Preconditions.checkArgument(normalizedData instanceof
134 // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
135 if (normalizedData instanceof DataContainerNode<?>) {
136 return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
141 public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
142 if (node instanceof MixinNode) {
144 * Direct reading of MixinNodes is not supported, since it is not
145 * possible in legacy APIs create pointer to Mixin Nodes.
151 if (node instanceof DataContainerNode<?>) {
152 return toLegacyFromDataContainer((DataContainerNode<?>) node);
154 return toLegacySimple(node);
158 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
159 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
162 @SuppressWarnings({ "unchecked", "rawtypes" })
163 private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
164 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
165 builder.setQName(node.getNodeType());
166 for (NormalizedNode<?, ?> child : node.getValue()) {
167 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
168 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
169 } else if( child instanceof UnkeyedListNode) {
170 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
172 addToBuilder(builder, toLegacy(child));
175 return builder.toInstance();
178 private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
179 ArrayList<Node<?>> ret = new ArrayList<>();
180 for (NormalizedNode<?, ?> child : mixin.getValue()) {
181 ret.add(toLegacy(child));
183 return FluentIterable.from(ret).filter(Predicates.notNull());
186 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
187 if (legacy != null) {
192 @SuppressWarnings({ "rawtypes", "unchecked" })
193 private static Iterable<Node<?>> toLegacyNodesFromMixin(
194 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
195 ArrayList<Node<?>> ret = new ArrayList<>();
196 for (NormalizedNode<?, ?> child : mixin.getValue()) {
197 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
198 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
200 ret.add(toLegacy(child));
203 return FluentIterable.from(ret).filter(Predicates.notNull());
206 public DataNormalizationOperation<?> getRootOperation() {