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
9 package org.opendaylight.yangtools.yang.model.util;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Supplier;
13 import com.google.common.collect.SetMultimap;
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;
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;
44 public abstract class AbstractSchemaContext implements SchemaContext {
45 protected static final Comparator<Module> REVISION_COMPARATOR = (o1, o2) -> {
46 if (o2.getRevision() == null) {
50 return o2.getRevision().compareTo(o1.getRevision());
53 protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = () -> new TreeSet<>(REVISION_COMPARATOR);
56 * Returns the ModuleIdentifier-to-YANG source mapping.
58 * @return yang sources where key is ModuleIdentifier
60 protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
63 * Returns the namespace-to-module mapping.
65 * @return Map of modules where key is namespace
67 protected abstract SetMultimap<URI, Module> getNamespaceToModules();
70 * Returns the module name-to-module mapping.
72 * @return Map of modules where key is name of module
74 protected abstract SetMultimap<String, Module> getNameToModules();
77 public Set<DataSchemaNode> getDataDefinitions() {
78 final Set<DataSchemaNode> dataDefs = new HashSet<>();
79 for (Module m : getModules()) {
80 dataDefs.addAll(m.getChildNodes());
86 public Set<NotificationDefinition> getNotifications() {
87 final Set<NotificationDefinition> notifications = new HashSet<>();
88 for (Module m : getModules()) {
89 notifications.addAll(m.getNotifications());
95 public Set<RpcDefinition> getOperations() {
96 final Set<RpcDefinition> rpcs = new HashSet<>();
97 for (Module m : getModules()) {
98 rpcs.addAll(m.getRpcs());
104 public Set<ExtensionDefinition> getExtensions() {
105 final Set<ExtensionDefinition> extensions = new HashSet<>();
106 for (Module m : getModules()) {
107 extensions.addAll(m.getExtensionSchemaNodes());
113 public Module findModuleByName(final String name, final Date revision) {
114 for (final Module module : getNameToModules().get(name)) {
115 if (revision == null || revision.equals(module.getRevision())) {
124 public Set<Module> findModuleByNamespace(final URI namespace) {
125 final Set<Module> ret = getNamespaceToModules().get(namespace);
126 return ret == null ? Collections.emptySet() : ret;
130 public boolean isAugmenting() {
135 public boolean isAddedByUses() {
140 public boolean isConfiguration() {
145 public ConstraintDefinition getConstraints() {
151 public QName getQName() {
152 return SchemaContext.NAME;
157 public SchemaPath getPath() {
158 return SchemaPath.ROOT;
162 public String getDescription() {
167 public String getReference() {
173 public Status getStatus() {
174 return Status.CURRENT;
179 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
180 final List<UnknownSchemaNode> result = new ArrayList<>();
181 for (Module module : getModules()) {
182 result.addAll(module.getUnknownSchemaNodes());
184 return Collections.unmodifiableList(result);
188 public Set<TypeDefinition<?>> getTypeDefinitions() {
189 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
190 for (Module module : getModules()) {
191 result.addAll(module.getTypeDefinitions());
193 return Collections.unmodifiableSet(result);
197 public Set<DataSchemaNode> getChildNodes() {
198 final Set<DataSchemaNode> result = new LinkedHashSet<>();
199 for (Module module : getModules()) {
200 result.addAll(module.getChildNodes());
202 return Collections.unmodifiableSet(result);
206 public Set<GroupingDefinition> getGroupings() {
207 final Set<GroupingDefinition> result = new LinkedHashSet<>();
208 for (Module module : getModules()) {
209 result.addAll(module.getGroupings());
211 return Collections.unmodifiableSet(result);
215 public DataSchemaNode getDataChildByName(final QName name) {
216 for (Module module : getModules()) {
217 final DataSchemaNode result = module.getDataChildByName(name);
218 if (result != null) {
226 public Set<UsesNode> getUses() {
227 return Collections.emptySet();
231 public boolean isPresenceContainer() {
236 public Set<AugmentationSchema> getAvailableAugmentations() {
237 return Collections.emptySet();
240 //FIXME: should work for submodules too
242 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
243 return getIdentifiersToSources().keySet();
247 public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
248 String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
249 return Optional.fromNullable(maybeSource);