Bug 6867: Extend yang statement parser to support different yang versions
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / CustomStatementParserBuilder.java
1 /*
2  * Copyright (c) 2016 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.parser.stmt.reactor;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.Table;
12 import com.google.common.collect.Table.Cell;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
25 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.Builder;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
28
29 public class CustomStatementParserBuilder {
30     private final Map<ModelProcessingPhase, StatementSupportBundle.Builder> reactorSupportBundles;
31     private final Map<ValidationBundleType, Collection<StatementDefinition>> reactorValidationBundles = new HashMap<>();
32
33     /**
34      * Creates a new CustomStatementParserBuilder object initialized by
35      * YangInferencePipeline.SUPPORTED_VERSION_BUNDLE. Statement parser will
36      * support the same versions as defined in
37      * YangInferencePipeline.SUPPORTED_VERSION_BUNDLE.
38      */
39     public CustomStatementParserBuilder() {
40         this(YangInferencePipeline.SUPPORTED_VERSIONS);
41     }
42
43     /**
44      * Creates a new CustomStatementParserBuilder object initialized by specific
45      * version bundle. Statement parser will support all versions defined in
46      * given version bundle.
47      *
48      * @param supportedVersions
49      *            bundle of supported verions
50      */
51     public CustomStatementParserBuilder(final Set<YangVersion> supportedVersions) {
52         reactorSupportBundles = ImmutableMap.<ModelProcessingPhase, StatementSupportBundle.Builder> builder()
53                 .put(ModelProcessingPhase.INIT, StatementSupportBundle.builder(supportedVersions))
54                 .put(ModelProcessingPhase.SOURCE_PRE_LINKAGE, StatementSupportBundle.builder(supportedVersions))
55                 .put(ModelProcessingPhase.SOURCE_LINKAGE, StatementSupportBundle.builder(supportedVersions))
56                 .put(ModelProcessingPhase.STATEMENT_DEFINITION, StatementSupportBundle.builder(supportedVersions))
57                 .put(ModelProcessingPhase.FULL_DECLARATION, StatementSupportBundle.builder(supportedVersions))
58                 .put(ModelProcessingPhase.EFFECTIVE_MODEL, StatementSupportBundle.builder(supportedVersions)).build();
59     }
60
61     public CustomStatementParserBuilder addStatementSupport(final ModelProcessingPhase phase,
62             final StatementSupport<?, ?, ?> stmtSupport) {
63         reactorSupportBundles.get(phase).addSupport(stmtSupport);
64         return this;
65     }
66
67     public CustomStatementParserBuilder addNamespaceSupport(final ModelProcessingPhase phase,
68             final NamespaceBehaviour<?, ?, ?> namespaceSupport) {
69         reactorSupportBundles.get(phase).addSupport(namespaceSupport);
70         return this;
71     }
72
73     public CustomStatementParserBuilder addDefaultRFC6020Bundles() {
74         addRFC6020SupportBundles();
75         addRFC6020ValidationBundles();
76         return this;
77     }
78
79     private void addRFC6020ValidationBundles() {
80         reactorValidationBundles.putAll(YangInferencePipeline.RFC6020_VALIDATION_BUNDLE);
81     }
82
83     private void addRFC6020SupportBundles() {
84         for (final Entry<ModelProcessingPhase, StatementSupportBundle> entry : YangInferencePipeline.RFC6020_BUNDLES
85                 .entrySet()) {
86             addAllSupports(entry.getKey(), entry.getValue());
87         }
88     }
89
90     public CustomStatementParserBuilder addValidationBundle(final ValidationBundleType validationBundleType,
91             final Collection<StatementDefinition> validationBundle) {
92         reactorValidationBundles.put(validationBundleType, validationBundle);
93         return this;
94     }
95
96     public CustomStatementParserBuilder addAllSupports(final ModelProcessingPhase phase,
97             final StatementSupportBundle stmtSupportBundle) {
98         addAllCommonStatementSupports(phase, stmtSupportBundle.getCommonDefinitions().values());
99         addAllVersionSpecificSupports(phase, stmtSupportBundle.getAllVersionSpecificDefinitions());
100         addAllNamespaceSupports(phase, stmtSupportBundle.getNamespaceDefinitions().values());
101         return this;
102     }
103
104     public CustomStatementParserBuilder addAllNamespaceSupports(final ModelProcessingPhase phase,
105             final Collection<NamespaceBehaviour<?, ?, ?>> namespaceSupports) {
106         final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
107         for (final NamespaceBehaviour<?, ?, ?> namespaceSupport : namespaceSupports) {
108             stmtBundleBuilder.addSupport(namespaceSupport);
109         }
110         return this;
111     }
112
113     /**
114      * Use
115      * {@link #addAllCommonStatementSupports(ModelProcessingPhase, Collection)
116      * addAllCommonStatementSupports} method instead.
117      */
118     @Deprecated
119     public CustomStatementParserBuilder addAllStatementSupports(final ModelProcessingPhase phase,
120             final Collection<StatementSupport<?, ?, ?>> statementSupports) {
121         return addAllCommonStatementSupports(phase, statementSupports);
122     }
123
124     public CustomStatementParserBuilder addAllCommonStatementSupports(final ModelProcessingPhase phase,
125             final Collection<StatementSupport<?, ?, ?>> statementSupports) {
126         final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
127         for (final StatementSupport<?, ?, ?> statementSupport : statementSupports) {
128             stmtBundleBuilder.addSupport(statementSupport);
129         }
130         return this;
131     }
132
133     public CustomStatementParserBuilder addAllVersionSpecificSupports(final ModelProcessingPhase phase,
134             final Table<YangVersion, QName, StatementSupport<?, ?, ?>> versionSpecificSupports) {
135         final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
136         for (final Cell<YangVersion, QName, StatementSupport<?, ?, ?>> cell : versionSpecificSupports.cellSet()) {
137             stmtBundleBuilder.addVersionSpecificSupport(cell.getRowKey(), cell.getValue());
138         }
139         return this;
140     }
141
142     public CrossSourceStatementReactor build() {
143         final StatementSupportBundle initBundle = reactorSupportBundles.get(ModelProcessingPhase.INIT).build();
144         final StatementSupportBundle preLinkageBundle = reactorSupportBundles
145                 .get(ModelProcessingPhase.SOURCE_PRE_LINKAGE).setParent(initBundle).build();
146         final StatementSupportBundle linkageBundle = reactorSupportBundles.get(ModelProcessingPhase.SOURCE_LINKAGE)
147                 .setParent(preLinkageBundle).build();
148         final StatementSupportBundle stmtDefBundle = reactorSupportBundles
149                 .get(ModelProcessingPhase.STATEMENT_DEFINITION).setParent(linkageBundle).build();
150         final StatementSupportBundle fullDeclBundle = reactorSupportBundles.get(ModelProcessingPhase.FULL_DECLARATION)
151                 .setParent(stmtDefBundle).build();
152         final StatementSupportBundle effectiveBundle = reactorSupportBundles.get(ModelProcessingPhase.EFFECTIVE_MODEL)
153                 .setParent(fullDeclBundle).build();
154
155         final Builder reactorBuilder = CrossSourceStatementReactor.builder()
156                 .setBundle(ModelProcessingPhase.INIT, initBundle)
157                 .setBundle(ModelProcessingPhase.SOURCE_PRE_LINKAGE, preLinkageBundle)
158                 .setBundle(ModelProcessingPhase.SOURCE_LINKAGE, linkageBundle)
159                 .setBundle(ModelProcessingPhase.STATEMENT_DEFINITION, stmtDefBundle)
160                 .setBundle(ModelProcessingPhase.FULL_DECLARATION, fullDeclBundle)
161                 .setBundle(ModelProcessingPhase.EFFECTIVE_MODEL, effectiveBundle);
162
163         for (final Entry<ValidationBundleType, Collection<StatementDefinition>> entry : reactorValidationBundles
164                 .entrySet()) {
165             reactorBuilder.setValidationBundle(entry.getKey(), entry.getValue());
166         }
167
168         return reactorBuilder.build();
169     }
170 }