1 package org.opendaylight.controller.cluster.datastore.node;
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;
38 import java.util.Collections;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.Map.Entry;
44 import java.util.concurrent.ConcurrentHashMap;
46 import static com.google.common.base.Preconditions.checkArgument;
49 * NormalizedNodeBuilder is a builder that walks through a tree like structure and constructs a
50 * NormalizedNode from it.
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
57 public abstract class NodeToNormalizedNodeBuilder<T extends PathArgument>
58 implements Identifiable<T> {
60 private final T identifier;
62 protected static final Logger logger = LoggerFactory
63 .getLogger(NodeToNormalizedNodeBuilder.class);
66 public T getIdentifier() {
70 protected NodeToNormalizedNodeBuilder(final T identifier) {
72 this.identifier = identifier;
78 * @return Should return true if the node that this operation corresponds to is a mixin
80 public boolean isMixin() {
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
90 public boolean isKeyedEntry() {
94 protected Set<QName> getQNameIdentifiers() {
95 return Collections.singleton(identifier.getNodeType());
98 public abstract NodeToNormalizedNodeBuilder<?> getChild(
99 final PathArgument child);
101 public abstract NodeToNormalizedNodeBuilder<?> getChild(QName child);
103 public abstract NormalizedNode<?, ?> normalize(QName nodeType, Node node);
107 private static abstract class SimpleTypeNormalization<T extends PathArgument>
108 extends NodeToNormalizedNodeBuilder<T> {
110 protected SimpleTypeNormalization(final T identifier) {
115 public NormalizedNode<?, ?> normalize(final QName nodeType, final Node node) {
116 checkArgument(node != null);
117 return normalizeImpl(nodeType, node);
120 protected abstract NormalizedNode<?, ?> normalizeImpl(QName nodeType,
124 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
129 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
134 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
135 // TODO Auto-generated method stub
141 private static final class LeafNormalization extends
142 SimpleTypeNormalization<NodeIdentifier> {
144 protected LeafNormalization(final NodeIdentifier identifier) {
149 protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
151 return ImmutableNodes.leafNode(nodeType, node.getValue());
157 private static final class LeafListEntryNormalization extends
158 SimpleTypeNormalization<NodeWithValue> {
160 public LeafListEntryNormalization(final LeafListSchemaNode potential) {
161 super(new NodeWithValue(potential.getQName(), null));
165 protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
167 final Object data = node.getValue();
169 Preconditions.checkArgument(false,
170 "No data available in leaf list entry for " + nodeType);
172 NodeWithValue nodeId = new NodeWithValue(nodeType, data);
173 return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId)
174 .withValue(data).build();
179 public boolean isKeyedEntry() {
184 private static abstract class NodeToNormalizationNodeOperation<T extends PathArgument>
185 extends NodeToNormalizedNodeBuilder<T> {
187 protected NodeToNormalizationNodeOperation(final T identifier) {
191 @SuppressWarnings({"rawtypes", "unchecked"})
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);
200 NormalizedNodeContainerBuilder builder = createBuilder(node);
202 Set<NodeToNormalizedNodeBuilder<?>> usedMixins = new HashSet<>();
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(),
215 Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue)
216 .withValue(node.getValue()).build();
219 ImmutableNodes.leafNode(childPathArgument.getNodeType(),
222 builder.addChild(child);
225 final List<Node> children = node.getChildList();
226 for (Node nodeChild : children) {
228 PathArgument childPathArgument =
229 NodeIdentifierFactory.getArgument(nodeChild.getPath());
231 QName childNodeType = null;
232 NodeToNormalizedNodeBuilder childOp = null;
235 if (childPathArgument instanceof AugmentationIdentifier) {
236 childOp = getChild(childPathArgument);
238 childNodeType = childPathArgument.getNodeType();
239 childOp = getChild(childNodeType);
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()) {
245 } else if (childOp == null) {
247 "childOp is null and this operation is not a mixin : this = {}",
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
257 // skiping next nodes.
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);
267 final NormalizedNode childNode =
268 childOp.normalize(childNodeType, nodeChild);
269 if (childNode != null)
270 builder.addChild(childNode);
276 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
277 } catch (Exception e) {
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());
289 @SuppressWarnings("rawtypes")
290 protected abstract NormalizedNodeContainerBuilder createBuilder(
295 private static abstract class DataContainerNormalizationOperation<T extends PathArgument>
296 extends NodeToNormalizationNodeOperation<T> {
298 private final DataNodeContainer schema;
299 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
300 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
302 protected DataContainerNormalizationOperation(final T identifier,
303 final DataNodeContainer schema) {
305 this.schema = schema;
306 this.byArg = new ConcurrentHashMap<>();
307 this.byQName = new ConcurrentHashMap<>();
311 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
312 NodeToNormalizedNodeBuilder<?> potential = byArg.get(child);
313 if (potential != null) {
316 potential = fromSchema(schema, child);
317 return register(potential);
321 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
326 NodeToNormalizedNodeBuilder<?> potential = byQName.get(child);
327 if (potential != null) {
330 potential = fromSchemaAndPathArgument(schema, child);
331 return register(potential);
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);
347 private static final class ListItemNormalization extends
348 DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
350 private final List<QName> keyDefinition;
351 private final ListSchemaNode schemaNode;
353 protected ListItemNormalization(
354 final NodeIdentifierWithPredicates identifier,
355 final ListSchemaNode schema) {
356 super(identifier, schema);
357 this.schemaNode = schema;
358 keyDefinition = schema.getKeyDefinition();
362 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
363 return Builders.mapEntryBuilder().withNodeIdentifier(
364 (NodeIdentifierWithPredicates) NodeIdentifierFactory.getArgument(node
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 : "
379 builder.addChild(Builders.leafBuilder()
381 .withNodeIdentifier(new NodeIdentifier(keyValue.getKey()))
382 .withValue(keyValue.getValue()).build());
384 return builder.build();
389 public boolean isKeyedEntry() {
394 private static final class ContainerNormalization extends
395 DataContainerNormalizationOperation<NodeIdentifier> {
397 protected ContainerNormalization(final ContainerSchemaNode schema) {
398 super(new NodeIdentifier(schema.getQName()), schema);
402 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
403 return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
407 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
408 return Builders.containerBuilder()
409 .withNodeIdentifier((NodeIdentifier) currentArg).build();
414 private static abstract class MixinNormalizationOp<T extends PathArgument>
415 extends NodeToNormalizationNodeOperation<T> {
417 protected MixinNormalizationOp(final T identifier) {
422 public final boolean isMixin() {
428 private static final class LeafListMixinNormalization extends
429 MixinNormalizationOp<NodeIdentifier> {
431 private final NodeToNormalizedNodeBuilder<?> innerOp;
433 public LeafListMixinNormalization(final LeafListSchemaNode potential) {
434 super(new NodeIdentifier(potential.getQName()));
435 innerOp = new LeafListEntryNormalization(potential);
439 protected NormalizedNodeContainerBuilder createBuilder(
441 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
445 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
446 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier())
451 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
452 if (child instanceof NodeWithValue) {
459 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
460 if (getIdentifier().getNodeType().equals(child)) {
468 private static final class AugmentationNormalization extends
469 MixinNormalizationOp<AugmentationIdentifier> {
471 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
472 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
474 public AugmentationNormalization(final AugmentationSchema augmentation,
475 final DataNodeContainer schema) {
476 super(augmentationIdentifierFrom(augmentation));
478 ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>> byQNameBuilder =
479 ImmutableMap.builder();
480 ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>> byArgBuilder =
481 ImmutableMap.builder();
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);
493 byQName = byQNameBuilder.build();
494 byArg = byArgBuilder.build();
499 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
500 return byArg.get(child);
504 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
505 return byQName.get(child);
509 protected Set<QName> getQNameIdentifiers() {
510 return getIdentifier().getPossibleChildNames();
513 @SuppressWarnings("rawtypes")
515 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
516 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
520 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
521 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier())
527 private static final class ListMixinNormalization extends
528 MixinNormalizationOp<NodeIdentifier> {
530 private final ListItemNormalization innerNode;
532 public ListMixinNormalization(final ListSchemaNode list) {
533 super(new NodeIdentifier(list.getQName()));
535 new ListItemNormalization(new NodeIdentifierWithPredicates(
536 list.getQName(), Collections.<QName, Object>emptyMap()), list);
539 @SuppressWarnings("rawtypes")
541 protected NormalizedNodeContainerBuilder createBuilder(
543 return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
547 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
548 return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
552 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
553 if (child.getNodeType().equals(getIdentifier().getNodeType())) {
560 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
561 if (getIdentifier().getNodeType().equals(child)) {
569 private static class ChoiceNodeNormalization extends
570 MixinNormalizationOp<NodeIdentifier> {
572 private final ImmutableMap<QName, NodeToNormalizedNodeBuilder<?>> byQName;
573 private final ImmutableMap<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
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();
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);
593 byQName = byQNameBuilder.build();
594 byArg = byArgBuilder.build();
598 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
599 return byArg.get(child);
603 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
604 return byQName.get(child);
608 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
609 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
613 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
614 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier())
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);
628 if (potential == null) {
629 if (logger.isTraceEnabled()) {
630 logger.trace("BAD CHILD = {}", child.toString());
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);
641 return fromDataSchemaNode(potential);
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
652 private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
653 final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices,
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;
669 * Create an AugmentationIdentifier based on the AugmentationSchema
671 * @param augmentation
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());
680 return new AugmentationIdentifier(null, potentialChildren.build());
684 * Create an AugmentationNormalization based on the schema of the DataContainer, the
685 * AugmentationTarget and the potential schema node
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());
704 if (augmentation != null) {
705 return new AugmentationNormalization(augmentation, schema);
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());
723 return fromSchemaAndPathArgument(schema, child.getNodeType());
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);
743 public static NodeToNormalizedNodeBuilder<?> from(final SchemaContext ctx) {
744 return new ContainerNormalization(ctx);
747 public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);