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

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