Bug 509: Fixed small discrepancies in Binding Data Change Events.
[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.impl.ImmutableCompositeNode;
29 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
30 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 import com.google.common.base.Preconditions;
34 import com.google.common.base.Predicate;
35 import com.google.common.collect.FluentIterable;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.Iterables;
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.getChildren().size() == 1) {
95             final DataNormalizationOperation<?> potentialOp;
96
97             try {
98                 final QName childType = legacyData.getChildren().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         }
137         return null;
138     }
139
140     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
141         if (node instanceof MixinNode) {
142             /**
143              * Direct reading of MixinNodes is not supported, since it is not
144              * possible in legacy APIs create pointer to Mixin Nodes.
145              *
146              */
147             return null;
148         }
149
150         if (node instanceof DataContainerNode<?>) {
151             return toLegacyFromDataContainer((DataContainerNode<?>) node);
152         }
153         return toLegacySimple(node);
154
155     }
156
157     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
158         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
159     }
160
161     @SuppressWarnings({ "unchecked", "rawtypes" })
162     private static CompositeNode toLegacyFromDataContainer(final DataContainerNode<?> node) {
163         CompositeNodeBuilder<ImmutableCompositeNode> builder = ImmutableCompositeNode.builder();
164         builder.setQName(node.getNodeType());
165         for (NormalizedNode<?, ?> child : node.getValue()) {
166             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
167                 builder.addAll(toLegacyNodesFromMixin((NormalizedNodeContainer) child));
168             } else {
169                 addToBuilder(builder, toLegacy(child));
170             }
171         }
172         return builder.toInstance();
173     }
174
175     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
176         if (legacy != null) {
177             builder.add(legacy);
178         }
179     }
180
181     @SuppressWarnings({ "rawtypes", "unchecked" })
182     private static Iterable<Node<?>> toLegacyNodesFromMixin(
183             final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> mixin) {
184         ArrayList<Node<?>> ret = new ArrayList<>();
185         for (NormalizedNode<?, ?> child : mixin.getValue()) {
186             if (child instanceof MixinNode && child instanceof NormalizedNodeContainer<?, ?, ?>) {
187                 Iterables.addAll(ret, toLegacyNodesFromMixin((NormalizedNodeContainer) child));
188             } else {
189                 ret.add(toLegacy(child));
190             }
191         }
192         return FluentIterable.from(ret).filter(new Predicate<Node<?>>() {
193
194             @Override
195             public boolean apply(final Node<?> input) {
196                 return input != null;
197             }
198         });
199     }
200
201     public DataNormalizationOperation<?> getRootOperation() {
202         return operation;
203     }
204
205 }