BUG-2159: fix wrong javadocs
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / serializer / BaseDispatcherSerializer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.transform.base.serializer;
9
10 import java.util.List;
11 import java.util.Set;
12
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.transform.FromNormalizedNodeSerializer;
18 import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Iterables;
24 import com.google.common.collect.Lists;
25
26 /**
27  * Abstract(base) Serializer for DataContainerNodes e.g. ContainerNode, AugmentationNode.
28  */
29 public abstract class BaseDispatcherSerializer<E, N extends DataContainerNode<?>, S> implements
30         FromNormalizedNodeSerializer<E, N, S> {
31
32     /**
33      *
34      * @param schema
35      * @param augmentationSchema
36      * @return Set of real schema objects that represent child nodes of an
37      *         augmentation. Augmentation schema child nodes, if further
38      *         augmented, do not contain further augmented, that are crucial for
39      *         parsing. The real schema object can be retrieved from parent schema: schema.
40      */
41     protected abstract Set<DataSchemaNode> getRealSchemasForAugment(S schema, AugmentationSchema augmentationSchema);
42
43     /**
44      *
45      * @param schema
46      * @param childNode
47      * @return Schema object associated with child node identified as: childNode.
48      *         Schema should be retrieved from parent schema: schema.
49      */
50     protected abstract DataSchemaNode getSchemaForChild(S schema,
51             DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> childNode);
52
53     /**
54      *
55      * @param schema
56      * @param augmentationNode
57      * @return Schema object associated with augmentation child node identified as: augmentationNode.
58      *         Schema should be retrieved from parent schema: schema.
59      */
60     protected abstract AugmentationSchema getAugmentedCase(S schema, AugmentationNode augmentationNode);
61
62     /**
63      *
64      * @return Dispatcher object to dispatch serialization of child elements, might be
65      *         the same instance if provided serializers are immutable.
66      */
67     protected abstract NodeSerializerDispatcher<E> getNodeDispatcher();
68
69     @Override
70     public Iterable<E> serialize(S schema, N node) {
71         List<Iterable<E>> choiceChildren = Lists.newArrayList();
72
73         for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> choiceChild : node.getValue()) {
74
75             Object childSchema;
76
77             if (choiceChild instanceof AugmentationNode) {
78
79                 AugmentationSchema augSchema = getAugmentedCase(schema, (AugmentationNode) choiceChild);
80                 Set<DataSchemaNode> realChildSchemas = getRealSchemasForAugment(schema, augSchema);
81                 childSchema = new AugmentationSchemaProxy(augSchema, realChildSchemas);
82
83             } else {
84                 childSchema = getSchemaForChild(schema, choiceChild);
85             }
86
87             Iterable<E> childElements = getNodeDispatcher().dispatchChildElement(childSchema, choiceChild);
88             choiceChildren.add(Preconditions.checkNotNull(childElements));
89         }
90
91         return Iterables.concat(choiceChildren);
92     }
93 }