BUG-868: do not use deprecated InstanceIdentifier methods
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.FluentIterable;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.opendaylight.yangtools.concepts.Identifiable;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.Node;
35 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
44 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
46 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
47 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
48 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55
56 public abstract class DataNormalizationOperation<T extends PathArgument> implements Identifiable<T> {
57
58     private final T identifier;
59
60     @Override
61     public T getIdentifier() {
62         return identifier;
63     };
64
65     protected DataNormalizationOperation(final T identifier) {
66         super();
67         this.identifier = identifier;
68     }
69
70     public boolean isMixin() {
71         return false;
72     }
73
74
75     public boolean isKeyedEntry() {
76         return false;
77     }
78
79     protected Set<QName> getQNameIdentifiers() {
80         return Collections.singleton(identifier.getNodeType());
81     }
82
83     public abstract DataNormalizationOperation<?> getChild(final PathArgument child) throws DataNormalizationException;
84
85     public abstract DataNormalizationOperation<?> getChild(QName child) throws DataNormalizationException;
86
87     public abstract NormalizedNode<?, ?> normalize(Node<?> legacyData);
88
89     public abstract boolean isLeaf();
90
91     private static abstract class SimpleTypeNormalization<T extends PathArgument> extends DataNormalizationOperation<T> {
92
93         protected SimpleTypeNormalization(final T identifier) {
94             super(identifier);
95         }
96
97         @Override
98         public NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
99             checkArgument(legacyData != null);
100             checkArgument(legacyData instanceof SimpleNode<?>);
101             return normalizeImpl((SimpleNode<?>) legacyData);
102         }
103
104         protected abstract NormalizedNode<?, ?> normalizeImpl(SimpleNode<?> node);
105
106         @Override
107         public DataNormalizationOperation<?> getChild(final PathArgument child) {
108             return null;
109         }
110
111         @Override
112         public DataNormalizationOperation<?> getChild(final QName child) {
113             return null;
114         }
115
116         @Override
117         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
118             return null;
119         }
120
121         @Override
122         public boolean isLeaf() {
123             return true;
124         }
125
126     }
127
128     private static final class LeafNormalization extends SimpleTypeNormalization<NodeIdentifier> {
129
130         protected LeafNormalization(final NodeIdentifier identifier) {
131             super(identifier);
132         }
133
134         @Override
135         protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
136             return ImmutableNodes.leafNode(node.getNodeType(), node.getValue());
137         }
138
139     }
140
141     private static final class LeafListEntryNormalization extends SimpleTypeNormalization<NodeWithValue> {
142
143         public LeafListEntryNormalization(final LeafListSchemaNode potential) {
144             super(new NodeWithValue(potential.getQName(), null));
145         }
146
147         @Override
148         protected NormalizedNode<?, ?> normalizeImpl(final SimpleNode<?> node) {
149             NodeWithValue nodeId = new NodeWithValue(node.getNodeType(), node.getValue());
150             return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
151         }
152
153
154         @Override
155         public boolean isKeyedEntry() {
156             return true;
157         }
158     }
159
160     private static abstract class CompositeNodeNormalizationOperation<T extends PathArgument> extends
161     DataNormalizationOperation<T> {
162
163         protected CompositeNodeNormalizationOperation(final T identifier) {
164             super(identifier);
165         }
166
167         @SuppressWarnings({ "rawtypes", "unchecked" })
168         @Override
169         public final NormalizedNode<?, ?> normalize(final Node<?> legacyData) {
170             checkArgument(legacyData != null);
171             if (!isMixin() && getIdentifier().getNodeType() != null) {
172                 checkArgument(getIdentifier().getNodeType().equals(legacyData.getNodeType()),
173                         "Node QName must be %s was %s", getIdentifier().getNodeType(), legacyData.getNodeType());
174             }
175             checkArgument(legacyData instanceof CompositeNode, "Node %s should be composite", legacyData);
176             CompositeNode compositeNode = (CompositeNode) legacyData;
177             NormalizedNodeContainerBuilder builder = createBuilder(compositeNode);
178
179             Set<DataNormalizationOperation<?>> usedMixins = new HashSet<>();
180             for (Node<?> childLegacy : compositeNode.getValue()) {
181                 final DataNormalizationOperation childOp;
182
183                 try {
184                     childOp = getChild(childLegacy.getNodeType());
185                 } catch (DataNormalizationException e) {
186                     throw new IllegalArgumentException(String.format("Failed to normalize data %s", compositeNode.getValue()), e);
187                 }
188
189                 // We skip unknown nodes if this node is mixin since
190                 // it's nodes and parent nodes are interleaved
191                 if (childOp == null && isMixin()) {
192                     continue;
193                 }
194
195                 checkArgument(childOp != null, "Node %s is not allowed inside %s", childLegacy.getNodeType(),
196                         getIdentifier());
197                 if (childOp.isMixin()) {
198                     if (usedMixins.contains(childOp)) {
199                         // We already run / processed that mixin, so to avoid
200                         // duplicity we are skipping next nodes.
201                         continue;
202                     }
203                     builder.addChild(childOp.normalize(compositeNode));
204                     usedMixins.add(childOp);
205                 } else {
206                     builder.addChild(childOp.normalize(childLegacy));
207                 }
208             }
209             return builder.build();
210         }
211
212         @Override
213         public boolean isLeaf() {
214             return false;
215         }
216
217         @SuppressWarnings("rawtypes")
218         protected abstract NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode);
219
220     }
221
222     private static abstract class DataContainerNormalizationOperation<T extends PathArgument> extends
223     CompositeNodeNormalizationOperation<T> {
224
225         private final DataNodeContainer schema;
226         private final Map<QName, DataNormalizationOperation<?>> byQName;
227         private final Map<PathArgument, DataNormalizationOperation<?>> byArg;
228
229         protected DataContainerNormalizationOperation(final T identifier, final DataNodeContainer schema) {
230             super(identifier);
231             this.schema = schema;
232             this.byArg = new ConcurrentHashMap<>();
233             this.byQName = new ConcurrentHashMap<>();
234         }
235
236         @Override
237         public DataNormalizationOperation<?> getChild(final PathArgument child) throws DataNormalizationException {
238             DataNormalizationOperation<?> potential = byArg.get(child);
239             if (potential != null) {
240                 return potential;
241             }
242             potential = fromLocalSchema(child);
243             return register(potential);
244         }
245
246         private DataNormalizationOperation<?> fromLocalSchema(final PathArgument child) throws DataNormalizationException {
247             if (child instanceof AugmentationIdentifier) {
248                 return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
249                         .iterator().next());
250             }
251             return fromSchemaAndQNameChecked(schema, child.getNodeType());
252         }
253
254         @Override
255         public DataNormalizationOperation<?> getChild(final QName child) throws DataNormalizationException {
256             DataNormalizationOperation<?> potential = byQName.get(child);
257             if (potential != null) {
258                 return potential;
259             }
260             potential = fromLocalSchemaAndQName(schema, child);
261             return register(potential);
262         }
263
264         protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema2, final QName child) throws DataNormalizationException {
265             return fromSchemaAndQNameChecked(schema2, child);
266         }
267
268         private DataNormalizationOperation<?> register(final DataNormalizationOperation<?> potential) {
269             if (potential != null) {
270                 byArg.put(potential.getIdentifier(), potential);
271                 for (QName qName : potential.getQNameIdentifiers()) {
272                     byQName.put(qName, potential);
273                 }
274             }
275             return potential;
276         }
277
278     }
279
280     private static final class ListItemNormalization extends
281     DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
282
283         private final List<QName> keyDefinition;
284
285         protected ListItemNormalization(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
286             super(identifier, schema);
287             keyDefinition = schema.getKeyDefinition();
288         }
289
290         @Override
291         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
292             ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
293             for (QName key : keyDefinition) {
294
295                 SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),
296                         "List node %s MUST contain leaf %s with value.", getIdentifier().getNodeType(), key);
297                 keys.put(key, valueNode.getValue());
298             }
299
300             return Builders.mapEntryBuilder().withNodeIdentifier(
301                     new NodeIdentifierWithPredicates(getIdentifier().getNodeType(), keys.build()));
302         }
303
304         @Override
305         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
306             DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder = Builders
307                     .mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) currentArg);
308             for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg).getKeyValues().entrySet()) {
309                 builder.addChild(Builders.leafBuilder()
310                         //
311                         .withNodeIdentifier(new NodeIdentifier(keyValue.getKey())).withValue(keyValue.getValue())
312                         .build());
313             }
314             return builder.build();
315         }
316
317
318         @Override
319         public boolean isKeyedEntry() {
320             return true;
321         }
322     }
323
324     private static final class UnkeyedListItemNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
325
326         protected UnkeyedListItemNormalization(final ListSchemaNode schema) {
327             super(new NodeIdentifier(schema.getQName()), schema);
328         }
329
330         @Override
331         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
332             return Builders.unkeyedListEntryBuilder().withNodeIdentifier(getIdentifier());
333         }
334
335         @Override
336         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
337             return Builders.unkeyedListEntryBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
338         }
339
340     }
341
342     private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
343
344         protected ContainerNormalization(final ContainerSchemaNode schema) {
345             super(new NodeIdentifier(schema.getQName()), schema);
346         }
347
348         @Override
349         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
350             return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
351         }
352
353         @Override
354         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
355             return Builders.containerBuilder().withNodeIdentifier((NodeIdentifier) currentArg).build();
356         }
357
358     }
359
360     private static abstract class MixinNormalizationOp<T extends PathArgument> extends
361     CompositeNodeNormalizationOperation<T> {
362
363         protected MixinNormalizationOp(final T identifier) {
364             super(identifier);
365         }
366
367         @Override
368         public final boolean isMixin() {
369             return true;
370         }
371
372     }
373
374
375     private static final class OrderedLeafListMixinNormalization extends UnorderedLeafListMixinNormalization {
376
377
378         public OrderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
379             super(potential);
380         }
381
382         @Override
383         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
384             return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier());
385         }
386
387         @Override
388         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
389             return Builders.orderedLeafSetBuilder().withNodeIdentifier(getIdentifier()).build();
390         }
391     }
392
393     private static class UnorderedLeafListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
394
395         private final DataNormalizationOperation<?> innerOp;
396
397         public UnorderedLeafListMixinNormalization(final LeafListSchemaNode potential) {
398             super(new NodeIdentifier(potential.getQName()));
399             innerOp = new LeafListEntryNormalization(potential);
400         }
401
402         @Override
403         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
404             return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
405         }
406
407         @Override
408         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
409             return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier()).build();
410         }
411
412         @Override
413         public DataNormalizationOperation<?> getChild(final PathArgument child) {
414             if (child instanceof NodeWithValue) {
415                 return innerOp;
416             }
417             return null;
418         }
419
420         @Override
421         public DataNormalizationOperation<?> getChild(final QName child) {
422             if (getIdentifier().getNodeType().equals(child)) {
423                 return innerOp;
424             }
425             return null;
426         }
427     }
428
429     private static final class AugmentationNormalization extends DataContainerNormalizationOperation<AugmentationIdentifier> {
430
431         public AugmentationNormalization(final AugmentationSchema augmentation, final DataNodeContainer schema) {
432             //super();
433             super(augmentationIdentifierFrom(augmentation), augmentationProxy(augmentation,schema));
434         }
435
436         @Override
437         public boolean isMixin() {
438             return true;
439         }
440
441
442
443         @Override
444         protected DataNormalizationOperation<?> fromLocalSchemaAndQName(final DataNodeContainer schema, final QName child)
445                 throws DataNormalizationException {
446             Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
447             if (!potential.isPresent()) {
448                 return null;
449             }
450
451             DataSchemaNode result = potential.get();
452             // We try to look up if this node was added by augmentation
453             if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
454                 return fromAugmentation(schema, (AugmentationTarget) schema, result);
455             }
456             return fromDataSchemaNode(result);
457         }
458
459         @Override
460         protected Set<QName> getQNameIdentifiers() {
461             return getIdentifier().getPossibleChildNames();
462         }
463
464         @SuppressWarnings("rawtypes")
465         @Override
466         protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
467             return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
468         }
469
470         @Override
471         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
472             return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier()).build();
473         }
474
475     }
476
477     private static class UnorderedMapMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
478
479         private final ListItemNormalization innerNode;
480
481         public UnorderedMapMixinNormalization(final ListSchemaNode list) {
482             super(new NodeIdentifier(list.getQName()));
483             this.innerNode = new ListItemNormalization(new NodeIdentifierWithPredicates(list.getQName(),
484                     Collections.<QName, Object> emptyMap()), list);
485         }
486
487         @SuppressWarnings("rawtypes")
488         @Override
489         protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
490             return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
491         }
492
493         @Override
494         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
495             return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
496         }
497
498         @Override
499         public DataNormalizationOperation<?> getChild(final PathArgument child) {
500             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
501                 return innerNode;
502             }
503             return null;
504         }
505
506         @Override
507         public DataNormalizationOperation<?> getChild(final QName child) {
508             if (getIdentifier().getNodeType().equals(child)) {
509                 return innerNode;
510             }
511             return null;
512         }
513
514     }
515
516
517     private static class UnkeyedListMixinNormalization extends MixinNormalizationOp<NodeIdentifier> {
518
519         private final UnkeyedListItemNormalization innerNode;
520
521         public UnkeyedListMixinNormalization(final ListSchemaNode list) {
522             super(new NodeIdentifier(list.getQName()));
523             this.innerNode = new UnkeyedListItemNormalization(list);
524         }
525
526         @SuppressWarnings("rawtypes")
527         @Override
528         protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
529             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier());
530         }
531
532         @Override
533         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
534             return Builders.unkeyedListBuilder().withNodeIdentifier(getIdentifier()).build();
535         }
536
537         @Override
538         public DataNormalizationOperation<?> getChild(final PathArgument child) {
539             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
540                 return innerNode;
541             }
542             return null;
543         }
544
545         @Override
546         public DataNormalizationOperation<?> getChild(final QName child) {
547             if (getIdentifier().getNodeType().equals(child)) {
548                 return innerNode;
549             }
550             return null;
551         }
552
553     }
554
555     private static final class OrderedMapMixinNormalization extends UnorderedMapMixinNormalization {
556
557         public OrderedMapMixinNormalization(final ListSchemaNode list) {
558             super(list);
559         }
560
561         @SuppressWarnings("rawtypes")
562         @Override
563         protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
564             return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier());
565         }
566
567         @Override
568         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
569             return Builders.orderedMapBuilder().withNodeIdentifier(getIdentifier()).build();
570         }
571
572     }
573
574     private static class ChoiceNodeNormalization extends MixinNormalizationOp<NodeIdentifier> {
575
576         private final ImmutableMap<QName, DataNormalizationOperation<?>> byQName;
577         private final ImmutableMap<PathArgument, DataNormalizationOperation<?>> byArg;
578
579         protected ChoiceNodeNormalization(final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
580             super(new NodeIdentifier(schema.getQName()));
581             ImmutableMap.Builder<QName, DataNormalizationOperation<?>> byQNameBuilder = ImmutableMap.builder();
582             ImmutableMap.Builder<PathArgument, DataNormalizationOperation<?>> byArgBuilder = ImmutableMap.builder();
583
584             for (ChoiceCaseNode caze : schema.getCases()) {
585                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
586                     DataNormalizationOperation<?> childOp = fromDataSchemaNode(cazeChild);
587                     byArgBuilder.put(childOp.getIdentifier(), childOp);
588                     for (QName qname : childOp.getQNameIdentifiers()) {
589                         byQNameBuilder.put(qname, childOp);
590                     }
591                 }
592             }
593             byQName = byQNameBuilder.build();
594             byArg = byArgBuilder.build();
595         }
596
597         @Override
598         public DataNormalizationOperation<?> getChild(final PathArgument child) {
599             return byArg.get(child);
600         }
601
602         @Override
603         public DataNormalizationOperation<?> getChild(final QName child) {
604             return byQName.get(child);
605         }
606
607         @Override
608         protected NormalizedNodeContainerBuilder<?, ?, ?, ?> createBuilder(final CompositeNode compositeNode) {
609             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
610         }
611
612         @Override
613         public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
614             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier()).build();
615         }
616     }
617
618     private static class AnyXmlNormalization extends DataNormalizationOperation<NodeIdentifier> {
619
620         protected AnyXmlNormalization( final NodeIdentifier identifier ) {
621             super( identifier );
622         }
623
624         @Override
625         public DataNormalizationOperation<?> getChild( final PathArgument child ) throws DataNormalizationException {
626             return null;
627         }
628
629         @Override
630         public DataNormalizationOperation<?> getChild( final QName child ) throws DataNormalizationException {
631             return null;
632         }
633
634         @Override
635         public NormalizedNode<?, ?> normalize( final Node<?> legacyData ) {
636             NormalizedNodeAttrBuilder<NodeIdentifier, Node<?>, AnyXmlNode> builder =
637                     Builders.anyXmlBuilder().withNodeIdentifier(
638                             new NodeIdentifier( legacyData.getNodeType() ) );
639             builder.withValue(legacyData);
640             return builder.build();
641         }
642
643         @Override
644         public boolean isLeaf() {
645             return false;
646         }
647
648         @Override
649         public NormalizedNode<?, ?> createDefault( final PathArgument currentArg ) {
650             return null;
651         }
652     }
653
654     private static final Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent,final QName child) {
655         DataSchemaNode potential = parent.getDataChildByName(child);
656         if (potential == null) {
657             Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices = FluentIterable.from(
658                     parent.getChildNodes()).filter(org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
659             potential = findChoice(choices, child);
660         }
661         return Optional.fromNullable(potential);
662     }
663
664     private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema,
665             final QName child) throws DataNormalizationException {
666
667         Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
668         if (!potential.isPresent()) {
669             throw new DataNormalizationException(String.format("Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,schema.getChildNodes()));
670         }
671
672         DataSchemaNode result = potential.get();
673         // We try to look up if this node was added by augmentation
674         if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
675             return fromAugmentation(schema, (AugmentationTarget) schema, result);
676         }
677         return fromDataSchemaNode(result);
678     }
679
680     private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
681             final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices, final QName child) {
682         org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
683         choiceLoop: for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
684             for (ChoiceCaseNode caze : choice.getCases()) {
685                 if (findChildSchemaNode(caze, child).isPresent()) {
686                     foundChoice = choice;
687                     break choiceLoop;
688                 }
689             }
690         }
691         return foundChoice;
692     }
693
694     public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
695         ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
696         for (DataSchemaNode child : augmentation.getChildNodes()) {
697             potentialChildren.add(child.getQName());
698         }
699         return new AugmentationIdentifier(potentialChildren.build());
700     }
701
702     private static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation, final DataNodeContainer schema) {
703         Set<DataSchemaNode> children = new HashSet<>();
704         for (DataSchemaNode augNode : augmentation.getChildNodes()) {
705             children.add(schema.getDataChildByName(augNode.getQName()));
706         }
707         return new DataSchemaContainerProxy(children);
708     }
709
710     /**
711      * Returns a DataNormalizationOperation for provided child node
712      *
713      * If supplied child is added by Augmentation this operation returns
714      * a DataNormalizationOperation for augmentation,
715      * otherwise returns a DataNormalizationOperation for child as
716      * call for {@link #fromDataSchemaNode(DataSchemaNode)}.
717      *
718      *
719      * @param parent
720      * @param parentAug
721      * @param child
722      * @return
723      */
724     private static DataNormalizationOperation<?> fromAugmentation(final DataNodeContainer parent,
725             final AugmentationTarget parentAug, final DataSchemaNode child) {
726         AugmentationSchema augmentation = null;
727         for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
728             DataSchemaNode potential = aug.getDataChildByName(child.getQName());
729             if (potential != null) {
730                 augmentation = aug;
731                 break;
732             }
733
734         }
735         if (augmentation != null) {
736             return new AugmentationNormalization(augmentation, parent);
737         } else {
738             return fromDataSchemaNode(child);
739         }
740     }
741
742     public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) {
743         if (potential instanceof ContainerSchemaNode) {
744             return new ContainerNormalization((ContainerSchemaNode) potential);
745         } else if (potential instanceof ListSchemaNode) {
746
747             return fromListSchemaNode((ListSchemaNode) potential);
748         } else if (potential instanceof LeafSchemaNode) {
749             return new LeafNormalization(new NodeIdentifier(potential.getQName()));
750         } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
751             return new ChoiceNodeNormalization((org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
752         } else if (potential instanceof LeafListSchemaNode) {
753             return fromLeafListSchemaNode((LeafListSchemaNode) potential);
754         } else if (potential instanceof AnyXmlSchemaNode) {
755             return new AnyXmlNormalization( new NodeIdentifier(potential.getQName() ) );
756         }
757         return null;
758     }
759
760     private static DataNormalizationOperation<?> fromListSchemaNode(final ListSchemaNode potential) {
761         List<QName> keyDefinition = potential.getKeyDefinition();
762         if(keyDefinition == null || keyDefinition.isEmpty()) {
763             return new UnkeyedListMixinNormalization(potential);
764         }
765         if(potential.isUserOrdered()) {
766             return new OrderedMapMixinNormalization(potential);
767         }
768         return new UnorderedMapMixinNormalization(potential);
769     }
770
771     private static DataNormalizationOperation<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
772         if(potential.isUserOrdered()) {
773             return new OrderedLeafListMixinNormalization(potential);
774         }
775         return new UnorderedLeafListMixinNormalization(potential);
776     }
777
778
779     public static DataNormalizationOperation<?> from(final SchemaContext ctx) {
780         return new ContainerNormalization(ctx);
781     }
782
783     public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);
784 }

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