Switch to using protocol buffer serialization for the WriteData message
[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.InstanceIdentifier.AugmentationIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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       if (!node.getType().equals(AugmentationNode.class.getSimpleName())) {
197         checkArgument(nodeType != null);
198       }
199
200       NormalizedNodeContainerBuilder builder = createBuilder(node);
201
202       Set<NodeToNormalizedNodeBuilder<?>> usedMixins = new HashSet<>();
203
204       logNode(node);
205
206       if (node.getChildCount() == 0 && !node.getType().equals(ContainerNode.class.getSimpleName())) {
207         PathArgument childPathArgument =
208             NodeIdentifierFactory.getArgument(node.getPath());
209         NormalizedNode child = null;
210         if (childPathArgument instanceof NodeWithValue) {
211           final NodeWithValue nodeWithValue =
212               new NodeWithValue(childPathArgument.getNodeType(),
213                   node.getValue());
214           child =
215               Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue)
216                   .withValue(node.getValue()).build();
217         } else {
218           child =
219               ImmutableNodes.leafNode(childPathArgument.getNodeType(),
220                   node.getValue());
221         }
222         builder.addChild(child);
223       }
224
225       final List<Node> children = node.getChildList();
226       for (Node nodeChild : children) {
227
228         PathArgument childPathArgument =
229             NodeIdentifierFactory.getArgument(nodeChild.getPath());
230
231         QName childNodeType = null;
232         NodeToNormalizedNodeBuilder childOp = null;
233
234
235         if (childPathArgument instanceof AugmentationIdentifier) {
236           childOp = getChild(childPathArgument);
237         } else {
238           childNodeType = childPathArgument.getNodeType();
239           childOp = getChild(childNodeType);
240         }
241         // We skip unknown nodes if this node is mixin since
242         // it's nodes and parent nodes are interleaved
243         if (childOp == null && isMixin()) {
244           continue;
245         } else if (childOp == null) {
246           logger.error(
247               "childOp is null and this operation is not a mixin : this = {}",
248               this.toString());
249         }
250
251         checkArgument(childOp != null, "Node %s is not allowed inside %s",
252             childNodeType, getIdentifier());
253         if (childOp.isMixin()) {
254           if (usedMixins.contains(childOp)) {
255             // We already run / processed that mixin, so to avoid
256             // duplicate we are
257             // skiping next nodes.
258             continue;
259           }
260           // builder.addChild(childOp.normalize(nodeType, treeCacheNode));
261           final NormalizedNode childNode =
262               childOp.normalize(childNodeType, nodeChild);
263           if (childNode != null)
264             builder.addChild(childNode);
265           usedMixins.add(childOp);
266         } else {
267           final NormalizedNode childNode =
268               childOp.normalize(childNodeType, nodeChild);
269           if (childNode != null)
270             builder.addChild(childNode);
271         }
272       }
273
274
275       try {
276         return (NormalizedNodeContainer<?, ?, ?>) builder.build();
277       } catch (Exception e) {
278         return null;
279       }
280
281     }
282
283     private void logNode(Node node) {
284       //let us find out the type of the node
285       logger.debug("We got a {} , with identifier {} with {} children", node.getType(),node.getPath(),
286                    node.getChildList());
287     }
288
289     @SuppressWarnings("rawtypes")
290     protected abstract NormalizedNodeContainerBuilder createBuilder(
291         final Node node);
292
293   }
294
295   private static abstract class DataContainerNormalizationOperation<T extends PathArgument>
296       extends NodeToNormalizationNodeOperation<T> {
297
298     private final DataNodeContainer schema;
299     private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
300     private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
301
302     protected DataContainerNormalizationOperation(final T identifier,
303         final DataNodeContainer schema) {
304       super(identifier);
305       this.schema = schema;
306       this.byArg = new ConcurrentHashMap<>();
307       this.byQName = new ConcurrentHashMap<>();
308     }
309
310     @Override
311     public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
312       NodeToNormalizedNodeBuilder<?> potential = byArg.get(child);
313       if (potential != null) {
314         return potential;
315       }
316       potential = fromSchema(schema, child);
317       return register(potential);
318     }
319
320     @Override
321     public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
322       if (child == null) {
323         return null;
324       }
325
326       NodeToNormalizedNodeBuilder<?> potential = byQName.get(child);
327       if (potential != null) {
328         return potential;
329       }
330       potential = fromSchemaAndPathArgument(schema, child);
331       return register(potential);
332     }
333
334     private NodeToNormalizedNodeBuilder<?> register(
335         final NodeToNormalizedNodeBuilder<?> potential) {
336       if (potential != null) {
337         byArg.put(potential.getIdentifier(), potential);
338         for (QName qName : potential.getQNameIdentifiers()) {
339           byQName.put(qName, potential);
340         }
341       }
342       return potential;
343     }
344
345   }
346
347   private static final class ListItemNormalization extends
348       DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
349
350     private final List<QName> keyDefinition;
351     private final ListSchemaNode schemaNode;
352
353     protected ListItemNormalization(
354         final NodeIdentifierWithPredicates identifier,
355         final ListSchemaNode schema) {
356       super(identifier, schema);
357       this.schemaNode = schema;
358       keyDefinition = schema.getKeyDefinition();
359     }
360
361     @Override
362     protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
363       return Builders.mapEntryBuilder().withNodeIdentifier(
364           (NodeIdentifierWithPredicates) NodeIdentifierFactory.getArgument(node
365               .getPath()));
366     }
367
368     @Override
369     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
370       DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> builder =
371           Builders.mapEntryBuilder().withNodeIdentifier(
372               (NodeIdentifierWithPredicates) currentArg);
373       for (Entry<QName, Object> keyValue : ((NodeIdentifierWithPredicates) currentArg)
374           .getKeyValues().entrySet()) {
375         if (keyValue.getValue() == null) {
376           throw new NullPointerException("Null value found for path : "
377               + currentArg);
378         }
379         builder.addChild(Builders.leafBuilder()
380             //
381             .withNodeIdentifier(new NodeIdentifier(keyValue.getKey()))
382             .withValue(keyValue.getValue()).build());
383       }
384       return builder.build();
385     }
386
387
388     @Override
389     public boolean isKeyedEntry() {
390       return true;
391     }
392   }
393
394   private static final class ContainerNormalization extends
395       DataContainerNormalizationOperation<NodeIdentifier> {
396
397     protected ContainerNormalization(final ContainerSchemaNode schema) {
398       super(new NodeIdentifier(schema.getQName()), schema);
399     }
400
401     @Override
402     protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
403       return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
404     }
405
406     @Override
407     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
408       return Builders.containerBuilder()
409           .withNodeIdentifier((NodeIdentifier) currentArg).build();
410     }
411
412   }
413
414   private static abstract class MixinNormalizationOp<T extends PathArgument>
415       extends NodeToNormalizationNodeOperation<T> {
416
417     protected MixinNormalizationOp(final T identifier) {
418       super(identifier);
419     }
420
421     @Override
422     public final boolean isMixin() {
423       return true;
424     }
425
426   }
427
428   private static final class LeafListMixinNormalization extends
429       MixinNormalizationOp<NodeIdentifier> {
430
431     private final NodeToNormalizedNodeBuilder<?> innerOp;
432
433     public LeafListMixinNormalization(final LeafListSchemaNode potential) {
434       super(new NodeIdentifier(potential.getQName()));
435       innerOp = new LeafListEntryNormalization(potential);
436     }
437
438     @Override
439     protected NormalizedNodeContainerBuilder createBuilder(
440         final Node node) {
441       return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
442     }
443
444     @Override
445     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
446       return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier())
447           .build();
448     }
449
450     @Override
451     public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
452       if (child instanceof NodeWithValue) {
453         return innerOp;
454       }
455       return null;
456     }
457
458     @Override
459     public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
460       if (getIdentifier().getNodeType().equals(child)) {
461         return innerOp;
462       }
463       return null;
464     }
465
466   }
467
468   private static final class AugmentationNormalization extends
469       MixinNormalizationOp<AugmentationIdentifier> {
470
471     private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
472     private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
473
474     public AugmentationNormalization(final AugmentationSchema augmentation,
475         final DataNodeContainer schema) {
476       super(augmentationIdentifierFrom(augmentation));
477
478       ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>> byQNameBuilder =
479           ImmutableMap.builder();
480       ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>> byArgBuilder =
481           ImmutableMap.builder();
482
483       for (DataSchemaNode augNode : augmentation.getChildNodes()) {
484         DataSchemaNode resolvedNode =
485             schema.getDataChildByName(augNode.getQName());
486         NodeToNormalizedNodeBuilder<?> resolvedOp =
487             fromDataSchemaNode(resolvedNode);
488         byArgBuilder.put(resolvedOp.getIdentifier(), resolvedOp);
489         for (QName resQName : resolvedOp.getQNameIdentifiers()) {
490           byQNameBuilder.put(resQName, resolvedOp);
491         }
492       }
493       byQName = byQNameBuilder.build();
494       byArg = byArgBuilder.build();
495
496     }
497
498     @Override
499     public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
500       return byArg.get(child);
501     }
502
503     @Override
504     public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
505       return byQName.get(child);
506     }
507
508     @Override
509     protected Set<QName> getQNameIdentifiers() {
510       return getIdentifier().getPossibleChildNames();
511     }
512
513     @SuppressWarnings("rawtypes")
514     @Override
515     protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
516       return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
517     }
518
519     @Override
520     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
521       return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier())
522           .build();
523     }
524
525   }
526
527   private static final class ListMixinNormalization extends
528       MixinNormalizationOp<NodeIdentifier> {
529
530     private final ListItemNormalization innerNode;
531
532     public ListMixinNormalization(final ListSchemaNode list) {
533       super(new NodeIdentifier(list.getQName()));
534       this.innerNode =
535           new ListItemNormalization(new NodeIdentifierWithPredicates(
536               list.getQName(), Collections.<QName, Object>emptyMap()), list);
537     }
538
539     @SuppressWarnings("rawtypes")
540     @Override
541     protected NormalizedNodeContainerBuilder createBuilder(
542         final Node node) {
543       return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
544     }
545
546     @Override
547     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
548       return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
549     }
550
551     @Override
552     public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
553       if (child.getNodeType().equals(getIdentifier().getNodeType())) {
554         return innerNode;
555       }
556       return null;
557     }
558
559     @Override
560     public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
561       if (getIdentifier().getNodeType().equals(child)) {
562         return innerNode;
563       }
564       return null;
565     }
566
567   }
568
569   private static class ChoiceNodeNormalization extends
570       MixinNormalizationOp<NodeIdentifier> {
571
572     private final ImmutableMap<QName, NodeToNormalizedNodeBuilder<?>> byQName;
573     private final ImmutableMap<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
574
575     protected ChoiceNodeNormalization(
576         final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
577       super(new NodeIdentifier(schema.getQName()));
578       ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>> byQNameBuilder =
579           ImmutableMap.builder();
580       ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>> byArgBuilder =
581           ImmutableMap.builder();
582
583       for (ChoiceCaseNode caze : schema.getCases()) {
584         for (DataSchemaNode cazeChild : caze.getChildNodes()) {
585           NodeToNormalizedNodeBuilder<?> childOp =
586               fromDataSchemaNode(cazeChild);
587           byArgBuilder.put(childOp.getIdentifier(), childOp);
588           for (QName qname : childOp.getQNameIdentifiers()) {
589             byQNameBuilder.put(qname, childOp);
590           }
591         }
592       }
593       byQName = byQNameBuilder.build();
594       byArg = byArgBuilder.build();
595     }
596
597     @Override
598     public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
599       return byArg.get(child);
600     }
601
602     @Override
603     public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
604       return byQName.get(child);
605     }
606
607     @Override
608     protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
609       return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
610     }
611
612     @Override
613     public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
614       return Builders.choiceBuilder().withNodeIdentifier(getIdentifier())
615           .build();
616     }
617   }
618
619   public static NodeToNormalizedNodeBuilder<?> fromSchemaAndPathArgument(
620       final DataNodeContainer schema, final QName child) {
621     DataSchemaNode potential = schema.getDataChildByName(child);
622     if (potential == null) {
623       Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices =
624           FluentIterable.from(schema.getChildNodes()).filter(
625               org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
626       potential = findChoice(choices, child);
627     }
628     if (potential == null) {
629       if (logger.isTraceEnabled()) {
630         logger.trace("BAD CHILD = {}", child.toString());
631       }
632     }
633
634     checkArgument(potential != null,
635         "Supplied QName %s is not valid according to schema %s", child, schema);
636     if ((schema instanceof DataSchemaNode)
637         && !((DataSchemaNode) schema).isAugmenting()
638         && potential.isAugmenting()) {
639       return fromAugmentation(schema, (AugmentationTarget) schema, potential);
640     }
641     return fromDataSchemaNode(potential);
642   }
643
644   /**
645    * Given a bunch of choice nodes and a the name of child find a choice node for that child which
646    * has a non-null value
647    *
648    * @param choices
649    * @param child
650    * @return
651    */
652   private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
653       final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices,
654       final QName child) {
655     org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
656     choiceLoop: for (org.opendaylight.yangtools.yang.model.api.ChoiceNode choice : choices) {
657       for (ChoiceCaseNode caze : choice.getCases()) {
658         if (caze.getDataChildByName(child) != null) {
659           foundChoice = choice;
660           break choiceLoop;
661         }
662       }
663     }
664     return foundChoice;
665   }
666
667
668   /**
669    * Create an AugmentationIdentifier based on the AugmentationSchema
670    *
671    * @param augmentation
672    * @return
673    */
674   public static AugmentationIdentifier augmentationIdentifierFrom(
675       final AugmentationSchema augmentation) {
676     ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
677     for (DataSchemaNode child : augmentation.getChildNodes()) {
678       potentialChildren.add(child.getQName());
679     }
680     return new AugmentationIdentifier(null, potentialChildren.build());
681   }
682
683   /**
684    * Create an AugmentationNormalization based on the schema of the DataContainer, the
685    * AugmentationTarget and the potential schema node
686    *
687    * @param schema
688    * @param augments
689    * @param potential
690    * @return
691    */
692   private static AugmentationNormalization fromAugmentation(
693       final DataNodeContainer schema, final AugmentationTarget augments,
694       final DataSchemaNode potential) {
695     AugmentationSchema augmentation = null;
696     for (AugmentationSchema aug : augments.getAvailableAugmentations()) {
697       DataSchemaNode child = aug.getDataChildByName(potential.getQName());
698       if (child != null) {
699         augmentation = aug;
700         break;
701       }
702
703     }
704     if (augmentation != null) {
705       return new AugmentationNormalization(augmentation, schema);
706     } else {
707       return null;
708     }
709   }
710
711   /**
712    *
713    * @param schema
714    * @param child
715    * @return
716    */
717   private static NodeToNormalizedNodeBuilder<?> fromSchema(
718       final DataNodeContainer schema, final PathArgument child) {
719     if (child instanceof AugmentationIdentifier) {
720       return fromSchemaAndPathArgument(schema, ((AugmentationIdentifier) child)
721           .getPossibleChildNames().iterator().next());
722     }
723     return fromSchemaAndPathArgument(schema, child.getNodeType());
724   }
725
726   public static NodeToNormalizedNodeBuilder<?> fromDataSchemaNode(
727       final DataSchemaNode potential) {
728     if (potential instanceof ContainerSchemaNode) {
729       return new ContainerNormalization((ContainerSchemaNode) potential);
730     } else if (potential instanceof ListSchemaNode) {
731       return new ListMixinNormalization((ListSchemaNode) potential);
732     } else if (potential instanceof LeafSchemaNode) {
733       return new LeafNormalization(new NodeIdentifier(potential.getQName()));
734     } else if (potential instanceof org.opendaylight.yangtools.yang.model.api.ChoiceNode) {
735       return new ChoiceNodeNormalization(
736           (org.opendaylight.yangtools.yang.model.api.ChoiceNode) potential);
737     } else if (potential instanceof LeafListSchemaNode) {
738       return new LeafListMixinNormalization((LeafListSchemaNode) potential);
739     }
740     return null;
741   }
742
743   public static NodeToNormalizedNodeBuilder<?> from(final SchemaContext ctx) {
744     return new ContainerNormalization(ctx);
745   }
746
747   public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);
748
749 }