Merge "Revert "Composite node <-> normalized node serializer and deserializer""
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / parser / NodeParserDispatcher.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.parser;
9
10 import java.util.List;
11
12 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
13 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParserFactory;
14 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20
21 import com.google.common.base.Preconditions;
22
23 /**
24  *
25  * Dispatches the parsing process of elements according to schema and returns the parsed Node.
26  *
27  * @param <E> type of elements parsed
28  */
29 public interface NodeParserDispatcher<E> {
30
31     DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes);
32
33     /**
34      * Abstract implementation that implements the dispatch conditions. Only requires parsers to be provided.
35      * The same instance of parser can be provided in case it is immutable.
36      */
37     public static abstract class BaseNodeParserDispatcher<E> implements NodeParserDispatcher<E> {
38         private final ToNormalizedNodeParserFactory<E> factory;
39
40         protected BaseNodeParserDispatcher(final ToNormalizedNodeParserFactory<E> factory) {
41             this.factory = Preconditions.checkNotNull(factory);
42         }
43
44         @Override
45         public final DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes) {
46             Preconditions.checkArgument(childNodes.isEmpty() == false);
47
48             if (schema instanceof ContainerSchemaNode) {
49                 return factory.getContainerNodeParser().parse(childNodes, (ContainerSchemaNode) schema);
50             } else if (schema instanceof LeafSchemaNode) {
51                 return factory.getLeafNodeParser().parse(childNodes, (LeafSchemaNode) schema);
52             } else if (schema instanceof LeafListSchemaNode) {
53                 return factory.getLeafSetNodeParser().parse(childNodes, (LeafListSchemaNode) schema);
54             } else if (schema instanceof ListSchemaNode) {
55                 return factory.getMapNodeParser().parse(childNodes, (ListSchemaNode) schema);
56             } else if (schema instanceof ChoiceNode) {
57                 return factory.getChoiceNodeParser().parse(childNodes, (ChoiceNode) schema);
58             } else if (schema instanceof AugmentationSchema) {
59                 return factory.getAugmentationNodeParser().parse(childNodes, (AugmentationSchema) schema);
60             }
61
62             throw new IllegalArgumentException("Unable to parse node, unknown schema type: " + schema.getClass());
63         }
64     }
65 }