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 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;
43 public abstract class AbstractSchemaContext implements SchemaContext {
44 protected static final Comparator<Module> REVISION_COMPARATOR = (o1, o2) -> {
45 if (o2.getRevision() == null) {
49 return o2.getRevision().compareTo(o1.getRevision());
52 protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = () -> new TreeSet<>(REVISION_COMPARATOR);
55 * @return yang sources where key is ModuleIdentifier
57 protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
60 * @return Map of modules where key is namespace
62 protected abstract SetMultimap<URI, Module> getNamespaceToModules();
65 * @return Map of modules where key is name of module
67 protected abstract SetMultimap<String, Module> getNameToModules();
70 public Set<DataSchemaNode> getDataDefinitions() {
71 final Set<DataSchemaNode> dataDefs = new HashSet<>();
72 for (Module m : getModules()) {
73 dataDefs.addAll(m.getChildNodes());
79 public Set<NotificationDefinition> getNotifications() {
80 final Set<NotificationDefinition> notifications = new HashSet<>();
81 for (Module m : getModules()) {
82 notifications.addAll(m.getNotifications());
88 public Set<RpcDefinition> getOperations() {
89 final Set<RpcDefinition> rpcs = new HashSet<>();
90 for (Module m : getModules()) {
91 rpcs.addAll(m.getRpcs());
97 public Set<ExtensionDefinition> getExtensions() {
98 final Set<ExtensionDefinition> extensions = new HashSet<>();
99 for (Module m : getModules()) {
100 extensions.addAll(m.getExtensionSchemaNodes());
106 public Module findModuleByName(final String name, final Date revision) {
107 for (final Module module : getNameToModules().get(name)) {
108 if (revision == null || revision.equals(module.getRevision())) {
117 public Set<Module> findModuleByNamespace(final URI namespace) {
118 final Set<Module> ret = getNamespaceToModules().get(namespace);
119 return ret == null ? Collections.emptySet() : ret;
123 public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
124 if (namespace == null) {
127 for (Module module : findModuleByNamespace(namespace)) {
128 if (revision == null || revision.equals(module.getRevision())) {
136 public boolean isAugmenting() {
141 public boolean isAddedByUses() {
146 public boolean isConfiguration() {
151 public ConstraintDefinition getConstraints() {
156 public QName getQName() {
157 return SchemaContext.NAME;
161 public SchemaPath getPath() {
162 return SchemaPath.ROOT;
166 public String getDescription() {
171 public String getReference() {
176 public Status getStatus() {
177 return Status.CURRENT;
181 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
182 final List<UnknownSchemaNode> result = new ArrayList<>();
183 for (Module module : getModules()) {
184 result.addAll(module.getUnknownSchemaNodes());
186 return Collections.unmodifiableList(result);
190 public Set<TypeDefinition<?>> getTypeDefinitions() {
191 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
192 for (Module module : getModules()) {
193 result.addAll(module.getTypeDefinitions());
195 return Collections.unmodifiableSet(result);
199 public Set<DataSchemaNode> getChildNodes() {
200 final Set<DataSchemaNode> result = new LinkedHashSet<>();
201 for (Module module : getModules()) {
202 result.addAll(module.getChildNodes());
204 return Collections.unmodifiableSet(result);
208 public Set<GroupingDefinition> getGroupings() {
209 final Set<GroupingDefinition> result = new LinkedHashSet<>();
210 for (Module module : getModules()) {
211 result.addAll(module.getGroupings());
213 return Collections.unmodifiableSet(result);
217 public DataSchemaNode getDataChildByName(final QName name) {
218 for (Module module : getModules()) {
219 final DataSchemaNode result = module.getDataChildByName(name);
220 if (result != null) {
228 public Set<UsesNode> getUses() {
229 return Collections.emptySet();
233 public boolean isPresenceContainer() {
238 public Set<AugmentationSchema> getAvailableAugmentations() {
239 return Collections.emptySet();
242 //FIXME: should work for submodules too
244 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
245 return getIdentifiersToSources().keySet();
249 public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
250 String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
251 return Optional.fromNullable(maybeSource);