Fix checkstyle in yang-parser-impl and enable enforcement
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ext / 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.rfc6020.ext;
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;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.Builder;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
29
30 public class CustomStatementParserBuilder {
31     private final Map<ModelProcessingPhase, StatementSupportBundle.Builder> reactorSupportBundles;
32     private final Map<ValidationBundleType, Collection<StatementDefinition>> reactorValidationBundles = new HashMap<>();
33
34     /**
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.
39      */
40     public CustomStatementParserBuilder() {
41         this(YangInferencePipeline.SUPPORTED_VERSIONS);
42     }
43
44     /**
45      * Creates a new CustomStatementParserBuilder object initialized by specific
46      * version bundle. Statement parser will support all versions defined in
47      * given version bundle.
48      *
49      * @param supportedVersions
50      *            bundle of supported verions
51      */
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();
60     }
61
62     public CustomStatementParserBuilder addStatementSupport(final ModelProcessingPhase phase,
63             final StatementSupport<?, ?, ?> stmtSupport) {
64         reactorSupportBundles.get(phase).addSupport(stmtSupport);
65         return this;
66     }
67
68     public CustomStatementParserBuilder addNamespaceSupport(final ModelProcessingPhase phase,
69             final NamespaceBehaviour<?, ?, ?> namespaceSupport) {
70         reactorSupportBundles.get(phase).addSupport(namespaceSupport);
71         return this;
72     }
73
74     public CustomStatementParserBuilder addDefaultRFC6020Bundles() {
75         addRFC6020SupportBundles();
76         addRFC6020ValidationBundles();
77         return this;
78     }
79
80     private void addRFC6020ValidationBundles() {
81         reactorValidationBundles.putAll(YangInferencePipeline.RFC6020_VALIDATION_BUNDLE);
82     }
83
84     private void addRFC6020SupportBundles() {
85         for (final Entry<ModelProcessingPhase, StatementSupportBundle> entry : YangInferencePipeline.RFC6020_BUNDLES
86                 .entrySet()) {
87             addAllSupports(entry.getKey(), entry.getValue());
88         }
89     }
90
91     public CustomStatementParserBuilder addValidationBundle(final ValidationBundleType validationBundleType,
92             final Collection<StatementDefinition> validationBundle) {
93         reactorValidationBundles.put(validationBundleType, validationBundle);
94         return this;
95     }
96
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());
102         return this;
103     }
104
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);
110         }
111         return this;
112     }
113
114     /**
115      * Use
116      * {@link #addAllCommonStatementSupports(ModelProcessingPhase, Collection)
117      * addAllCommonStatementSupports} method instead.
118      */
119     @Deprecated
120     public CustomStatementParserBuilder addAllStatementSupports(final ModelProcessingPhase phase,
121             final Collection<StatementSupport<?, ?, ?>> statementSupports) {
122         return addAllCommonStatementSupports(phase, statementSupports);
123     }
124
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);
130         }
131         return this;
132     }
133
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());
139         }
140         return this;
141     }
142
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();
155
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);
163
164         for (final Entry<ValidationBundleType, Collection<StatementDefinition>> entry : reactorValidationBundles
165                 .entrySet()) {
166             reactorBuilder.setValidationBundle(entry.getKey(), entry.getValue());
167         }
168
169         return reactorBuilder.build();
170     }
171 }