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.rfc6020.ext;
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;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.Builder;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
30 public class CustomStatementParserBuilder {
31 private final Map<ModelProcessingPhase, StatementSupportBundle.Builder> reactorSupportBundles;
32 private final Map<ValidationBundleType, Collection<StatementDefinition>> reactorValidationBundles = new HashMap<>();
35 * Creates a new CustomStatementParserBuilder object initialized by
36 * YangInferencePipeline.SUPPORTED_VERSION_BUNDLE. Statement parser will
37 * support the same versions as defined in
38 * YangInferencePipeline.SUPPORTED_VERSION_BUNDLE.
40 public CustomStatementParserBuilder() {
41 this(YangInferencePipeline.SUPPORTED_VERSIONS);
45 * Creates a new CustomStatementParserBuilder object initialized by specific
46 * version bundle. Statement parser will support all versions defined in
47 * given version bundle.
49 * @param supportedVersions
50 * bundle of supported verions
52 public CustomStatementParserBuilder(final Set<YangVersion> supportedVersions) {
53 reactorSupportBundles = ImmutableMap.<ModelProcessingPhase, StatementSupportBundle.Builder>builder()
54 .put(ModelProcessingPhase.INIT, StatementSupportBundle.builder(supportedVersions))
55 .put(ModelProcessingPhase.SOURCE_PRE_LINKAGE, StatementSupportBundle.builder(supportedVersions))
56 .put(ModelProcessingPhase.SOURCE_LINKAGE, StatementSupportBundle.builder(supportedVersions))
57 .put(ModelProcessingPhase.STATEMENT_DEFINITION, StatementSupportBundle.builder(supportedVersions))
58 .put(ModelProcessingPhase.FULL_DECLARATION, StatementSupportBundle.builder(supportedVersions))
59 .put(ModelProcessingPhase.EFFECTIVE_MODEL, StatementSupportBundle.builder(supportedVersions)).build();
62 public CustomStatementParserBuilder addStatementSupport(final ModelProcessingPhase phase,
63 final StatementSupport<?, ?, ?> stmtSupport) {
64 reactorSupportBundles.get(phase).addSupport(stmtSupport);
68 public CustomStatementParserBuilder addNamespaceSupport(final ModelProcessingPhase phase,
69 final NamespaceBehaviour<?, ?, ?> namespaceSupport) {
70 reactorSupportBundles.get(phase).addSupport(namespaceSupport);
74 public CustomStatementParserBuilder addDefaultRFC6020Bundles() {
75 addRFC6020SupportBundles();
76 addRFC6020ValidationBundles();
80 private void addRFC6020ValidationBundles() {
81 reactorValidationBundles.putAll(YangInferencePipeline.RFC6020_VALIDATION_BUNDLE);
84 private void addRFC6020SupportBundles() {
85 for (final Entry<ModelProcessingPhase, StatementSupportBundle> entry : YangInferencePipeline.RFC6020_BUNDLES
87 addAllSupports(entry.getKey(), entry.getValue());
91 public CustomStatementParserBuilder addValidationBundle(final ValidationBundleType validationBundleType,
92 final Collection<StatementDefinition> validationBundle) {
93 reactorValidationBundles.put(validationBundleType, validationBundle);
97 public CustomStatementParserBuilder addAllSupports(final ModelProcessingPhase phase,
98 final StatementSupportBundle stmtSupportBundle) {
99 addAllCommonStatementSupports(phase, stmtSupportBundle.getCommonDefinitions().values());
100 addAllVersionSpecificSupports(phase, stmtSupportBundle.getAllVersionSpecificDefinitions());
101 addAllNamespaceSupports(phase, stmtSupportBundle.getNamespaceDefinitions().values());
105 public CustomStatementParserBuilder addAllNamespaceSupports(final ModelProcessingPhase phase,
106 final Collection<NamespaceBehaviour<?, ?, ?>> namespaceSupports) {
107 final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
108 for (final NamespaceBehaviour<?, ?, ?> namespaceSupport : namespaceSupports) {
109 stmtBundleBuilder.addSupport(namespaceSupport);
116 * {@link #addAllCommonStatementSupports(ModelProcessingPhase, Collection)
117 * addAllCommonStatementSupports} method instead.
120 public CustomStatementParserBuilder addAllStatementSupports(final ModelProcessingPhase phase,
121 final Collection<StatementSupport<?, ?, ?>> statementSupports) {
122 return addAllCommonStatementSupports(phase, statementSupports);
125 public CustomStatementParserBuilder addAllCommonStatementSupports(final ModelProcessingPhase phase,
126 final Collection<StatementSupport<?, ?, ?>> statementSupports) {
127 final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
128 for (final StatementSupport<?, ?, ?> statementSupport : statementSupports) {
129 stmtBundleBuilder.addSupport(statementSupport);
134 public CustomStatementParserBuilder addAllVersionSpecificSupports(final ModelProcessingPhase phase,
135 final Table<YangVersion, QName, StatementSupport<?, ?, ?>> versionSpecificSupports) {
136 final StatementSupportBundle.Builder stmtBundleBuilder = reactorSupportBundles.get(phase);
137 for (final Cell<YangVersion, QName, StatementSupport<?, ?, ?>> cell : versionSpecificSupports.cellSet()) {
138 stmtBundleBuilder.addVersionSpecificSupport(cell.getRowKey(), cell.getValue());
143 public CrossSourceStatementReactor build() {
144 final StatementSupportBundle initBundle = reactorSupportBundles.get(ModelProcessingPhase.INIT).build();
145 final StatementSupportBundle preLinkageBundle = reactorSupportBundles
146 .get(ModelProcessingPhase.SOURCE_PRE_LINKAGE).setParent(initBundle).build();
147 final StatementSupportBundle linkageBundle = reactorSupportBundles.get(ModelProcessingPhase.SOURCE_LINKAGE)
148 .setParent(preLinkageBundle).build();
149 final StatementSupportBundle stmtDefBundle = reactorSupportBundles
150 .get(ModelProcessingPhase.STATEMENT_DEFINITION).setParent(linkageBundle).build();
151 final StatementSupportBundle fullDeclBundle = reactorSupportBundles.get(ModelProcessingPhase.FULL_DECLARATION)
152 .setParent(stmtDefBundle).build();
153 final StatementSupportBundle effectiveBundle = reactorSupportBundles.get(ModelProcessingPhase.EFFECTIVE_MODEL)
154 .setParent(fullDeclBundle).build();
156 final Builder reactorBuilder = CrossSourceStatementReactor.builder()
157 .setBundle(ModelProcessingPhase.INIT, initBundle)
158 .setBundle(ModelProcessingPhase.SOURCE_PRE_LINKAGE, preLinkageBundle)
159 .setBundle(ModelProcessingPhase.SOURCE_LINKAGE, linkageBundle)
160 .setBundle(ModelProcessingPhase.STATEMENT_DEFINITION, stmtDefBundle)
161 .setBundle(ModelProcessingPhase.FULL_DECLARATION, fullDeclBundle)
162 .setBundle(ModelProcessingPhase.EFFECTIVE_MODEL, effectiveBundle);
164 for (final Entry<ValidationBundleType, Collection<StatementDefinition>> entry : reactorValidationBundles
166 reactorBuilder.setValidationBundle(entry.getKey(), entry.getValue());
169 return reactorBuilder.build();