2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.netconf.cli.writer.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Iterables;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MixinNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.transform.FromNormalizedNodeSerializerFactory;
22 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer.NodeSerializerDispatcher;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 public class NodeCliSerializerDispatcher implements NodeSerializerDispatcher<String> {
31 private final FromNormalizedNodeSerializerFactory<String> factory;
33 public NodeCliSerializerDispatcher(final FromNormalizedNodeSerializerFactory<String> factory) {
34 this.factory = Preconditions.checkNotNull(factory);
38 public final Iterable<String> dispatchChildElement(final Object childSchema,
39 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
40 if (dataContainerChild instanceof ContainerNode) {
41 return onContainerNode(childSchema, dataContainerChild);
42 } else if (dataContainerChild instanceof LeafNode<?>) {
43 return onLeafNode(childSchema, dataContainerChild);
44 } else if (dataContainerChild instanceof MixinNode) {
45 if (dataContainerChild instanceof LeafSetNode<?>) {
46 return onLeafListNode(childSchema, dataContainerChild);
47 } else if (dataContainerChild instanceof MapNode) {
48 return onListNode(childSchema, dataContainerChild);
49 } else if (dataContainerChild instanceof ChoiceNode) {
50 return onChoiceNode(childSchema, dataContainerChild);
51 } else if (dataContainerChild instanceof AugmentationNode) {
52 return onAugmentationSchema(childSchema, dataContainerChild);
55 throw new IllegalArgumentException("Unable to serialize " + childSchema);
58 private Iterable<String> onAugmentationSchema(final Object childSchema,
59 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
60 checkSchemaCompatibility(childSchema, AugmentationSchema.class, dataContainerChild);
61 return factory.getAugmentationNodeSerializer().serialize((AugmentationSchema) childSchema,
62 (AugmentationNode) dataContainerChild);
65 private Iterable<String> onChoiceNode(final Object childSchema,
66 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
67 checkSchemaCompatibility(childSchema, ChoiceSchemaNode.class, dataContainerChild);
68 return factory.getChoiceNodeSerializer().serialize(
69 (ChoiceSchemaNode) childSchema, (ChoiceNode) dataContainerChild);
72 private Iterable<String> onListNode(final Object childSchema,
73 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
74 checkSchemaCompatibility(childSchema, ListSchemaNode.class, dataContainerChild);
75 return factory.getMapNodeSerializer().serialize((ListSchemaNode) childSchema, (MapNode) dataContainerChild);
78 private Iterable<String> onLeafListNode(final Object childSchema,
79 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
80 checkSchemaCompatibility(childSchema, LeafListSchemaNode.class, dataContainerChild);
81 return factory.getLeafSetNodeSerializer().serialize((LeafListSchemaNode) childSchema,
82 (LeafSetNode<?>) dataContainerChild);
85 private Iterable<String> onLeafNode(final Object childSchema,
86 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
87 checkSchemaCompatibility(childSchema, LeafSchemaNode.class, dataContainerChild);
88 final Iterable<String> elements = factory.getLeafNodeSerializer().serialize((LeafSchemaNode) childSchema,
89 (LeafNode<?>) dataContainerChild);
90 checkOnlyOneSerializedElement(elements, dataContainerChild);
94 private static void checkOnlyOneSerializedElement(final Iterable<?> elements,
95 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
96 final int size = Iterables.size(elements);
97 Preconditions.checkArgument(size == 1,
98 "Unexpected count of elements for entry serialized from: %s, should be 1, was: %s", dataContainerChild,
102 private Iterable<String> onContainerNode(final Object childSchema,
103 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
104 checkSchemaCompatibility(childSchema, ContainerSchemaNode.class, dataContainerChild);
106 final Iterable<String> elements = factory.getContainerNodeSerializer().serialize(
107 (ContainerSchemaNode) childSchema, (ContainerNode) dataContainerChild);
108 checkOnlyOneSerializedElement(elements, dataContainerChild);
112 private static void checkSchemaCompatibility(final Object childSchema, final Class<?> containerSchemaNodeClass,
113 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild) {
114 Preconditions.checkArgument(containerSchemaNodeClass.isAssignableFrom(childSchema.getClass()),
115 "Incompatible schema: %s with node: %s, expected: %s", childSchema, dataContainerChild,
116 containerSchemaNodeClass);