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