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

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