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

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