3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
11 package org.opendaylight.controller.cluster.datastore.node;
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;
51 import java.util.Collections;
52 import java.util.HashSet;
53 import java.util.List;
55 import java.util.Map.Entry;
57 import java.util.concurrent.ConcurrentHashMap;
59 import static com.google.common.base.Preconditions.checkArgument;
62 * NormalizedNodeBuilder is a builder that walks through a tree like structure and constructs a
63 * NormalizedNode from it.
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
70 public abstract class NodeToNormalizedNodeBuilder<T extends PathArgument>
71 implements Identifiable<T> {
73 private final T identifier;
75 protected static final Logger logger = LoggerFactory
76 .getLogger(NodeToNormalizedNodeBuilder.class);
79 public T getIdentifier() {
85 protected NodeToNormalizedNodeBuilder(final T identifier) {
87 this.identifier = identifier;
92 * @return Should return true if the node that this operation corresponds to is a mixin
94 public boolean isMixin() {
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
103 public boolean isKeyedEntry() {
107 protected Set<QName> getQNameIdentifiers() {
108 return Collections.singleton(identifier.getNodeType());
111 public abstract NodeToNormalizedNodeBuilder<?> getChild(
112 final PathArgument child);
114 public abstract NodeToNormalizedNodeBuilder<?> getChild(QName child);
116 public abstract NormalizedNode<?, ?> normalize(QName nodeType, Node node);
120 private static abstract class SimpleTypeNormalization<T extends PathArgument>
121 extends NodeToNormalizedNodeBuilder<T> {
123 protected SimpleTypeNormalization(final T identifier) {
128 public NormalizedNode<?, ?> normalize(final QName nodeType,
130 checkArgument(node != null);
131 return normalizeImpl(nodeType, node);
134 protected abstract NormalizedNode<?, ?> normalizeImpl(QName nodeType,
138 public NodeToNormalizedNodeBuilder<?> getChild(
139 final PathArgument child) {
144 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
149 public NormalizedNode<?, ?> createDefault(
150 final PathArgument currentArg) {
151 // TODO Auto-generated method stub
158 private static final class LeafNormalization extends
159 SimpleTypeNormalization<NodeIdentifier> {
161 private final LeafSchemaNode schema;
163 protected LeafNormalization(final LeafSchemaNode schema, final NodeIdentifier identifier) {
165 this.schema = schema;
169 protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
171 Object value = NodeValueCodec.toTypeSafeValue(this.schema, this.schema.getType(), node);
172 return ImmutableNodes.leafNode(nodeType, value);
179 private static final class LeafListEntryNormalization extends
180 SimpleTypeNormalization<NodeWithValue> {
182 private final LeafListSchemaNode schema;
184 public LeafListEntryNormalization(final LeafListSchemaNode potential) {
185 super(new NodeWithValue(potential.getQName(), null));
186 this.schema = potential;
190 protected NormalizedNode<?, ?> normalizeImpl(final QName nodeType,
192 final Object data = node.getValue();
194 Preconditions.checkArgument(false,
195 "No data available in leaf list entry for " + nodeType);
198 Object value = NodeValueCodec.toTypeSafeValue(this.schema, this.schema.getType(), node);
200 NodeWithValue nodeId = new NodeWithValue(nodeType, value);
201 return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId)
202 .withValue(value).build();
207 public boolean isKeyedEntry() {
213 private static abstract class NodeToNormalizationNodeOperation<T extends PathArgument>
214 extends NodeToNormalizedNodeBuilder<T> {
216 protected NodeToNormalizationNodeOperation(final T identifier) {
220 @SuppressWarnings({"rawtypes", "unchecked"})
222 public final NormalizedNodeContainer<?, ?, ?> normalize(
223 final QName nodeType, final Node node) {
224 checkArgument(node != null);
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);
232 NormalizedNodeContainerBuilder builder = createBuilder(node);
234 Set<NodeToNormalizedNodeBuilder<?>> usedMixins = new HashSet<>();
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());
244 final NormalizedNode child;
245 if (childPathArgument instanceof NodeWithValue) {
246 final NodeWithValue nodeWithValue =
247 new NodeWithValue(childPathArgument.getNodeType(),
250 Builders.leafSetEntryBuilder()
251 .withNodeIdentifier(nodeWithValue)
252 .withValue(node.getValue()).build();
255 ImmutableNodes.leafNode(childPathArgument.getNodeType(),
258 builder.addChild(child);
261 final List<Node> children = node.getChildList();
262 for (Node nodeChild : children) {
264 PathArgument childPathArgument =
265 NodeIdentifierFactory.getArgument(nodeChild.getPath());
267 QName childNodeType = null;
268 NodeToNormalizedNodeBuilder childOp = null;
270 if (childPathArgument instanceof AugmentationIdentifier) {
271 childOp = getChild(childPathArgument);
272 checkArgument(childOp instanceof AugmentationNormalization, childPathArgument);
274 childNodeType = childPathArgument.getNodeType();
275 childOp = getChild(childNodeType);
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()) {
281 } else if (childOp == null) {
283 "childOp is null and this operation is not a mixin : this = {}",
287 checkArgument(childOp != null,
288 "Node %s is not allowed inside %s",
289 childNodeType, getIdentifier());
291 if (childOp.isMixin()) {
292 if (usedMixins.contains(childOp)) {
293 // We already run / processed that mixin, so to avoid
295 // skipping next nodes.
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);
305 final NormalizedNode childNode =
306 childOp.normalize(childNodeType, nodeChild);
307 if (childNode != null)
308 builder.addChild(childNode);
314 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
315 } catch (Exception e) {
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());
328 @SuppressWarnings("rawtypes")
329 protected abstract NormalizedNodeContainerBuilder createBuilder(
335 private static abstract class DataContainerNormalizationOperation<T extends PathArgument>
336 extends NodeToNormalizationNodeOperation<T> {
338 private final DataNodeContainer schema;
339 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
340 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
342 protected DataContainerNormalizationOperation(final T identifier,
343 final DataNodeContainer schema) {
345 this.schema = schema;
346 this.byArg = new ConcurrentHashMap<>();
347 this.byQName = new ConcurrentHashMap<>();
351 public NodeToNormalizedNodeBuilder<?> getChild(
352 final PathArgument child) {
353 NodeToNormalizedNodeBuilder<?> potential = byArg.get(child);
354 if (potential != null) {
357 potential = fromSchema(schema, child);
358 return register(potential);
362 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
367 NodeToNormalizedNodeBuilder<?> potential = byQName.get(child);
368 if (potential != null) {
371 potential = fromSchemaAndPathArgument(schema, child);
372 return register(potential);
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);
389 private static final class ListItemNormalization extends
390 DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
392 private final List<QName> keyDefinition;
393 private final ListSchemaNode schemaNode;
395 protected ListItemNormalization(
396 final NodeIdentifierWithPredicates identifier,
397 final ListSchemaNode schema) {
398 super(identifier, schema);
399 this.schemaNode = schema;
400 keyDefinition = schema.getKeyDefinition();
404 protected NormalizedNodeContainerBuilder createBuilder(
406 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
407 (NodeIdentifierWithPredicates) NodeIdentifierFactory
408 .createPathArgument(node
409 .getPath(), schemaNode);
410 return Builders.mapEntryBuilder()
412 nodeIdentifierWithPredicates
417 public NormalizedNode<?, ?> createDefault(
418 final PathArgument currentArg) {
419 DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode>
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 : "
430 builder.addChild(Builders.leafBuilder()
432 .withNodeIdentifier(new NodeIdentifier(keyValue.getKey()))
433 .withValue(keyValue.getValue()).build());
435 return builder.build();
440 public boolean isKeyedEntry() {
446 private static final class ContainerNormalization extends
447 DataContainerNormalizationOperation<NodeIdentifier> {
449 protected ContainerNormalization(final ContainerSchemaNode schema) {
450 super(new NodeIdentifier(schema.getQName()), schema);
454 protected NormalizedNodeContainerBuilder createBuilder(
456 return Builders.containerBuilder()
457 .withNodeIdentifier(getIdentifier());
461 public NormalizedNode<?, ?> createDefault(
462 final PathArgument currentArg) {
463 return Builders.containerBuilder()
464 .withNodeIdentifier((NodeIdentifier) currentArg).build();
470 private static abstract class MixinNormalizationOp<T extends PathArgument>
471 extends NodeToNormalizationNodeOperation<T> {
473 protected MixinNormalizationOp(final T identifier) {
478 public final boolean isMixin() {
485 private static final class LeafListMixinNormalization extends
486 MixinNormalizationOp<NodeIdentifier> {
488 private final NodeToNormalizedNodeBuilder<?> innerOp;
490 public LeafListMixinNormalization(final LeafListSchemaNode potential) {
491 super(new NodeIdentifier(potential.getQName()));
492 innerOp = new LeafListEntryNormalization(potential);
496 protected NormalizedNodeContainerBuilder createBuilder(
498 return Builders.leafSetBuilder()
499 .withNodeIdentifier(getIdentifier());
503 public NormalizedNode<?, ?> createDefault(
504 final PathArgument currentArg) {
505 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier())
510 public NodeToNormalizedNodeBuilder<?> getChild(
511 final PathArgument child) {
512 if (child instanceof NodeWithValue) {
519 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
520 if (getIdentifier().getNodeType().equals(child)) {
529 private static final class AugmentationNormalization extends
530 MixinNormalizationOp<AugmentationIdentifier> {
532 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
533 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
535 public AugmentationNormalization(final AugmentationSchema augmentation,
536 final DataNodeContainer schema) {
537 super(augmentationIdentifierFrom(augmentation));
539 ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>>
541 ImmutableMap.builder();
542 ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>>
544 ImmutableMap.builder();
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);
556 byQName = byQNameBuilder.build();
557 byArg = byArgBuilder.build();
562 public NodeToNormalizedNodeBuilder<?> getChild(
563 final PathArgument child) {
564 return byArg.get(child);
568 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
569 return byQName.get(child);
573 protected Set<QName> getQNameIdentifiers() {
574 return getIdentifier().getPossibleChildNames();
577 @SuppressWarnings("rawtypes")
579 protected NormalizedNodeContainerBuilder createBuilder(
581 return Builders.augmentationBuilder()
582 .withNodeIdentifier(getIdentifier());
586 public NormalizedNode<?, ?> createDefault(
587 final PathArgument currentArg) {
588 return Builders.augmentationBuilder()
589 .withNodeIdentifier(getIdentifier())
596 private static final class ListMixinNormalization extends
597 MixinNormalizationOp<NodeIdentifier> {
599 private final ListItemNormalization innerNode;
601 public ListMixinNormalization(final ListSchemaNode list) {
602 super(new NodeIdentifier(list.getQName()));
604 new ListItemNormalization(new NodeIdentifierWithPredicates(
605 list.getQName(), Collections.<QName, Object>emptyMap()),
609 @SuppressWarnings("rawtypes")
611 protected NormalizedNodeContainerBuilder createBuilder(
613 return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
617 public NormalizedNode<?, ?> createDefault(
618 final PathArgument currentArg) {
619 return Builders.mapBuilder().withNodeIdentifier(getIdentifier())
624 public NodeToNormalizedNodeBuilder<?> getChild(
625 final PathArgument child) {
626 if (child.getNodeType().equals(getIdentifier().getNodeType())) {
633 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
634 if (getIdentifier().getNodeType().equals(child)) {
643 private static class ChoiceNodeNormalization extends
644 MixinNormalizationOp<NodeIdentifier> {
646 private final ImmutableMap<QName, NodeToNormalizedNodeBuilder<?>>
648 private final ImmutableMap<PathArgument, NodeToNormalizedNodeBuilder<?>>
651 protected ChoiceNodeNormalization(
652 final org.opendaylight.yangtools.yang.model.api.ChoiceNode schema) {
653 super(new NodeIdentifier(schema.getQName()));
654 ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>>
656 ImmutableMap.builder();
657 ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>>
659 ImmutableMap.builder();
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);
671 byQName = byQNameBuilder.build();
672 byArg = byArgBuilder.build();
676 public NodeToNormalizedNodeBuilder<?> getChild(
677 final PathArgument child) {
678 return byArg.get(child);
682 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
683 return byQName.get(child);
687 protected NormalizedNodeContainerBuilder createBuilder(
689 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
693 public NormalizedNode<?, ?> createDefault(
694 final PathArgument currentArg) {
695 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier())
701 * Find an appropriate NormalizedNodeBuilder using both the schema and the
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>
714 FluentIterable.from(schema.getChildNodes()).filter(
715 org.opendaylight.yangtools.yang.model.api.ChoiceNode.class);
716 potential = findChoice(choices, child);
718 if (potential == null) {
719 if (logger.isTraceEnabled()) {
720 logger.trace("BAD CHILD = {}", child.toString());
724 checkArgument(potential != null,
725 "Supplied QName %s is not valid according to schema %s", child,
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()) {
734 AugmentationNormalization augmentation =
735 fromAugmentation(schema, (AugmentationTarget) schema,
738 // If an augmentation normalization (builder) is not found then
739 // we fall through to the regular processing
740 if(augmentation != null){
744 return fromDataSchemaNode(potential);
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
755 private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
756 final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices,
758 org.opendaylight.yangtools.yang.model.api.ChoiceNode foundChoice = null;
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;
773 * Create an AugmentationIdentifier based on the AugmentationSchema
775 * @param augmentation
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());
784 return new AugmentationIdentifier(potentialChildren.build());
788 * Create an AugmentationNormalization based on the schema of the DataContainer, the
789 * AugmentationTarget and the potential schema node
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());
808 if (augmentation != null) {
809 return new AugmentationNormalization(augmentation, schema);
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();
826 return fromSchemaAndPathArgument(schema, childQName);
828 return fromSchemaAndPathArgument(schema, child.getNodeType());
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);
850 public static NodeToNormalizedNodeBuilder<?> from(final SchemaContext ctx) {
851 return new ContainerNormalization(ctx);
854 public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);