BUG-692 Improve log message when negotiation fails
[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 import java.util.AbstractMap;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.Map;
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;
38
39 public class DataNormalizer {
40
41     private final DataNormalizationOperation<?> operation;
42
43     public DataNormalizer(final SchemaContext ctx) {
44         operation = DataNormalizationOperation.from(ctx);
45     }
46
47     public InstanceIdentifier toNormalized(final InstanceIdentifier legacy) {
48         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
49
50         DataNormalizationOperation<?> currentOp = operation;
51         Iterator<PathArgument> arguments = legacy.getPath().iterator();
52
53         try {
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());
63                 }
64                 normalizedArgs.add(legacyArg);
65             }
66         } catch (DataNormalizationException e) {
67             throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e);
68         }
69
70         return new InstanceIdentifier(normalizedArgs.build());
71     }
72
73     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(
74             final Map.Entry<InstanceIdentifier, CompositeNode> legacy) {
75         return toNormalized(legacy.getKey(), legacy.getValue());
76     }
77
78     public Map.Entry<InstanceIdentifier, NormalizedNode<?, ?>> toNormalized(final InstanceIdentifier legacyPath,
79             final CompositeNode legacyData) {
80
81         InstanceIdentifier normalizedPath = toNormalized(legacyPath);
82
83         DataNormalizationOperation<?> currentOp = operation;
84         for (PathArgument arg : normalizedPath.getPath()) {
85             try {
86                 currentOp = currentOp.getChild(arg);
87             } catch (DataNormalizationException e) {
88                 throw new IllegalArgumentException(String.format("Failed to validate normalized path %s",
89                         normalizedPath), e);
90             }
91         }
92
93         // Write Augmentation data resolution
94         if (legacyData.getValue().size() == 1) {
95             final DataNormalizationOperation<?> potentialOp;
96
97             try {
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);
102             }
103
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);
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         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);
126             }
127         }
128         return new InstanceIdentifier(legacyArgs.build());
129     }
130
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;
139         }
140         return null;
141     }
142
143     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
144         if (node instanceof MixinNode) {
145             /**
146              * Direct reading of MixinNodes is not supported, since it is not
147              * possible in legacy APIs create pointer to Mixin Nodes.
148              *
149              */
150             return null;
151         }
152
153         if (node instanceof DataContainerNode<?>) {
154             return toLegacyFromDataContainer((DataContainerNode<?>) node);
155         } else if (node instanceof AnyXmlNode) {
156             return ((AnyXmlNode) node).getValue();
157         }
158         return toLegacySimple(node);
159
160     }
161
162     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
163         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
164     }
165
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));
175             } else {
176                 addToBuilder(builder, toLegacy(child));
177             }
178         }
179         return builder.toInstance();
180     }
181
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));
186         }
187         return FluentIterable.from(ret).filter(Predicates.notNull());
188     }
189
190     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
191         if (legacy != null) {
192             builder.add(legacy);
193         }
194     }
195
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));
203             } else {
204                 ret.add(toLegacy(child));
205             }
206         }
207         return FluentIterable.from(ret).filter(Predicates.notNull());
208     }
209
210     public DataNormalizationOperation<?> getRootOperation() {
211         return operation;
212     }
213
214 }