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.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.Node;
25 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
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.Predicate;
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.getPath().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 new InstanceIdentifier(normalizedArgs.build());
75 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
76 final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
77 return toNormalized(legacy.getKey(), legacy.getValue());
80 public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
81 final CompositeNode legacyData) {
83 InstanceIdentifier normalizedPath = toNormalized(legacyPath);
85 DataNormalizationOperation<?> currentOp = operation;
86 for (PathArgument arg : normalizedPath.getPath()) {
88 currentOp = currentOp.getChild(arg);
89 } catch (DataNormalizationException e) {
90 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
95 // Write Augmentation data resolution
96 if (legacyData.getChildren().size() == 1) {
97 final DataNormalizationOperation<?> potentialOp;
100 final QName childType = legacyData.getChildren().get(0).getNodeType();
101 potentialOp = currentOp.getChild(childType);
102 } catch (DataNormalizationException e) {
103 throw new IllegalArgumentException(String.format("Failed to get child operation for %s", legacyData), e);
106 if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
107 currentOp = potentialOp;
108 ArrayList<PathArgument> reworkedArgs = new ArrayList<>(normalizedPath.getPath());
109 reworkedArgs.add(potentialOp.getIdentifier());
110 normalizedPath = new InstanceIdentifier(reworkedArgs);
114 Preconditions.checkArgument(currentOp != null,
115 "Instance Identifier %s does not reference correct schema Node.", normalizedPath);
116 return new AbstractMap.SimpleEntry<InstanceIdentifier, NormalizedNode<?, ?>>(normalizedPath,
117 currentOp.normalize(legacyData));
120 public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) {
121 ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
122 PathArgument previous = null;
123 for (PathArgument normalizedArg : normalized.getPath()) {
124 if (normalizedArg instanceof NodeIdentifier) {
125 if (previous != null) {
126 legacyArgs.add(previous);
128 previous = normalizedArg;
129 } else if (normalizedArg instanceof NodeIdentifierWithPredicates) {
130 // We skip previous node, which was mixin.
131 previous = normalizedArg;
132 } else if (normalizedArg instanceof AugmentationIdentifier) {
133 // We ignore argument
135 // FIXME : Add option for reading choice
137 if (previous != null) {
138 legacyArgs.add(previous);
140 return new InstanceIdentifier(legacyArgs.build());
143 public CompositeNode toLegacy(final InstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
144 // Preconditions.checkArgument(normalizedData instanceof
145 // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
146 if (normalizedData instanceof DataContainerNode<?>) {
147 return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
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);
165 return toLegacySimple(node);
169 private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
170 return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
173 @SuppressWarnings({ "unchecked", "rawtypes" })
174 private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
175 CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
176 builder.setQName(node.getNodeType());
177 for (NormalizedNode<?, ?> child : node.getValue()) {
178 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
179 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
181 addToBuilder(builder, toLegacy(child));
184 return builder.toInstance();
187 private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
188 if (legacy != null) {
193 @SuppressWarnings({ "rawtypes", "unchecked" })
194 private static Iterable<Node<?>> toLegacyNodesFromMixin(
195 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
196 ArrayList<Node<?>> ret = new ArrayList<>();
197 for (NormalizedNode<?, ?> child : mixin.getValue()) {
198 if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
199 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
201 ret.add(toLegacy(child));
204 return FluentIterable.from(ret).filter(new Predicate<Node<?>>() {
207 public boolean apply(final Node<?> input) {
208 return input != null;
213 public DataNormalizationOperation<?> getRootOperation() {