2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
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;
16 import java.util.Map.Entry;
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;
29 public class CustomStatementParserBuilder {
30 private final Map<ModelProcessingPhase, StatementSupportBundle.Builder> reactorSupportBundles;
31 private final Map<ValidationBundleType, Collection<StatementDefinition>> reactorValidationBundles = new HashMap<>();
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.
39 public CustomStatementParserBuilder() {
40 this(YangInferencePipeline.SUPPORTED_VERSIONS);
44 * Creates a new CustomStatementParserBuilder object initialized by specific
45 * version bundle. Statement parser will support all versions defined in
46 * given version bundle.
48 * @param supportedVersions
49 * bundle of supported verions
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();
61 public CustomStatementParserBuilder addStatementSupport(final ModelProcessingPhase phase,
62 final StatementSupport<?, ?, ?> stmtSupport) {
63 reactorSupportBundles.get(phase).addSupport(stmtSupport);
67 public CustomStatementParserBuilder addNamespaceSupport(final ModelProcessingPhase phase,
68 final NamespaceBehaviour<?, ?, ?> namespaceSupport) {
69 reactorSupportBundles.get(phase).addSupport(namespaceSupport);
73 public CustomStatementParserBuilder addDefaultRFC6020Bundles() {
74 addRFC6020SupportBundles();
75 addRFC6020ValidationBundles();
79 private void addRFC6020ValidationBundles() {
80 reactorValidationBundles.putAll(YangInferencePipeline.RFC6020_VALIDATION_BUNDLE);
83 private void addRFC6020SupportBundles() {
84 for (final Entry<ModelProcessingPhase, StatementSupportBundle> entry : YangInferencePipeline.RFC6020_BUNDLES
86 addAllSupports(entry.getKey(), entry.getValue());
90 public CustomStatementParserBuilder addValidationBundle(final ValidationBundleType validationBundleType,
91 final Collection<StatementDefinition> validationBundle) {
92 reactorValidationBundles.put(validationBundleType, validationBundle);
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());
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);
115 * {@link #addAllCommonStatementSupports(ModelProcessingPhase, Collection)
116 * addAllCommonStatementSupports} method instead.
119 public CustomStatementParserBuilder addAllStatementSupports(final ModelProcessingPhase phase,
120 final Collection<StatementSupport<?, ?, ?>> statementSupports) {
121 return addAllCommonStatementSupports(phase, statementSupports);
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);
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());
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();
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);
163 for (final Entry<ValidationBundleType, Collection<StatementDefinition>> entry : reactorValidationBundles
165 reactorBuilder.setValidationBundle(entry.getKey(), entry.getValue());
168 return reactorBuilder.build();