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.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;
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);
197 if (!node.getType().equals(AugmentationNode.class.getSimpleName())&& !node.getType().equals(ContainerNode.class.getSimpleName())) {
198 checkArgument(nodeType != null);
201 NormalizedNodeContainerBuilder builder = createBuilder(node);
203 Set<NodeToNormalizedNodeBuilder<?>> usedMixins = new HashSet<>();
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(),
216 Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue)
217 .withValue(node.getValue()).build();
220 ImmutableNodes.leafNode(childPathArgument.getNodeType(),
223 builder.addChild(child);
226 final List<Node> children = node.getChildList();
227 for (Node nodeChild : children) {
229 PathArgument childPathArgument =
230 NodeIdentifierFactory.getArgument(nodeChild.getPath());
232 QName childNodeType = null;
233 NodeToNormalizedNodeBuilder childOp = null;
236 if (childPathArgument instanceof AugmentationIdentifier) {
237 childOp = getChild(childPathArgument);
239 childNodeType = childPathArgument.getNodeType();
240 childOp = getChild(childNodeType);
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()) {
246 } else if (childOp == null) {
248 "childOp is null and this operation is not a mixin : this = {}",
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
258 // skiping next nodes.
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);
268 final NormalizedNode childNode =
269 childOp.normalize(childNodeType, nodeChild);
270 if (childNode != null)
271 builder.addChild(childNode);
277 return (NormalizedNodeContainer<?, ?, ?>) builder.build();
278 } catch (Exception e) {
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());
290 @SuppressWarnings("rawtypes")
291 protected abstract NormalizedNodeContainerBuilder createBuilder(
296 private static abstract class DataContainerNormalizationOperation<T extends PathArgument>
297 extends NodeToNormalizationNodeOperation<T> {
299 private final DataNodeContainer schema;
300 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
301 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
303 protected DataContainerNormalizationOperation(final T identifier,
304 final DataNodeContainer schema) {
306 this.schema = schema;
307 this.byArg = new ConcurrentHashMap<>();
308 this.byQName = new ConcurrentHashMap<>();
312 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
313 NodeToNormalizedNodeBuilder<?> potential = byArg.get(child);
314 if (potential != null) {
317 potential = fromSchema(schema, child);
318 return register(potential);
322 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
327 NodeToNormalizedNodeBuilder<?> potential = byQName.get(child);
328 if (potential != null) {
331 potential = fromSchemaAndPathArgument(schema, child);
332 return register(potential);
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);
348 private static final class ListItemNormalization extends
349 DataContainerNormalizationOperation<NodeIdentifierWithPredicates> {
351 private final List<QName> keyDefinition;
352 private final ListSchemaNode schemaNode;
354 protected ListItemNormalization(
355 final NodeIdentifierWithPredicates identifier,
356 final ListSchemaNode schema) {
357 super(identifier, schema);
358 this.schemaNode = schema;
359 keyDefinition = schema.getKeyDefinition();
363 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
364 return Builders.mapEntryBuilder().withNodeIdentifier(
365 (NodeIdentifierWithPredicates) NodeIdentifierFactory.getArgument(node
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 : "
380 builder.addChild(Builders.leafBuilder()
382 .withNodeIdentifier(new NodeIdentifier(keyValue.getKey()))
383 .withValue(keyValue.getValue()).build());
385 return builder.build();
390 public boolean isKeyedEntry() {
395 private static final class ContainerNormalization extends
396 DataContainerNormalizationOperation<NodeIdentifier> {
398 protected ContainerNormalization(final ContainerSchemaNode schema) {
399 super(new NodeIdentifier(schema.getQName()), schema);
403 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
404 return Builders.containerBuilder().withNodeIdentifier(getIdentifier());
408 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
409 return Builders.containerBuilder()
410 .withNodeIdentifier((NodeIdentifier) currentArg).build();
415 private static abstract class MixinNormalizationOp<T extends PathArgument>
416 extends NodeToNormalizationNodeOperation<T> {
418 protected MixinNormalizationOp(final T identifier) {
423 public final boolean isMixin() {
429 private static final class LeafListMixinNormalization extends
430 MixinNormalizationOp<NodeIdentifier> {
432 private final NodeToNormalizedNodeBuilder<?> innerOp;
434 public LeafListMixinNormalization(final LeafListSchemaNode potential) {
435 super(new NodeIdentifier(potential.getQName()));
436 innerOp = new LeafListEntryNormalization(potential);
440 protected NormalizedNodeContainerBuilder createBuilder(
442 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier());
446 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
447 return Builders.leafSetBuilder().withNodeIdentifier(getIdentifier())
452 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
453 if (child instanceof NodeWithValue) {
460 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
461 if (getIdentifier().getNodeType().equals(child)) {
469 private static final class AugmentationNormalization extends
470 MixinNormalizationOp<AugmentationIdentifier> {
472 private final Map<QName, NodeToNormalizedNodeBuilder<?>> byQName;
473 private final Map<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
475 public AugmentationNormalization(final AugmentationSchema augmentation,
476 final DataNodeContainer schema) {
477 super(augmentationIdentifierFrom(augmentation));
479 ImmutableMap.Builder<QName, NodeToNormalizedNodeBuilder<?>> byQNameBuilder =
480 ImmutableMap.builder();
481 ImmutableMap.Builder<PathArgument, NodeToNormalizedNodeBuilder<?>> byArgBuilder =
482 ImmutableMap.builder();
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);
494 byQName = byQNameBuilder.build();
495 byArg = byArgBuilder.build();
500 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
501 return byArg.get(child);
505 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
506 return byQName.get(child);
510 protected Set<QName> getQNameIdentifiers() {
511 return getIdentifier().getPossibleChildNames();
514 @SuppressWarnings("rawtypes")
516 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
517 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier());
521 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
522 return Builders.augmentationBuilder().withNodeIdentifier(getIdentifier())
528 private static final class ListMixinNormalization extends
529 MixinNormalizationOp<NodeIdentifier> {
531 private final ListItemNormalization innerNode;
533 public ListMixinNormalization(final ListSchemaNode list) {
534 super(new NodeIdentifier(list.getQName()));
536 new ListItemNormalization(new NodeIdentifierWithPredicates(
537 list.getQName(), Collections.<QName, Object>emptyMap()), list);
540 @SuppressWarnings("rawtypes")
542 protected NormalizedNodeContainerBuilder createBuilder(
544 return Builders.mapBuilder().withNodeIdentifier(getIdentifier());
548 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
549 return Builders.mapBuilder().withNodeIdentifier(getIdentifier()).build();
553 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
554 if (child.getNodeType().equals(getIdentifier().getNodeType())) {
561 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
562 if (getIdentifier().getNodeType().equals(child)) {
570 private static class ChoiceNodeNormalization extends
571 MixinNormalizationOp<NodeIdentifier> {
573 private final ImmutableMap<QName, NodeToNormalizedNodeBuilder<?>> byQName;
574 private final ImmutableMap<PathArgument, NodeToNormalizedNodeBuilder<?>> byArg;
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();
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);
594 byQName = byQNameBuilder.build();
595 byArg = byArgBuilder.build();
599 public NodeToNormalizedNodeBuilder<?> getChild(final PathArgument child) {
600 return byArg.get(child);
604 public NodeToNormalizedNodeBuilder<?> getChild(final QName child) {
605 return byQName.get(child);
609 protected NormalizedNodeContainerBuilder createBuilder(final Node node) {
610 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier());
614 public NormalizedNode<?, ?> createDefault(final PathArgument currentArg) {
615 return Builders.choiceBuilder().withNodeIdentifier(getIdentifier())
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);
629 if (potential == null) {
630 if (logger.isTraceEnabled()) {
631 logger.trace("BAD CHILD = {}", child.toString());
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);
642 return fromDataSchemaNode(potential);
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
653 private static org.opendaylight.yangtools.yang.model.api.ChoiceNode findChoice(
654 final Iterable<org.opendaylight.yangtools.yang.model.api.ChoiceNode> choices,
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;
670 * Create an AugmentationIdentifier based on the AugmentationSchema
672 * @param augmentation
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());
681 return new AugmentationIdentifier(null, potentialChildren.build());
685 * Create an AugmentationNormalization based on the schema of the DataContainer, the
686 * AugmentationTarget and the potential schema node
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());
705 if (augmentation != null) {
706 return new AugmentationNormalization(augmentation, schema);
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());
724 return fromSchemaAndPathArgument(schema, child.getNodeType());
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);
744 public static NodeToNormalizedNodeBuilder<?> from(final SchemaContext ctx) {
745 return new ContainerNormalization(ctx);
748 public abstract NormalizedNode<?, ?> createDefault(PathArgument currentArg);