2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Supplier;
12 import com.google.common.collect.SetMultimap;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.Date;
18 import java.util.HashSet;
19 import java.util.LinkedHashSet;
20 import java.util.List;
22 import java.util.NavigableSet;
24 import java.util.TreeSet;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
30 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.Status;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.UsesNode;
42 public abstract class AbstractEffectiveSchemaContext implements SchemaContext {
44 protected static final Supplier<NavigableSet<Module>> MODULE_SET_SUPPLIER = new Supplier<NavigableSet<Module>>() {
46 public NavigableSet<Module> get() {
47 return new TreeSet<>(REVISION_COMPARATOR);
51 protected static final Comparator<Module> REVISION_COMPARATOR = new Comparator<Module>() {
53 public int compare(final Module o1, final Module o2) {
54 if (o2.getRevision() == null) {
58 return o2.getRevision().compareTo(o1.getRevision());
63 * @return yang sources where key is ModuleIdentifier
65 protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
68 * @return Map of modules where key is namespace
70 protected abstract SetMultimap<URI, Module> getNamespaceToModules();
73 * @return Map of modules where key is name of module
75 protected abstract SetMultimap<String, Module> getNameToModules();
78 public Set<DataSchemaNode> getDataDefinitions() {
79 final Set<DataSchemaNode> dataDefs = new HashSet<>();
80 for (Module m : getModules()) {
81 dataDefs.addAll(m.getChildNodes());
87 public Set<NotificationDefinition> getNotifications() {
88 final Set<NotificationDefinition> notifications = new HashSet<>();
89 for (Module m : getModules()) {
90 notifications.addAll(m.getNotifications());
96 public Set<RpcDefinition> getOperations() {
97 final Set<RpcDefinition> rpcs = new HashSet<>();
98 for (Module m : getModules()) {
99 rpcs.addAll(m.getRpcs());
105 public Set<ExtensionDefinition> getExtensions() {
106 final Set<ExtensionDefinition> extensions = new HashSet<>();
107 for (Module m : getModules()) {
108 extensions.addAll(m.getExtensionSchemaNodes());
114 public Module findModuleByName(final String name, final Date revision) {
115 for (final Module module : getNameToModules().get(name)) {
116 if (revision == null || revision.equals(module.getRevision())) {
125 public Set<Module> findModuleByNamespace(final URI namespace) {
126 final Set<Module> ret = getNamespaceToModules().get(namespace);
127 return ret == null ? Collections.<Module> emptySet() : ret;
131 public Module findModuleByNamespaceAndRevision(final URI namespace,
132 final Date revision) {
133 if (namespace == null) {
136 for (Module module : findModuleByNamespace(namespace)) {
137 if (revision == null || revision.equals(module.getRevision())) {
145 public boolean isAugmenting() {
150 public boolean isAddedByUses() {
155 public boolean isConfiguration() {
160 public ConstraintDefinition getConstraints() {
165 public QName getQName() {
166 return SchemaContext.NAME;
170 public SchemaPath getPath() {
171 return SchemaPath.ROOT;
175 public String getDescription() {
180 public String getReference() {
185 public Status getStatus() {
186 return Status.CURRENT;
190 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
191 final List<UnknownSchemaNode> result = new ArrayList<>();
192 for (Module module : getModules()) {
193 result.addAll(module.getUnknownSchemaNodes());
195 return Collections.unmodifiableList(result);
199 public Set<TypeDefinition<?>> getTypeDefinitions() {
200 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
201 for (Module module : getModules()) {
202 result.addAll(module.getTypeDefinitions());
204 return Collections.unmodifiableSet(result);
208 public Set<DataSchemaNode> getChildNodes() {
209 final Set<DataSchemaNode> result = new LinkedHashSet<>();
210 for (Module module : getModules()) {
211 result.addAll(module.getChildNodes());
213 return Collections.unmodifiableSet(result);
217 public Set<GroupingDefinition> getGroupings() {
218 final Set<GroupingDefinition> result = new LinkedHashSet<>();
219 for (Module module : getModules()) {
220 result.addAll(module.getGroupings());
222 return Collections.unmodifiableSet(result);
226 public DataSchemaNode getDataChildByName(final QName name) {
227 for (Module module : getModules()) {
228 final DataSchemaNode result = module.getDataChildByName(name);
229 if (result != null) {
237 public DataSchemaNode getDataChildByName(final String name) {
238 for (Module module : getModules()) {
239 final DataSchemaNode result = module.getDataChildByName(name);
240 if (result != null) {
248 public Set<UsesNode> getUses() {
249 return Collections.emptySet();
253 public boolean isPresenceContainer() {
258 public Set<AugmentationSchema> getAvailableAugmentations() {
259 return Collections.emptySet();
262 // FIXME: should work for submodules too
264 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
265 return getIdentifiersToSources().keySet();
269 public Optional<String> getModuleSource(
270 final ModuleIdentifier moduleIdentifier) {
271 String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
272 return Optional.fromNullable(maybeSource);