Deprecate old MD-SAL APIs for removal
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / util / compat / DataNormalizationOperation.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 com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Iterables;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
35 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
38 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
49
50 @Deprecated(forRemoval = true)
51 public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
52
53     private final T identifier;
54     private final DataSchemaNode dataSchemaNode;
55
56     @Override
57     public T getIdentifier() {
58         return identifier;
59     }
60
61     protected DataNormalizationOperation(final T identifier, final SchemaNode schema) {
62         this.identifier = identifier;
63         dataSchemaNode = schema instanceof DataSchemaNode ? (DataSchemaNode)schema : null;
64     }
65
66     public boolean isMixin() {
67         return false;
68     }
69
70
71     public boolean isKeyedEntry() {
72         return false;
73     }
74
75     protected Set<QName> getQNameIdentifiers() {
76         return Collections.singleton(identifier.getNodeType());
77     }
78
79     public abstract DataNormalizationOperation<?> getChild(PathArgument child) throws DataNormalizationException;
80
81     public abstract DataNormalizationOperation<?> getChild(QName child) throws DataNormalizationException;
82
83     public abstract boolean isLeaf();
84
85     @SuppressModernizer
86     public Optional<DataSchemaNode> getDataSchemaNode() {
87         return Optional.fromNullable(dataSchemaNode);
88     }
89
90     private abstract static class SimpleTypeNormalization<T extends PathArgument>
91             extends DataNormalizationOperation<T> {
92         SimpleTypeNormalization(final T identifier, final DataSchemaNode potential) {
93             super(identifier,potential);
94         }
95
96         @Override
97         public DataNormalizationOperation<?> getChild(final PathArgument child) {
98             return null;
99         }
100
101         @Override
102         public DataNormalizationOperation<?> getChild(final QName child) {
103             return null;
104         }
105
106         @Override
107         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
108             return null;
109         }
110
111         @Override
112         public boolean isLeaf() {
113             return true;
114         }
115     }
116
117     private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
118         LeafNormalization(final LeafSchemaNode potential) {
119             super(new NodeIdentifier(potential.getQName()),potential);
120         }
121     }
122
123     private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
124         LeafListEntryNormalization(final LeafListSchemaNode potential) {
125             super(new NodeWithValue(potential.getQName(), null),potential);
126         }
127
128         @Override
129         public boolean isKeyedEntry() {
130             return true;
131         }
132     }
133
134     private abstract static class CompositeNodeNormalizationOperation<T extends PathArgument>
135             extends DataNormalizationOperation<T> {
136         CompositeNodeNormalizationOperation(final T identifier, final DataSchemaNode schema) {
137             super(identifier,schema);
138         }
139
140         @Override
141         public boolean isLeaf() {
142             return false;
143         }
144     }
145
146     private abstract static class DataContainerNormalizationOperation<T extends PathArgument>
147             extends CompositeNodeNormalizationOperation<T> {
148         private final DataNodeContainer schema;
149         private final Map<QName, DataNormalizationOperation<?>> byQName;
150         private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
151
152         DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema,
153                 final DataSchemaNode node) {
154             super(identifier,node);
155             this.schema = schema;
156             this.byArg = new ConcurrentHashMap<>();
157             this.byQName = new ConcurrentHashMap<>();
158         }
159
160         @Override
161         public DataNormalizationOperation<?> getChild(final PathArgument child) throws DataNormalizationException {
162             DataNormalizationOperation<?> potential = byArg.get(child);
163             if (potential != null) {
164                 return potential;
165             }
166             potential = fromLocalSchema(child);
167             return register(potential);
168         }
169
170         @Override
171         public DataNormalizationOperation<?> getChild(final QName child) throws DataNormalizationException {
172             DataNormalizationOperation<?> potential = byQName.get(child);
173             if (potential != null) {
174                 return potential;
175             }
176             potential = fromLocalSchemaAndQName(schema, child);
177             return register(potential);
178         }
179
180         private DataNormalizationOperation<?> fromLocalSchema(final PathArgument child)
181                 throws DataNormalizationException {
182             if (child instanceof AugmentationIdentifier) {
183                 return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
184                         .iterator().next());
185             }
186             return fromSchemaAndQNameChecked(schema, child.getNodeType());
187         }
188
189         protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema2,
190                 final QName child) throws DataNormalizationException {
191             return fromSchemaAndQNameChecked(schema2, child);
192         }
193
194         private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
195             if (potential != null) {
196                 byArg.put(potential.getIdentifier(), potential);
197                 for (final QName qname : potential.getQNameIdentifiers()) {
198                     byQName.put(qname, potential);
199                 }
200             }
201             return potential;
202         }
203
204         private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema,
205                 final QName child) throws DataNormalizationException {
206
207             final DataSchemaNode result = findChildSchemaNode(schema, child);
208             if (result == null) {
209                 throw new DataNormalizationException(String.format(
210                         "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child,
211                         schema,schema.getChildNodes()));
212             }
213
214             // We try to look up if this node was added by augmentation
215             if (schema instanceof DataSchemaNode && result.isAugmenting()) {
216                 return fromAugmentation(schema, (AugmentationTarget) schema, result);
217             }
218             return fromDataSchemaNode(result);
219         }
220     }
221
222     private static final class ListItemNormalization extends
223             DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
224         ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
225             super(identifier, schema, schema);
226         }
227
228         @Override
229         @SuppressFBWarnings("BC_UNCONFIRMED_CAST")
230         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
231             final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
232                     .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
233             for (final Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).entrySet()) {
234                 builder.addChild(Builders.leafBuilder()
235                         //
236                         .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
237                         .build());
238             }
239             return builder.build();
240         }
241
242         @Override
243         public boolean isKeyedEntry() {
244             return true;
245         }
246     }
247
248     private static final class UnkeyedListItemNormalization
249             extends DataContainerNormalizationOperation<NodeIdentifier> {
250         UnkeyedListItemNormalization(final ListSchemaNode schema) {
251             super(new NodeIdentifier(schema.getQName()), schema,schema);
252         }
253
254         @Override
255         @SuppressFBWarnings("BC_UNCONFIRMED_CAST")
256         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
257             return Builders.unkeyedListEntryBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
258         }
259     }
260
261     private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
262         ContainerNormalization(final ContainerSchemaNode schema) {
263             super(new NodeIdentifier(schema.getQName()),schema, schema);
264         }
265
266         @Override
267         @SuppressFBWarnings("BC_UNCONFIRMED_CAST")
268         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
269             return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
270         }
271     }
272
273     private abstract static class MixinNormalizationOp<T extends PathArgument>
274             extends CompositeNodeNormalizationOperation<T> {
275
276         MixinNormalizationOp(final T identifier, final DataSchemaNode schema) {
277             super(identifier,schema);
278         }
279
280         @Override
281         public final boolean isMixin() {
282             return true;
283         }
284     }
285
286     private static final class OrderedLeafListMixinNormalization extends UnorderedLeafListMixinNormalization {
287         OrderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
288             super(potential);
289         }
290
291         @Override
292         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
293             return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier()).build();
294         }
295     }
296
297     private static class UnorderedLeafListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
298
299         private final DataNormalizationOperation<?> innerOp;
300
301         UnorderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
302             super(new NodeIdentifier(potential.getQName()),potential);
303             innerOp = new LeafListEntryNormalization(potential);
304         }
305
306         @Override
307         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
308             return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
309         }
310
311         @Override
312         public DataNormalizationOperation<?> getChild(final PathArgument child) {
313             if (child instanceof NodeWithValue) {
314                 return innerOp;
315             }
316             return null;
317         }
318
319         @Override
320         public DataNormalizationOperation<?> getChild(final QName child) {
321             if (getIdentifier().getNodeType().equals(child)) {
322                 return innerOp;
323             }
324             return null;
325         }
326     }
327
328     private static final class AugmentationNormalization
329             extends DataContainerNormalizationOperation<AugmentationIdentifier> {
330
331         AugmentationNormalization(final AugmentationSchemaNode augmentation, final DataNodeContainer schema) {
332             super(augmentationIdentifierFrom(augmentation), augmentationProxy(augmentation,schema),null);
333         }
334
335         private static DataNodeContainer augmentationProxy(final AugmentationSchemaNode augmentation,
336                 final DataNodeContainer schema) {
337             final Set<DataSchemaNode> children = new HashSet<>();
338             for (final DataSchemaNode augNode : augmentation.getChildNodes()) {
339                 children.add(schema.getDataChildByName(augNode.getQName()));
340             }
341             return new EffectiveAugmentationSchema(augmentation, children);
342         }
343
344         @Override
345         public boolean isMixin() {
346             return true;
347         }
348
349         @Override
350         protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema,
351                 final QName child) {
352             final DataSchemaNode result = findChildSchemaNode(schema, child);
353             if (result == null) {
354                 return null;
355             }
356
357             // We try to look up if this node was added by augmentation
358             if (schema instanceof DataSchemaNode && result.isAugmenting()) {
359                 return fromAugmentation(schema, (AugmentationTarget) schema, result);
360             }
361             return fromDataSchemaNode(result);
362         }
363
364         @Override
365         protected Set<QName> getQNameIdentifiers() {
366             return getIdentifier().getPossibleChildNames();
367         }
368
369         @Override
370         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
371             return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
372         }
373     }
374
375     private static class UnorderedMapMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
376         private final ListItemNormalization innerNode;
377
378         UnorderedMapMixinNormalization(final ListSchemaNode list) {
379             super(new NodeIdentifier(list.getQName()),list);
380             this.innerNode = new ListItemNormalization(NodeIdentifierWithPredicates.of(list.getQName(),
381                     Collections.<QName, Object>emptyMap()), list);
382         }
383
384         @Override
385         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
386             return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
387         }
388
389         @Override
390         public DataNormalizationOperation<?> getChild(final PathArgument child) {
391             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
392                 return innerNode;
393             }
394             return null;
395         }
396
397         @Override
398         public DataNormalizationOperation<?> getChild(final QName child) {
399             if (getIdentifier().getNodeType().equals(child)) {
400                 return innerNode;
401             }
402             return null;
403         }
404     }
405
406     private static class UnkeyedListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
407         private final UnkeyedListItemNormalization innerNode;
408
409         UnkeyedListMixinNormalization(final ListSchemaNode list) {
410             super(new NodeIdentifier(list.getQName()),list);
411             this.innerNode = new UnkeyedListItemNormalization(list);
412         }
413
414         @Override
415         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
416             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier()).build();
417         }
418
419         @Override
420         public DataNormalizationOperation<?> getChild(final PathArgument child) {
421             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
422                 return innerNode;
423             }
424             return null;
425         }
426
427         @Override
428         public DataNormalizationOperation<?> getChild(final QName child) {
429             if (getIdentifier().getNodeType().equals(child)) {
430                 return innerNode;
431             }
432             return null;
433         }
434     }
435
436     private static final class OrderedMapMixinNormalization extends UnorderedMapMixinNormalization {
437         OrderedMapMixinNormalization(final ListSchemaNode list) {
438             super(list);
439         }
440
441         @Override
442         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
443             return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier()).build();
444         }
445     }
446
447     private static class ChoiceNodeNormalization extends MixinNormalizationOp<NodeIdentifier> {
448         private final ImmutableMap<QName, DataNormalizationOperation<?>> byQName;
449         private final ImmutableMap<PathArgument, DataNormalizationOperation<?>> byArg;
450
451         ChoiceNodeNormalization(final ChoiceSchemaNode schema) {
452             super(new NodeIdentifier(schema.getQName()),schema);
453             final ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
454             final ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder =
455                     ImmutableMap.builder();
456
457             for (final CaseSchemaNode caze : schema.getCases().values()) {
458                 for (final DataSchemaNode cazeChild : caze.getChildNodes()) {
459                     final DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
460                     byArgBuilder.put(childOp.getIdentifier(), childOp);
461                     for (final QName qname : childOp.getQNameIdentifiers()) {
462                         byQNameBuilder.put(qname, childOp);
463                     }
464                 }
465             }
466             byQName = byQNameBuilder.build();
467             byArg = byArgBuilder.build();
468         }
469
470         @Override
471         public DataNormalizationOperation<?> getChild(final PathArgument child) {
472             return byArg.get(child);
473         }
474
475         @Override
476         public DataNormalizationOperation<?> getChild(final QName child) {
477             return byQName.get(child);
478         }
479
480         @Override
481         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
482             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
483         }
484     }
485
486     private static class AnyxmlNormalization extends DataNormalizationOperation<NodeIdentifier> {
487         AnyxmlNormalization(final AnyxmlSchemaNode schema) {
488             super(new NodeIdentifier(schema.getQName()), schema);
489         }
490
491         @Override
492         public DataNormalizationOperation<?> getChild(final PathArgument child) {
493             return null;
494         }
495
496         @Override
497         public DataNormalizationOperation<?> getChild(final QName child) {
498             return null;
499         }
500
501         @Override
502         public boolean isLeaf() {
503             return false;
504         }
505
506         @Override
507         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
508             return null;
509         }
510     }
511
512     private static @Nullable DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
513         final DataSchemaNode potential = parent.getDataChildByName(child);
514         return potential != null ? potential : findChoice(parent, child);
515     }
516
517     private static @Nullable ChoiceSchemaNode findChoice(final DataNodeContainer parent, final QName child) {
518         for (final ChoiceSchemaNode choice : Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class)) {
519             for (final CaseSchemaNode caze : choice.getCases().values()) {
520                 if (findChildSchemaNode(caze, child) != null) {
521                     return choice;
522                 }
523             }
524         }
525         return null;
526     }
527
528     public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode augmentation) {
529         final ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
530         for (final DataSchemaNode child : augmentation.getChildNodes()) {
531             potentialChildren.add(child.getQName());
532         }
533         return new AugmentationIdentifier(potentialChildren.build());
534     }
535
536     /**
537      * Returns a DataNormalizationOperation for provided child node.
538      *
539      * <p>
540      * If supplied child is added by Augmentation this operation returns
541      * a DataNormalizationOperation for augmentation,
542      * otherwise returns a DataNormalizationOperation for child as
543      * call for {@link #fromDataSchemaNode(DataSchemaNode)}.
544      */
545     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
546             justification = "https://github.com/spotbugs/spotbugs/issues/811")
547     private static DataNormalizationOperation<?> fromAugmentation(final DataNodeContainer parent,
548             final AugmentationTarget parentAug, final DataSchemaNode child) {
549         AugmentationSchemaNode augmentation = null;
550         for (final AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
551             final DataSchemaNode potential = aug.getDataChildByName(child.getQName());
552             if (potential != null) {
553                 augmentation = aug;
554                 break;
555             }
556
557         }
558         if (augmentation != null) {
559             return new AugmentationNormalization(augmentation, parent);
560         } else {
561             return fromDataSchemaNode(child);
562         }
563     }
564
565     public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) {
566         if (potential instanceof ContainerSchemaNode) {
567             return new ContainerNormalization((ContainerSchemaNode) potential);
568         } else if (potential instanceof ListSchemaNode) {
569
570             return fromListSchemaNode((ListSchemaNode) potential);
571         } else if (potential instanceof LeafSchemaNode) {
572             return new LeafNormalization((LeafSchemaNode) potential);
573         } else if (potential instanceof ChoiceSchemaNode) {
574             return new ChoiceNodeNormalization((ChoiceSchemaNode) potential);
575         } else if (potential instanceof LeafListSchemaNode) {
576             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
577         } else if (potential instanceof AnyxmlSchemaNode) {
578             return new AnyxmlNormalization((AnyxmlSchemaNode) potential);
579         }
580         return null;
581     }
582
583     private static DataNormalizationOperation<?> fromListSchemaNode(final ListSchemaNode potential) {
584         final List<QName> keyDefinition = potential.getKeyDefinition();
585         if (keyDefinition == null || keyDefinition.isEmpty()) {
586             return new UnkeyedListMixinNormalization(potential);
587         }
588         if (potential.isUserOrdered()) {
589             return new OrderedMapMixinNormalization(potential);
590         }
591         return new UnorderedMapMixinNormalization(potential);
592     }
593
594     private static DataNormalizationOperation<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
595         if (potential.isUserOrdered()) {
596             return new OrderedLeafListMixinNormalization(potential);
597         }
598         return new UnorderedLeafListMixinNormalization(potential);
599     }
600
601
602     public static DataNormalizationOperation<?> from(final SchemaContext ctx) {
603         return new ContainerNormalization(ctx);
604     }
605
606     public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);
607 }

©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.