Merge "Bug2174: XSQL log file is hardcode to /tmp/xql.log"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / NodeToNormalizedNodeBuilder.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.FluentIterable;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
18 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
39 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
40 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import java.util.Collections;
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Map.Entry;
56 import java.util.Set;
57 import java.util.concurrent.ConcurrentHashMap;
58
59 import static com.google.common.base.Preconditions.checkArgument;
60
61 /**
62  * NormalizedNodeBuilder is a builder that walks through a tree like structure and constructs a
63  * NormalizedNode from it.
64  * <p/>
65  * A large part of this code has been copied over from a similar class in sal-common-impl which was
66  * originally supposed to convert a CompositeNode to NormalizedNode
67  *
68  * @param <T>
69  */
70 public abstract class NodeToNormalizedNodeBuilder<T extends PathArgument>
71     implements Identifiable<T> {
72
73     private final T identifier;
74
75     protected static final Logger logger = LoggerFactory
76         .getLogger(NodeToNormalizedNodeBuilder.class);
77
78     @Override
79     public T getIdentifier() {
80         return identifier;
81     }
82
83     ;
84
85     protected NodeToNormalizedNodeBuilder(final T identifier) {
86         super();
87         this.identifier = identifier;
88
89     }
90
91     /**
92      * @return Should return true if the node that this operation corresponds to is a mixin
93      */
94     public boolean isMixin() {
95         return false;
96     }
97
98
99     /**
100      * @return Should return true if the node that this operation corresponds to has a 'key'
101      * associated with it. This is typically true for a list-item or leaf-list entry in yang
102      */
103     public boolean isKeyedEntry() {
104         return false;
105     }
106
107     protected Set<QName> getQNameIdentifiers() {
108         return Collections.singleton(identifier.getNodeType());
109     }
110
111     public abstract NodeToNormalizedNodeBuilder<?> getChild(
112         final PathArgument child);
113
114     public abstract NodeToNormalizedNodeBuilder<?> getChild(QName child);
115
116     public abstract NormalizedNode<?, ?> normalize(QName nodeType, Node node);
117
118
119
120     private static abstract class SimpleTypeNormalization<T extends PathArgument>
121         extends NodeToNormalizedNodeBuilder<T> {
122
123         protected SimpleTypeNormalization(final T identifier) {
124             super(identifier);
125         }
126
127         @Override
128         public NormalizedNode<?, ?> normalize(final QName nodeType,
129             final Node node) {
130             checkArgument(node != null);
131             return normalizeImpl(nodeType, node);
132         }
133
134         protected abstract NormalizedNode<?, ?> normalizeImpl(QName nodeType,
135             Node node);
136
137         @Override
138         public NodeToNormalizedNodeBuilder<?> getChild(
139             final PathArgument child) {
140             return null;
141         }
142
143         @Override
144         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
145             return null;
146         }
147
148         @Override
149         public NormalizedNode<?, ?> createDefault(
150             final PathArgument currentArg) {
151             // TODO Auto-generated method stub
152             return null;
153         }
154
155     }
156
157
158     private static final class LeafNormalization extends
159         SimpleTypeNormalization<NodeIdentifier> {
160
161         private final LeafSchemaNode schema;
162
163         protected LeafNormalization(final LeafSchemaNode schema, final NodeIdentifier identifier) {
164             super(identifier);
165             this.schema = schema;
166         }
167
168         @Override
169         protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
170             final Node node) {
171             Object value = NodeValueCodec.toTypeSafeValue(this.schema, this.schema.getType(), node);
172             return ImmutableNodes.leafNode(nodeType, value);
173
174         }
175
176     }
177
178
179     private static final class LeafListEntryNormalization extends
180         SimpleTypeNormalization<NodeWithValue> {
181
182         private final LeafListSchemaNode schema;
183
184         public LeafListEntryNormalization(final LeafListSchemaNode potential) {
185             super(new NodeWithValue(potential.getQName(), null));
186             this.schema = potential;
187         }
188
189         @Override
190         protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
191             final Node node) {
192             final Object data = node.getValue();
193             if (data == null) {
194                 Preconditions.checkArgument(false,
195                     "No data available in leaf list entry for " + nodeType);
196             }
197
198             Object value = NodeValueCodec.toTypeSafeValue(this.schema, this.schema.getType(), node);
199
200             NodeWithValue nodeId = new NodeWithValue(nodeType, value);
201             return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId)
202                 .withValue(value).build();
203         }
204
205
206         @Override
207         public boolean isKeyedEntry() {
208             return true;
209         }
210     }
211
212
213     private static abstract class NodeToNormalizationNodeOperation<T extends PathArgument>
214         extends NodeToNormalizedNodeBuilder<T> {
215
216         protected NodeToNormalizationNodeOperation(final T identifier) {
217             super(identifier);
218         }
219
220         @SuppressWarnings({"rawtypes", "unchecked"})
221         @Override
222         public final NormalizedNodeContainer<?, ?, ?> normalize(
223             final QName nodeType, final Node node) {
224             checkArgument(node != null);
225
226             if (!node.getType().equals(AugmentationNode.class.getSimpleName())
227                 && !node.getType().equals(ContainerNode.class.getSimpleName())
228                 && !node.getType().equals(MapNode.class.getSimpleName())) {
229                 checkArgument(nodeType != null);
230             }
231
232             NormalizedNodeContainerBuilder builder = createBuilder(node);
233
234             Set<NodeToNormalizedNodeBuilder<?>> usedMixins = new HashSet<>();
235
236             logNode(node);
237
238             if (node.getChildCount() == 0 && (
239                 node.getType().equals(LeafSetEntryNode.class.getSimpleName())
240                     || node.getType().equals(LeafNode.class.getSimpleName()))) {
241                 PathArgument childPathArgument =
242                     NodeIdentifierFactory.getArgument(node.getPath());
243
244                 final NormalizedNode child;
245                 if (childPathArgument instanceof NodeWithValue) {
246                     final NodeWithValue nodeWithValue =
247                         new NodeWithValue(childPathArgument.getNodeType(),
248                             node.getValue());
249                     child =
250                         Builders.leafSetEntryBuilder()
251                             .withNodeIdentifier(nodeWithValue)
252                             .withValue(node.getValue()).build();
253                 } else {
254                     child =
255                         ImmutableNodes.leafNode(childPathArgument.getNodeType(),
256                             node.getValue());
257                 }
258                 builder.addChild(child);
259             }
260
261             final List<Node> children = node.getChildList();
262             for (Node nodeChild : children) {
263
264                 PathArgument childPathArgument =
265                     NodeIdentifierFactory.getArgument(nodeChild.getPath());
266
267                 QName childNodeType = null;
268                 NodeToNormalizedNodeBuilder childOp = null;
269
270                 if (childPathArgument instanceof AugmentationIdentifier) {
271                     childOp = getChild(childPathArgument);
272                     checkArgument(childOp instanceof AugmentationNormalization, childPathArgument);
273                 } else {
274                     childNodeType = childPathArgument.getNodeType();
275                     childOp = getChild(childNodeType);
276                 }
277                 // We skip unknown nodes if this node is mixin since
278                 // it's nodes and parent nodes are interleaved
279                 if (childOp == null && isMixin()) {
280                     continue;
281                 } else if (childOp == null) {
282                     logger.error(
283                         "childOp is null and this operation is not a mixin : this = {}",
284                         this.toString());
285                 }
286
287                 checkArgument(childOp != null,
288                     "Node %s is not allowed inside %s",
289                     childNodeType, getIdentifier());
290
291                 if (childOp.isMixin()) {
292                     if (usedMixins.contains(childOp)) {
293                         // We already run / processed that mixin, so to avoid
294                         // duplicate we are
295                         // skipping next nodes.
296                         continue;
297                     }
298                     // builder.addChild(childOp.normalize(nodeType, treeCacheNode));
299                     final NormalizedNode childNode =
300                         childOp.normalize(childNodeType, nodeChild);
301                     if (childNode != null)
302                         builder.addChild(childNode);
303                     usedMixins.add(childOp);
304                 } else {
305                     final NormalizedNode childNode =
306                         childOp.normalize(childNodeType, nodeChild);
307                     if (childNode != null)
308                         builder.addChild(childNode);
309                 }
310             }
311
312
313             try {
314                 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
315             } catch (Exception e) {
316                 return null;
317             }
318
319         }
320
321         private void logNode(Node node) {
322             //let us find out the type of the node
323             logger.debug("We got a {} , with identifier {} with {} children",
324                 node.getType(), node.getPath(),
325                 node.getChildList());
326         }
327
328         @SuppressWarnings("rawtypes")
329         protected abstract NormalizedNodeContainerBuilder createBuilder(
330             final Node node);
331
332     }
333
334
335     private static abstract class DataContainerNormalizationOperation<T extends PathArgument>
336         extends NodeToNormalizationNodeOperation<T> {
337
338         private final DataNodeContainer schema;
339         private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
340         private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
341
342         protected DataContainerNormalizationOperation(final T identifier,
343             final DataNodeContainer schema) {
344             super(identifier);
345             this.schema = schema;
346             this.byArg = new ConcurrentHashMap<>();
347             this.byQName = new ConcurrentHashMap<>();
348         }
349
350         @Override
351         public NodeToNormalizedNodeBuilder<?> getChild(
352             final PathArgument child) {
353             NodeToNormalizedNodeBuilder<?> potential = byArg.get(child);
354             if (potential != null) {
355                 return potential;
356             }
357             potential = fromSchema(schema, child);
358             return register(potential);
359         }
360
361         @Override
362         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
363             if (child == null) {
364                 return null;
365             }
366
367             NodeToNormalizedNodeBuilder<?> potential = byQName.get(child);
368             if (potential != null) {
369                 return potential;
370             }
371             potential = fromSchemaAndPathArgument(schema, child);
372             return register(potential);
373         }
374
375         private NodeToNormalizedNodeBuilder<?> register(
376             final NodeToNormalizedNodeBuilder<?> potential) {
377             if (potential != null) {
378                 byArg.put(potential.getIdentifier(), potential);
379                 for (QName qName : potential.getQNameIdentifiers()) {
380                     byQName.put(qName, potential);
381                 }
382             }
383             return potential;
384         }
385
386     }
387
388
389     private static final class ListItemNormalization extends
390         DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
391
392         private final List<QName> keyDefinition;
393         private final ListSchemaNode schemaNode;
394
395         protected ListItemNormalization(
396             final NodeIdentifierWithPredicates identifier,
397             final ListSchemaNode schema) {
398             super(identifier, schema);
399             this.schemaNode = schema;
400             keyDefinition = schema.getKeyDefinition();
401         }
402
403         @Override
404         protected NormalizedNodeContainerBuilder createBuilder(
405             final Node node) {
406             NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
407                 (NodeIdentifierWithPredicates) NodeIdentifierFactory
408                     .createPathArgument(node
409                         .getPath(), schemaNode);
410             return Builders.mapEntryBuilder()
411                 .withNodeIdentifier(
412                     nodeIdentifierWithPredicates
413             );
414         }
415
416         @Override
417         public NormalizedNode<?, ?> createDefault(
418             final PathArgument currentArg) {
419             DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode>
420                 builder =
421                 Builders.mapEntryBuilder().withNodeIdentifier(
422                     (NodeIdentifierWithPredicates) currentArg);
423             for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg)
424                 .getKeyValues().entrySet()) {
425                 if (keyValue.getValue() == null) {
426                     throw new NullPointerException(
427                         "Null value found for path : "
428                             + currentArg);
429                 }
430                 builder.addChild(Builders.leafBuilder()
431                     //
432                     .withNodeIdentifier(new NodeIdentifier(keyValue.getKey()))
433                     .withValue(keyValue.getValue()).build());
434             }
435             return builder.build();
436         }
437
438
439         @Override
440         public boolean isKeyedEntry() {
441             return true;
442         }
443     }
444
445
446     private static final class ContainerNormalization extends
447         DataContainerNormalizationOperation<NodeIdentifier> {
448
449         protected ContainerNormalization(final ContainerSchemaNode schema) {
450             super(new NodeIdentifier(schema.getQName()), schema);
451         }
452
453         @Override
454         protected NormalizedNodeContainerBuilder createBuilder(
455             final Node node) {
456             return Builders.containerBuilder()
457                 .withNodeIdentifier(getIdentifier());
458         }
459
460         @Override
461         public NormalizedNode<?, ?> createDefault(
462             final PathArgument currentArg) {
463             return Builders.containerBuilder()
464                 .withNodeIdentifier((NodeIdentifier) currentArg).build();
465         }
466
467     }
468
469
470     private static abstract class MixinNormalizationOp<T extends PathArgument>
471         extends NodeToNormalizationNodeOperation<T> {
472
473         protected MixinNormalizationOp(final T identifier) {
474             super(identifier);
475         }
476
477         @Override
478         public final boolean isMixin() {
479             return true;
480         }
481
482     }
483
484
485     private static final class LeafListMixinNormalization extends
486         MixinNormalizationOp<NodeIdentifier> {
487
488         private final NodeToNormalizedNodeBuilder<?> innerOp;
489
490         public LeafListMixinNormalization(final LeafListSchemaNode potential) {
491             super(new NodeIdentifier(potential.getQName()));
492             innerOp = new LeafListEntryNormalization(potential);
493         }
494
495         @Override
496         protected NormalizedNodeContainerBuilder createBuilder(
497             final Node node) {
498             return Builders.leafSetBuilder()
499                 .withNodeIdentifier(getIdentifier());
500         }
501
502         @Override
503         public NormalizedNode<?, ?> createDefault(
504             final PathArgument currentArg) {
505             return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier())
506                 .build();
507         }
508
509         @Override
510         public NodeToNormalizedNodeBuilder<?> getChild(
511             final PathArgument child) {
512             if (child instanceof NodeWithValue) {
513                 return innerOp;
514             }
515             return null;
516         }
517
518         @Override
519         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
520             if (getIdentifier().getNodeType().equals(child)) {
521                 return innerOp;
522             }
523             return null;
524         }
525
526     }
527
528
529     private static final class AugmentationNormalization extends
530         MixinNormalizationOp<AugmentationIdentifier> {
531
532         private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
533         private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
534
535         public AugmentationNormalization(final AugmentationSchema augmentation,
536             final DataNodeContainer schema) {
537             super(augmentationIdentifierFrom(augmentation));
538
539             ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>>
540                 byQNameBuilder =
541                 ImmutableMap.builder();
542             ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>>
543                 byArgBuilder =
544                 ImmutableMap.builder();
545
546             for (DataSchemaNode augNode : augmentation.getChildNodes()) {
547                 DataSchemaNode resolvedNode =
548                     schema.getDataChildByName(augNode.getQName());
549                 NodeToNormalizedNodeBuilder<?> resolvedOp =
550                     fromDataSchemaNode(resolvedNode);
551                 byArgBuilder.put(resolvedOp.getIdentifier(), resolvedOp);
552                 for (QName resQName : resolvedOp.getQNameIdentifiers()) {
553                     byQNameBuilder.put(resQName, resolvedOp);
554                 }
555             }
556             byQName = byQNameBuilder.build();
557             byArg = byArgBuilder.build();
558
559         }
560
561         @Override
562         public NodeToNormalizedNodeBuilder<?> getChild(
563             final PathArgument child) {
564             return byArg.get(child);
565         }
566
567         @Override
568         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
569             return byQName.get(child);
570         }
571
572         @Override
573         protected Set<QName> getQNameIdentifiers() {
574             return getIdentifier().getPossibleChildNames();
575         }
576
577         @SuppressWarnings("rawtypes")
578         @Override
579         protected NormalizedNodeContainerBuilder createBuilder(
580             final Node node) {
581             return Builders.augmentationBuilder()
582                 .withNodeIdentifier(getIdentifier());
583         }
584
585         @Override
586         public NormalizedNode<?, ?> createDefault(
587             final PathArgument currentArg) {
588             return Builders.augmentationBuilder()
589                 .withNodeIdentifier(getIdentifier())
590                 .build();
591         }
592
593     }
594
595
596     private static final class ListMixinNormalization extends
597         MixinNormalizationOp<NodeIdentifier> {
598
599         private final ListItemNormalization innerNode;
600
601         public ListMixinNormalization(final ListSchemaNode list) {
602             super(new NodeIdentifier(list.getQName()));
603             this.innerNode =
604                 new ListItemNormalization(new NodeIdentifierWithPredicates(
605                     list.getQName(), Collections.<QName, Object>emptyMap()),
606                     list);
607         }
608
609         @SuppressWarnings("rawtypes")
610         @Override
611         protected NormalizedNodeContainerBuilder createBuilder(
612             final Node node) {
613             return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
614         }
615
616         @Override
617         public NormalizedNode<?, ?> createDefault(
618             final PathArgument currentArg) {
619             return Builders.mapBuilder().withNodeIdentifier(getIdentifier())
620                 .build();
621         }
622
623         @Override
624         public NodeToNormalizedNodeBuilder<?> getChild(
625             final PathArgument child) {
626             if (child.getNodeType().equals(getIdentifier().getNodeType())) {
627                 return innerNode;
628             }
629             return null;
630         }
631
632         @Override
633         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
634             if (getIdentifier().getNodeType().equals(child)) {
635                 return innerNode;
636             }
637             return null;
638         }
639
640     }
641
642
643     private static class ChoiceNodeNormalization extends
644         MixinNormalizationOp<NodeIdentifier> {
645
646         private final ImmutableMap<QName, NodeToNormalizedNodeBuilder<?>>
647             byQName;
648         private final ImmutableMap<PathArgument, NodeToNormalizedNodeBuilder<?>>
649             byArg;
650
651         protected ChoiceNodeNormalization(
652             final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
653             super(new NodeIdentifier(schema.getQName()));
654             ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>>
655                 byQNameBuilder =
656                 ImmutableMap.builder();
657             ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>>
658                 byArgBuilder =
659                 ImmutableMap.builder();
660
661             for (ChoiceCaseNode caze : schema.getCases()) {
662                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
663                     NodeToNormalizedNodeBuilder<?> childOp =
664                         fromDataSchemaNode(cazeChild);
665                     byArgBuilder.put(childOp.getIdentifier(), childOp);
666                     for (QName qname : childOp.getQNameIdentifiers()) {
667                         byQNameBuilder.put(qname, childOp);
668                     }
669                 }
670             }
671             byQName = byQNameBuilder.build();
672             byArg = byArgBuilder.build();
673         }
674
675         @Override
676         public NodeToNormalizedNodeBuilder<?> getChild(
677             final PathArgument child) {
678             return byArg.get(child);
679         }
680
681         @Override
682         public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
683             return byQName.get(child);
684         }
685
686         @Override
687         protected NormalizedNodeContainerBuilder createBuilder(
688             final Node node) {
689             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
690         }
691
692         @Override
693         public NormalizedNode<?, ?> createDefault(
694             final PathArgument currentArg) {
695             return Builders.choiceBuilder().withNodeIdentifier(getIdentifier())
696                 .build();
697         }
698     }
699
700     /**
701      * Find an appropriate NormalizedNodeBuilder using both the schema and the
702      * Path Argument
703      *
704      * @param schema
705      * @param child
706      * @return
707      */
708     public static NodeToNormalizedNodeBuilder<?> fromSchemaAndPathArgument(
709         final DataNodeContainer schema, final QName child) {
710         DataSchemaNode potential = schema.getDataChildByName(child);
711         if (potential == null) {
712             Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode>
713                 choices =
714                 FluentIterable.from(schema.getChildNodes()).filter(
715                     org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
716             potential = findChoice(choices, child);
717         }
718         if (potential == null) {
719             if (logger.isTraceEnabled()) {
720                 logger.trace("BAD CHILD = {}", child.toString());
721             }
722         }
723
724         checkArgument(potential != null,
725             "Supplied QName %s is not valid according to schema %s", child,
726             schema);
727
728         // If the schema in an instance of DataSchemaNode and the potential
729         // is augmenting something then there is a chance that this may be
730         // and augmentation node
731         if ((schema instanceof DataSchemaNode)
732             && potential.isAugmenting()) {
733
734             AugmentationNormalization augmentation =
735                 fromAugmentation(schema, (AugmentationTarget) schema,
736                     potential);
737
738             // If an augmentation normalization (builder) is not found then
739             // we fall through to the regular processing
740             if(augmentation != null){
741                 return augmentation;
742             }
743         }
744         return fromDataSchemaNode(potential);
745     }
746
747     /**
748      * Given a bunch of choice nodes and a the name of child find a choice node for that child which
749      * has a non-null value
750      *
751      * @param choices
752      * @param child
753      * @return
754      */
755     private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
756         final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices,
757         final QName child) {
758         org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
759         choiceLoop:
760         for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
761             for (ChoiceCaseNode caze : choice.getCases()) {
762                 if (caze.getDataChildByName(child) != null) {
763                     foundChoice = choice;
764                     break choiceLoop;
765                 }
766             }
767         }
768         return foundChoice;
769     }
770
771
772     /**
773      * Create an AugmentationIdentifier based on the AugmentationSchema
774      *
775      * @param augmentation
776      * @return
777      */
778     public static AugmentationIdentifier augmentationIdentifierFrom(
779         final AugmentationSchema augmentation) {
780         ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
781         for (DataSchemaNode child : augmentation.getChildNodes()) {
782             potentialChildren.add(child.getQName());
783         }
784         return new AugmentationIdentifier(potentialChildren.build());
785     }
786
787     /**
788      * Create an AugmentationNormalization based on the schema of the DataContainer, the
789      * AugmentationTarget and the potential schema node
790      *
791      * @param schema
792      * @param augments
793      * @param potential
794      * @return
795      */
796     private static AugmentationNormalization fromAugmentation(
797         final DataNodeContainer schema, final AugmentationTarget augments,
798         final DataSchemaNode potential) {
799         AugmentationSchema augmentation = null;
800         for (AugmentationSchema aug : augments.getAvailableAugmentations()) {
801             DataSchemaNode child = aug.getDataChildByName(potential.getQName());
802             if (child != null) {
803                 augmentation = aug;
804                 break;
805             }
806
807         }
808         if (augmentation != null) {
809             return new AugmentationNormalization(augmentation, schema);
810         } else {
811             return null;
812         }
813     }
814
815     /**
816      * @param schema
817      * @param child
818      * @return
819      */
820     private static NodeToNormalizedNodeBuilder<?> fromSchema(
821         final DataNodeContainer schema, final PathArgument child) {
822         if (child instanceof AugmentationIdentifier) {
823             QName childQName = ((AugmentationIdentifier) child)
824                 .getPossibleChildNames().iterator().next();
825
826             return fromSchemaAndPathArgument(schema, childQName);
827         }
828         return fromSchemaAndPathArgument(schema, child.getNodeType());
829     }
830
831     public static NodeToNormalizedNodeBuilder<?> fromDataSchemaNode(
832         final DataSchemaNode potential) {
833         if (potential instanceof ContainerSchemaNode) {
834             return new ContainerNormalization((ContainerSchemaNode) potential);
835         } else if (potential instanceof ListSchemaNode) {
836             return new ListMixinNormalization((ListSchemaNode) potential);
837         } else if (potential instanceof LeafSchemaNode) {
838             return new LeafNormalization((LeafSchemaNode) potential,
839                 new NodeIdentifier(potential.getQName()));
840         } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
841             return new ChoiceNodeNormalization(
842                 (org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
843         } else if (potential instanceof LeafListSchemaNode) {
844             return new LeafListMixinNormalization(
845                 (LeafListSchemaNode) potential);
846         }
847         return null;
848     }
849
850     public static NodeToNormalizedNodeBuilder<?> from(final SchemaContext ctx) {
851         return new ContainerNormalization(ctx);
852     }
853
854     public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);
855
856 }