Bug 509: Added In-memory datastore support for wildcarded change listeners
[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.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;
34
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;
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.getPath().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 new InstanceIdentifier(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.getPath()) {
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.getChildren().size() == 1) {
97             final DataNormalizationOperation<?> potentialOp;
98
99             try {
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);
104             }
105
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);
111             }
112         }
113
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));
118     }
119
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);
127                 }
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
134             }
135             // FIXME : Add option for reading choice
136         }
137         if (previous != null) {
138             legacyArgs.add(previous);
139         }
140         return new InstanceIdentifier(legacyArgs.build());
141     }
142
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);
148         }
149         return null;
150     }
151
152     public static Node<?> toLegacy(final NormalizedNode<?, ?> node) {
153         if (node instanceof MixinNode) {
154             /**
155              * Direct reading of MixinNodes is not supported, since it is not
156              * possible in legacy APIs create pointer to Mixin Nodes.
157              *
158              */
159             return null;
160         }
161
162         if (node instanceof DataContainerNode<?>) {
163             return toLegacyFromDataContainer((DataContainerNode<?>) node);
164         }
165         return toLegacySimple(node);
166
167     }
168
169     private static SimpleNode<?> toLegacySimple(final NormalizedNode<?, ?> node) {
170         return new SimpleNodeTOImpl<Object>(node.getNodeType(), null, node.getValue());
171     }
172
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));
180             } else {
181                 addToBuilder(builder, toLegacy(child));
182             }
183         }
184         return builder.toInstance();
185     }
186
187     private static void addToBuilder(final CompositeNodeBuilder<ImmutableCompositeNode> builder, final Node<?> legacy) {
188         if (legacy != null) {
189             builder.add(legacy);
190         }
191     }
192
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));
200             } else {
201                 ret.add(toLegacy(child));
202             }
203         }
204         return FluentIterable.from(ret).filter(new Predicate<Node<?>>() {
205
206             @Override
207             public boolean apply(final Node<?> input) {
208                 return input != null;
209             }
210         });
211     }
212
213     public DataNormalizationOperation<?> getRootOperation() {
214         return operation;
215     }
216
217 }