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