2 * Copyright (c) 2013 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.impl;
10 import com.google.common.base.Optional;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
16 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
20 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.HashSet;
34 import java.util.LinkedHashSet;
35 import java.util.List;
38 import java.util.TreeMap;
40 final class SchemaContextImpl implements SchemaContext {
41 private final Set<Module> modules;
42 private final Map<ModuleIdentifier, String> identifiersToSources;
44 SchemaContextImpl(final Set<Module> modules, Map<ModuleIdentifier, String> identifiersToSources) {
45 this.modules = modules;
46 this.identifiersToSources = identifiersToSources;
50 public Set<DataSchemaNode> getDataDefinitions() {
51 final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();
52 for (Module m : modules) {
53 dataDefs.addAll(m.getChildNodes());
59 public Set<Module> getModules() {
60 List<Module> sorted = ModuleDependencySort.sort(modules.toArray(new Module[modules.size()]));
61 return new LinkedHashSet<Module>(sorted);
65 public Set<NotificationDefinition> getNotifications() {
66 final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
67 for (Module m : modules) {
68 notifications.addAll(m.getNotifications());
74 public Set<RpcDefinition> getOperations() {
75 final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
76 for (Module m : modules) {
77 rpcs.addAll(m.getRpcs());
83 public Set<ExtensionDefinition> getExtensions() {
84 final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();
85 for (Module m : modules) {
86 extensions.addAll(m.getExtensionSchemaNodes());
92 public Module findModuleByName(final String name, final Date revision) {
94 for (final Module module : modules) {
95 if (revision == null) {
96 if (module.getName().equals(name)) {
99 } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
108 public Set<Module> findModuleByNamespace(final URI namespace) {
109 final Set<Module> ret = new HashSet<Module>();
110 if (namespace != null) {
111 for (final Module module : modules) {
112 if (module.getNamespace().equals(namespace)) {
121 public Module findModuleByNamespaceAndRevision(URI namespace, Date revision) {
122 if (namespace != null) {
123 Set<Module> modules = findModuleByNamespace(namespace);
125 if (revision == null) {
126 TreeMap<Date, Module> map = new TreeMap<Date, Module>();
127 for (Module module : modules) {
128 map.put(module.getRevision(), module);
133 return map.lastEntry().getValue();
135 for (Module module : modules) {
136 if (module.getRevision().equals(revision)) {
146 public boolean isAugmenting() {
151 public boolean isAddedByUses() {
156 public boolean isConfiguration() {
161 public ConstraintDefinition getConstraints() {
166 public QName getQName() {
167 return SchemaContext.NAME;
171 public SchemaPath getPath() {
176 public String getDescription() {
181 public String getReference() {
186 public Status getStatus() {
187 return Status.CURRENT;
191 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
192 final List<UnknownSchemaNode> result = new ArrayList<>();
193 for (Module module : modules) {
194 result.addAll(module.getUnknownSchemaNodes());
196 return Collections.unmodifiableList(result);
200 public Set<TypeDefinition<?>> getTypeDefinitions() {
201 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
202 for (Module module : modules) {
203 result.addAll(module.getTypeDefinitions());
205 return Collections.unmodifiableSet(result);
209 public Set<DataSchemaNode> getChildNodes() {
210 final Set<DataSchemaNode> result = new LinkedHashSet<>();
211 for (Module module : modules) {
212 result.addAll(module.getChildNodes());
214 return Collections.unmodifiableSet(result);
218 public Set<GroupingDefinition> getGroupings() {
219 final Set<GroupingDefinition> result = new LinkedHashSet<>();
220 for (Module module : modules) {
221 result.addAll(module.getGroupings());
223 return Collections.unmodifiableSet(result);
227 public DataSchemaNode getDataChildByName(QName name) {
228 DataSchemaNode result = null;
229 for (Module module : modules) {
230 result = module.getDataChildByName(name);
231 if (result != null) {
239 public DataSchemaNode getDataChildByName(String name) {
240 DataSchemaNode result = null;
241 for (Module module : modules) {
242 result = module.getDataChildByName(name);
243 if (result != null) {
251 public Set<UsesNode> getUses() {
252 return Collections.emptySet();
256 public boolean isPresenceContainer() {
261 public Set<AugmentationSchema> getAvailableAugmentations() {
262 return Collections.emptySet();
265 //FIXME: should work for submodules too
267 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
268 return identifiersToSources.keySet();
272 public Optional<String> getModuleSource(ModuleIdentifier moduleIdentifier) {
273 String maybeSource = identifiersToSources.get(moduleIdentifier);
274 return Optional.fromNullable(maybeSource);