Encapsulate regexes in a non-capturing group
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.yangtools.yang.model.util;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Supplier;
13 import com.google.common.collect.SetMultimap;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.Date;
19 import java.util.HashSet;
20 import java.util.LinkedHashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
31 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.Status;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.UsesNode;
42
43
44 public abstract class AbstractSchemaContext implements SchemaContext {
45     protected static final Comparator<Module> REVISION_COMPARATOR = (o1, o2) -> {
46         if (o2.getRevision() == null) {
47             return -1;
48         }
49
50         return o2.getRevision().compareTo(o1.getRevision());
51     };
52
53     protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = () -> new TreeSet<>(REVISION_COMPARATOR);
54
55     /**
56      * @return yang sources where key is ModuleIdentifier
57      */
58     protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
59
60     /**
61      * @return Map of modules where key is namespace
62      */
63     protected abstract SetMultimap<URI, Module> getNamespaceToModules();
64
65     /**
66      * @return Map of modules where key is name of module
67      */
68     protected abstract SetMultimap<String, Module> getNameToModules();
69
70     @Override
71     public Set<DataSchemaNode> getDataDefinitions() {
72         final Set<DataSchemaNode> dataDefs = new HashSet<>();
73         for (Module m : getModules()) {
74             dataDefs.addAll(m.getChildNodes());
75         }
76         return dataDefs;
77     }
78
79     @Override
80     public Set<NotificationDefinition> getNotifications() {
81         final Set<NotificationDefinition> notifications = new HashSet<>();
82         for (Module m : getModules()) {
83             notifications.addAll(m.getNotifications());
84         }
85         return notifications;
86     }
87
88     @Override
89     public Set<RpcDefinition> getOperations() {
90         final Set<RpcDefinition> rpcs = new HashSet<>();
91         for (Module m : getModules()) {
92             rpcs.addAll(m.getRpcs());
93         }
94         return rpcs;
95     }
96
97     @Override
98     public Set<ExtensionDefinition> getExtensions() {
99         final Set<ExtensionDefinition> extensions = new HashSet<>();
100         for (Module m : getModules()) {
101             extensions.addAll(m.getExtensionSchemaNodes());
102         }
103         return extensions;
104     }
105
106     @Override
107     public Module findModuleByName(final String name, final Date revision) {
108         for (final Module module : getNameToModules().get(name)) {
109             if (revision == null || revision.equals(module.getRevision())) {
110                 return module;
111             }
112         }
113
114         return null;
115     }
116
117     @Override
118     public Set<Module> findModuleByNamespace(final URI namespace) {
119         final Set<Module> ret = getNamespaceToModules().get(namespace);
120         return ret == null ? Collections.emptySet() : ret;
121     }
122
123     @Override
124     public boolean isAugmenting() {
125         return false;
126     }
127
128     @Override
129     public boolean isAddedByUses() {
130         return false;
131     }
132
133     @Override
134     public boolean isConfiguration() {
135         return false;
136     }
137
138     @Override
139     public ConstraintDefinition getConstraints() {
140         return null;
141     }
142
143     @Nonnull
144     @Override
145     public QName getQName() {
146         return SchemaContext.NAME;
147     }
148
149     @Nonnull
150     @Override
151     public SchemaPath getPath() {
152         return SchemaPath.ROOT;
153     }
154
155     @Override
156     public String getDescription() {
157         return null;
158     }
159
160     @Override
161     public String getReference() {
162         return null;
163     }
164
165     @Nonnull
166     @Override
167     public Status getStatus() {
168         return Status.CURRENT;
169     }
170
171     @Nonnull
172     @Override
173     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
174         final List<UnknownSchemaNode> result = new ArrayList<>();
175         for (Module module : getModules()) {
176             result.addAll(module.getUnknownSchemaNodes());
177         }
178         return Collections.unmodifiableList(result);
179     }
180
181     @Override
182     public Set<TypeDefinition<?>> getTypeDefinitions() {
183         final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
184         for (Module module : getModules()) {
185             result.addAll(module.getTypeDefinitions());
186         }
187         return Collections.unmodifiableSet(result);
188     }
189
190     @Override
191     public Set<DataSchemaNode> getChildNodes() {
192         final Set<DataSchemaNode> result = new LinkedHashSet<>();
193         for (Module module : getModules()) {
194             result.addAll(module.getChildNodes());
195         }
196         return Collections.unmodifiableSet(result);
197     }
198
199     @Override
200     public Set<GroupingDefinition> getGroupings() {
201         final Set<GroupingDefinition> result = new LinkedHashSet<>();
202         for (Module module : getModules()) {
203             result.addAll(module.getGroupings());
204         }
205         return Collections.unmodifiableSet(result);
206     }
207
208     @Override
209     public DataSchemaNode getDataChildByName(final QName name) {
210         for (Module module : getModules()) {
211             final DataSchemaNode result = module.getDataChildByName(name);
212             if (result != null) {
213                 return result;
214             }
215         }
216         return null;
217     }
218
219     @Override
220     public Set<UsesNode> getUses() {
221         return Collections.emptySet();
222     }
223
224     @Override
225     public boolean isPresenceContainer() {
226         return false;
227     }
228
229     @Override
230     public Set<AugmentationSchema> getAvailableAugmentations() {
231         return Collections.emptySet();
232     }
233
234     //FIXME: should work for submodules too
235     @Override
236     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
237         return getIdentifiersToSources().keySet();
238     }
239
240     @Override
241     public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
242         String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
243         return Optional.fromNullable(maybeSource);
244     }
245
246 }