Remove RevisionSourceIdentifier
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SchemaContextFactoryConfiguration.java
1 /*
2  * Copyright (c) 2017 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.model.repo.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSetMultimap;
16 import com.google.common.collect.SetMultimap;
17 import java.util.Objects;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.concepts.Mutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26
27 /**
28  * SchemaContextFactory configuration class. It currently supports the following options to be set:
29  * <ul>
30  *   <li>schema source filter</li>
31  *   <li>statement parser mode</li>
32  *   <li>supported features</li>
33  *   <li>supported deviations</li>
34  * </ul>
35  */
36 @Beta
37 public final class SchemaContextFactoryConfiguration implements Immutable {
38     private static final @NonNull SchemaContextFactoryConfiguration DEFAULT_CONFIGURATION = new Builder().build();
39
40     private final @NonNull SchemaSourceFilter filter;
41     private final @NonNull StatementParserMode statementParserMode;
42     private final @Nullable ImmutableSet<QName> supportedFeatures;
43     private final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
44
45     private SchemaContextFactoryConfiguration(final @NonNull SchemaSourceFilter filter,
46             final @NonNull StatementParserMode statementParserMode,
47             final @Nullable ImmutableSet<QName> supportedFeatures,
48             final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
49         this.filter = requireNonNull(filter);
50         this.statementParserMode = requireNonNull(statementParserMode);
51         this.supportedFeatures = supportedFeatures;
52         this.modulesDeviatedByModules = modulesDeviatedByModules;
53     }
54
55     public @NonNull SchemaSourceFilter getSchemaSourceFilter() {
56         return filter;
57     }
58
59     public @NonNull StatementParserMode getStatementParserMode() {
60         return statementParserMode;
61     }
62
63     public Optional<Set<QName>> getSupportedFeatures() {
64         return Optional.ofNullable(supportedFeatures);
65     }
66
67     public Optional<SetMultimap<QNameModule, QNameModule>> getModulesDeviatedByModules() {
68         return Optional.ofNullable(modulesDeviatedByModules);
69     }
70
71     public static @NonNull SchemaContextFactoryConfiguration getDefault() {
72         return DEFAULT_CONFIGURATION;
73     }
74
75     public static @NonNull Builder builder() {
76         return new Builder();
77     }
78
79     @Override
80     public int hashCode() {
81         return Objects.hash(filter, statementParserMode, supportedFeatures, modulesDeviatedByModules);
82     }
83
84     @Override
85     public boolean equals(final Object obj) {
86         if (this == obj) {
87             return true;
88         }
89         if (!(obj instanceof SchemaContextFactoryConfiguration)) {
90             return false;
91         }
92         final SchemaContextFactoryConfiguration other = (SchemaContextFactoryConfiguration) obj;
93         return filter.equals(other.filter) && statementParserMode.equals(other.statementParserMode)
94                 && Objects.equals(supportedFeatures, other.supportedFeatures)
95                 && Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
96     }
97
98     @Override
99     public String toString() {
100         return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
101                 .add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
102                 .add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
103     }
104
105     public static class Builder implements Mutable {
106         private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
107         private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
108         private ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
109         private ImmutableSet<QName> supportedFeatures;
110
111         /**
112          * Set schema source filter which will filter available schema sources using the provided filter.
113          *
114          * @param filter schema source filter which acts as the gating function before a schema source is considered
115          *               by the factory for inclusion in the SchemaContext it produces.
116          * @return this builder
117          */
118         public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
119             this.filter = requireNonNull(filter);
120             return this;
121         }
122
123         /**
124          * Set YANG statement parser mode.
125          *
126          * @param statementParserMode mode of yang statement parser
127          * @return this builder
128          */
129         public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
130             this.statementParserMode = requireNonNull(statementParserMode);
131             return this;
132         }
133
134         /**
135          * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
136          *
137          * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
138          *                          features encountered will be supported.
139          * @return this builder
140          */
141         public @NonNull Builder setSupportedFeatures(final Set<QName> supportedFeatures) {
142             this.supportedFeatures = supportedFeatures != null ? ImmutableSet.copyOf(supportedFeatures) : null;
143             return this;
144         }
145
146         /**
147          * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
148          * denotes a module which can be deviated by the modules in the Map value.
149          *
150          * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
151          *                                 (Map values) in the final SchemaContext. If the map is empty, no deviations
152          *                                 encountered will be supported. If the map is null, all deviations will be
153          *                                 applied.
154          * @return this builder
155          */
156         public @NonNull Builder setModulesDeviatedByModules(
157                 final @Nullable SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
158             this.modulesDeviatedByModules = modulesDeviatedByModules != null
159                     ? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
160             return this;
161         }
162
163         /**
164          * Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
165          *
166          * @return A new {@link SchemaContextFactoryConfiguration}
167          */
168         public @NonNull SchemaContextFactoryConfiguration build() {
169             return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
170                     modulesDeviatedByModules);
171         }
172     }
173 }