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