BUG-868: do not use deprecated InstanceIdentifier methods
[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 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
18 import java.util.AbstractMap;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.Node;
29 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
36 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
37 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
38 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40
41 public class DataNormalizer {
42
43     private final DataNormalizationOperation<?> operation;
44
45     public DataNormalizer(final SchemaContext ctx) {
46         operation = DataNormalizationOperation.from(ctx);
47     }
48
49     public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
50         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
51
52         DataNormalizationOperation<?> currentOp = operation;
53         Iterator<PathArgument> arguments = legacy.getPathArguments().iterator();
54
55         try {
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());
65                 }
66                 normalizedArgs.add(legacyArg);
67             }
68         } catch (DataNormalizationException e) {
69             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
70         }
71
72         return InstanceIdentifier.create(normalizedArgs.build());
73     }
74
75     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
76             final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
77         return toNormalized(legacy.getKey(), legacy.getValue());
78     }
79
80     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
81             final CompositeNode legacyData) {
82
83         InstanceIdentifier normalizedPath = toNormalized(legacyPath);
84
85         DataNormalizationOperation<?> currentOp = operation;
86         for (PathArgument arg : normalizedPath.getPathArguments()) {
87             try {
88                 currentOp = currentOp.getChild(arg);
89             } catch (DataNormalizationException e) {
90                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
91                         normalizedPath), e);
92             }
93         }
94
95         // Write Augmentation data resolution
96         if (legacyData.getValue().size() == 1) {
97             final DataNormalizationOperation<?> potentialOp;
98
99             try {
100                 final QName childType = legacyData.getValue().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);
104             }
105
106             if (potentialOp.getIdentifier() instanceof AugmentationIdentifier) {
107                 currentOp = potentialOp;
108                 normalizedPath = normalizedPath.node(potentialOp.getIdentifier());
109             }
110         }
111
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));
116     }
117
118     public InstanceIdentifier toLegacy(final InstanceIdentifier normalized) throws DataNormalizationException {
119         ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder();
120         DataNormalizationOperation<?> currentOp = operation;
121         for (PathArgument normalizedArg : normalized.getPathArguments()) {
122             currentOp = currentOp.getChild(normalizedArg);
123             if(!currentOp.isMixin()) {
124                 legacyArgs.add(normalizedArg);
125             }
126         }
127         return InstanceIdentifier.create(legacyArgs.build());
128     }
129
130     public CompositeNode toLegacy(final InstanceIdentifier normalizedPath, final NormalizedNode<?, ?> normalizedData) {
131         // Preconditions.checkArgument(normalizedData instanceof
132         // DataContainerNode<?>,"Node object %s, %s should be of type DataContainerNode",normalizedPath,normalizedData);
133         if (normalizedData instanceof DataContainerNode<?>) {
134             return toLegacyFromDataContainer((DataContainerNode<?>) normalizedData);
135         } else if (normalizedData instanceof AnyXmlNode) {
136             Node<?> value = ((AnyXmlNode) normalizedData).getValue();
137             return value instanceof CompositeNode ? (CompositeNode)value : null;
138         }
139         return null;
140     }
141
142     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
143         if (node instanceof MixinNode) {
144             /**
145              * Direct reading of MixinNodes is not supported, since it is not
146              * possible in legacy APIs create pointer to Mixin Nodes.
147              *
148              */
149             return null;
150         }
151
152         if (node instanceof DataContainerNode<?>) {
153             return toLegacyFromDataContainer((DataContainerNode<?>) node);
154         } else if (node instanceof AnyXmlNode) {
155             return ((AnyXmlNode) node).getValue();
156         }
157         return toLegacySimple(node);
158
159     }
160
161     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
162         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
163     }
164
165     @SuppressWarnings({ "unchecked", "rawtypes" })
166     private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
167         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
168         builder.setQName(node.getNodeType());
169         for (NormalizedNode<?, ?> child : node.getValue()) {
170             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
171                 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
172             } else if( child instanceof UnkeyedListNode) {
173                 builder.addAll(toLegacyNodesFromUnkeyedList((UnkeyedListNode) child));
174             } else {
175                 addToBuilder(builder, toLegacy(child));
176             }
177         }
178         return builder.toInstance();
179     }
180
181     private static Iterable<? extends Node<?>> toLegacyNodesFromUnkeyedList(final UnkeyedListNode mixin) {
182         ArrayList<Node<?>> ret = new ArrayList<>();
183         for (NormalizedNode<?, ?> child : mixin.getValue()) {
184             ret.add(toLegacy(child));
185         }
186         return FluentIterable.from(ret).filter(Predicates.notNull());
187     }
188
189     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
190         if (legacy != null) {
191             builder.add(legacy);
192         }
193     }
194
195     @SuppressWarnings({ "rawtypes", "unchecked" })
196     private static Iterable<Node<?>> toLegacyNodesFromMixin(
197             final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
198         ArrayList<Node<?>> ret = new ArrayList<>();
199         for (NormalizedNode<?, ?> child : mixin.getValue()) {
200             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
201                 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
202             } else {
203                 ret.add(toLegacy(child));
204             }
205         }
206         return FluentIterable.from(ret).filter(Predicates.notNull());
207     }
208
209     public DataNormalizationOperation<?> getRootOperation() {
210         return operation;
211     }
212
213 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.