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 * @return yang sources where key is ModuleIdentifier
58 protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
61 * @return Map of modules where key is namespace
63 protected abstract SetMultimap<URI, Module> getNamespaceToModules();
66 * @return Map of modules where key is name of module
68 protected abstract SetMultimap<String, Module> getNameToModules();
71 public Set<DataSchemaNode> getDataDefinitions() {
72 final Set<DataSchemaNode> dataDefs = new HashSet<>();
73 for (Module m : getModules()) {
74 dataDefs.addAll(m.getChildNodes());
80 public Set<NotificationDefinition> getNotifications() {
81 final Set<NotificationDefinition> notifications = new HashSet<>();
82 for (Module m : getModules()) {
83 notifications.addAll(m.getNotifications());
89 public Set<RpcDefinition> getOperations() {
90 final Set<RpcDefinition> rpcs = new HashSet<>();
91 for (Module m : getModules()) {
92 rpcs.addAll(m.getRpcs());
98 public Set<ExtensionDefinition> getExtensions() {
99 final Set<ExtensionDefinition> extensions = new HashSet<>();
100 for (Module m : getModules()) {
101 extensions.addAll(m.getExtensionSchemaNodes());
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())) {
118 public Set<Module> findModuleByNamespace(final URI namespace) {
119 final Set<Module> ret = getNamespaceToModules().get(namespace);
120 return ret == null ? Collections.emptySet() : ret;
124 public boolean isAugmenting() {
129 public boolean isAddedByUses() {
134 public boolean isConfiguration() {
139 public ConstraintDefinition getConstraints() {
145 public QName getQName() {
146 return SchemaContext.NAME;
151 public SchemaPath getPath() {
152 return SchemaPath.ROOT;
156 public String getDescription() {
161 public String getReference() {
167 public Status getStatus() {
168 return Status.CURRENT;
173 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
174 final List<UnknownSchemaNode> result = new ArrayList<>();
175 for (Module module : getModules()) {
176 result.addAll(module.getUnknownSchemaNodes());
178 return Collections.unmodifiableList(result);
182 public Set<TypeDefinition<?>> getTypeDefinitions() {
183 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
184 for (Module module : getModules()) {
185 result.addAll(module.getTypeDefinitions());
187 return Collections.unmodifiableSet(result);
191 public Set<DataSchemaNode> getChildNodes() {
192 final Set<DataSchemaNode> result = new LinkedHashSet<>();
193 for (Module module : getModules()) {
194 result.addAll(module.getChildNodes());
196 return Collections.unmodifiableSet(result);
200 public Set<GroupingDefinition> getGroupings() {
201 final Set<GroupingDefinition> result = new LinkedHashSet<>();
202 for (Module module : getModules()) {
203 result.addAll(module.getGroupings());
205 return Collections.unmodifiableSet(result);
209 public DataSchemaNode getDataChildByName(final QName name) {
210 for (Module module : getModules()) {
211 final DataSchemaNode result = module.getDataChildByName(name);
212 if (result != null) {
220 public Set<UsesNode> getUses() {
221 return Collections.emptySet();
225 public boolean isPresenceContainer() {
230 public Set<AugmentationSchema> getAvailableAugmentations() {
231 return Collections.emptySet();
234 //FIXME: should work for submodules too
236 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
237 return getIdentifiersToSources().keySet();
241 public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
242 String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
243 return Optional.fromNullable(maybeSource);