Abstract infrastructure for normalized nodes translation.
[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.AugmentationNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25
26 import com.google.common.base.Preconditions;
27
28 /**
29  *
30  * Dispatches the parsing process of elements according to schema and returns the parsed Node.
31  *
32  * @param <E> type of elements parsed
33  */
34 public interface NodeParserDispatcher<E> {
35
36     DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes);
37
38     /**
39      * Abstract implementation that implements the dispatch conditions. Only requires parsers to be provided.
40      * The same instance of parser can be provided in case it is immutable.
41      */
42     public static abstract class BaseNodeParserDispatcher<E> implements NodeParserDispatcher<E> {
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 getContainerNodeParser().parse(childNodes, (ContainerSchemaNode) schema);
50             } else if (schema instanceof LeafSchemaNode) {
51                 return getLeafNodeParser().parse(childNodes, (LeafSchemaNode) schema);
52             } else if (schema instanceof LeafListSchemaNode) {
53                 return getLeafSetNodeParser().parse(childNodes, (LeafListSchemaNode) schema);
54             } else if (schema instanceof ListSchemaNode) {
55                 return getMapNodeParser().parse(childNodes, (ListSchemaNode) schema);
56             } else if (schema instanceof ChoiceNode) {
57                 return getChoiceNodeParser().parse(childNodes, (ChoiceNode) schema);
58             } else if (schema instanceof AugmentationSchema) {
59                 return getAugmentationNodeParser().parse(childNodes, (AugmentationSchema) schema);
60             }
61
62             throw new IllegalArgumentException("Unable to parse node, unknown schema type: " + schema.getClass());
63         }
64
65         protected abstract ToNormalizedNodeParser<E, ContainerNode, ContainerSchemaNode> getContainerNodeParser();
66
67         protected abstract ToNormalizedNodeParser<E, LeafNode<?>, LeafSchemaNode> getLeafNodeParser();
68
69         protected abstract ToNormalizedNodeParser<E, LeafSetNode<?>, LeafListSchemaNode> getLeafSetNodeParser();
70
71         protected abstract ToNormalizedNodeParser<E, MapNode, ListSchemaNode> getMapNodeParser();
72
73         protected abstract ToNormalizedNodeParser<E, org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode, ChoiceNode> getChoiceNodeParser();
74
75         protected abstract ToNormalizedNodeParser<E, AugmentationNode, AugmentationSchema> getAugmentationNodeParser();
76
77     }
78 }