Merge "BUG-770: NumberFormatException for input string on switch OFPT_HELLO"
[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 java.util.AbstractMap;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.Map;
16
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;
33
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;
39
40 public class DataNormalizer {
41
42     private final DataNormalizationOperation<?> operation;
43
44     public DataNormalizer(final SchemaContext ctx) {
45         operation = DataNormalizationOperation.from(ctx);
46     }
47
48     public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
49         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
50
51         DataNormalizationOperation<?> currentOp = operation;
52         Iterator<PathArgument> arguments = legacy.getPath().iterator();
53
54         try {
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());
64                 }
65                 normalizedArgs.add(legacyArg);
66             }
67         } catch (DataNormalizationException e) {
68             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
69         }
70
71         return new InstanceIdentifier(normalizedArgs.build());
72     }
73
74     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
75             final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
76         return toNormalized(legacy.getKey(), legacy.getValue());
77     }
78
79     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
80             final CompositeNode legacyData) {
81
82         InstanceIdentifier normalizedPath = toNormalized(legacyPath);
83
84         DataNormalizationOperation<?> currentOp = operation;
85         for (PathArgument arg : normalizedPath.getPath()) {
86             try {
87                 currentOp = currentOp.getChild(arg);
88             } catch (DataNormalizationException e) {
89                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
90                         normalizedPath), e);
91             }
92         }
93
94         // Write Augmentation data resolution
95         if (legacyData.getValue().size() == 1) {
96             final DataNormalizationOperation<?> potentialOp;
97
98             try {
99                 final QName childType = legacyData.getValue().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);
103             }
104
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);
110             }
111         }
112
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));
117     }
118
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);
127             }
128         }
129         return new InstanceIdentifier(legacyArgs.build());
130     }
131
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);
137         }
138         return null;
139     }
140
141     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
142         if (node instanceof MixinNode) {
143             /**
144              * Direct reading of MixinNodes is not supported, since it is not
145              * possible in legacy APIs create pointer to Mixin Nodes.
146              *
147              */
148             return null;
149         }
150
151         if (node instanceof DataContainerNode<?>) {
152             return toLegacyFromDataContainer((DataContainerNode<?>) node);
153         }
154         return toLegacySimple(node);
155
156     }
157
158     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
159         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
160     }
161
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));
171             } else {
172                 addToBuilder(builder, toLegacy(child));
173             }
174         }
175         return builder.toInstance();
176     }
177
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));
182         }
183         return FluentIterable.from(ret).filter(Predicates.notNull());
184     }
185
186     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
187         if (legacy != null) {
188             builder.add(legacy);
189         }
190     }
191
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));
199             } else {
200                 ret.add(toLegacy(child));
201             }
202         }
203         return FluentIterable.from(ret).filter(Predicates.notNull());
204     }
205
206     public DataNormalizationOperation<?> getRootOperation() {
207         return operation;
208     }
209
210 }