8120518cf3f554b864366d854165396518e74fbd
[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 Set<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 Set<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         return this == obj || obj instanceof SchemaContextFactoryConfiguration other && filter.equals(other.filter)
87             && statementParserMode.equals(other.statementParserMode)
88             && equals(supportedFeatures, other.supportedFeatures)
89             && Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
90     }
91
92     // This a bit of a dance to deal with FeatureSet not conforming to Set.equals()
93     private static boolean equals(final @Nullable Set<QName> thisFeatures, final @Nullable Set<QName> otherFeatures) {
94         if (thisFeatures == otherFeatures) {
95             return true;
96         }
97         if (thisFeatures == null || otherFeatures == null) {
98             return false;
99         }
100         if (thisFeatures instanceof FeatureSet) {
101             return thisFeatures.equals(otherFeatures);
102         }
103         if (otherFeatures instanceof FeatureSet) {
104             return otherFeatures.equals(thisFeatures);
105         }
106         return thisFeatures.equals(otherFeatures);
107     }
108
109     @Override
110     public String toString() {
111         return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
112                 .add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
113                 .add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
114     }
115
116     public static class Builder implements Mutable {
117         private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
118         private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
119         private ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
120         private Set<QName> supportedFeatures;
121
122         /**
123          * Set schema source filter which will filter available schema sources using the provided filter.
124          *
125          * @param filter schema source filter which acts as the gating function before a schema source is considered
126          *               by the factory for inclusion in the SchemaContext it produces.
127          * @return this builder
128          */
129         public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
130             this.filter = requireNonNull(filter);
131             return this;
132         }
133
134         /**
135          * Set YANG statement parser mode.
136          *
137          * @param statementParserMode mode of yang statement parser
138          * @return this builder
139          */
140         public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
141             this.statementParserMode = requireNonNull(statementParserMode);
142             return this;
143         }
144
145         /**
146          * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
147          *
148          * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
149          *                          features encountered will be supported.
150          * @return this builder
151          */
152         public @NonNull Builder setSupportedFeatures(final Set<QName> supportedFeatures) {
153             if (supportedFeatures == null || supportedFeatures instanceof FeatureSet) {
154                 this.supportedFeatures = supportedFeatures;
155             } else {
156                 this.supportedFeatures = ImmutableSet.copyOf(supportedFeatures);
157             }
158             return this;
159         }
160
161         /**
162          * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
163          * denotes a module which can be deviated by the modules in the Map value.
164          *
165          * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
166          *                                 (Map values) in the final SchemaContext. If the map is empty, no deviations
167          *                                 encountered will be supported. If the map is null, all deviations will be
168          *                                 applied.
169          * @return this builder
170          */
171         public @NonNull Builder setModulesDeviatedByModules(
172                 final @Nullable SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
173             this.modulesDeviatedByModules = modulesDeviatedByModules != null
174                     ? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
175             return this;
176         }
177
178         /**
179          * Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
180          *
181          * @return A new {@link SchemaContextFactoryConfiguration}
182          */
183         public @NonNull SchemaContextFactoryConfiguration build() {
184             return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
185                     modulesDeviatedByModules);
186         }
187     }
188 }