Bug 6414: Fixed DataNodeIterator's traverseModule method
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractSchemaContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.model.util;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Supplier;
13 import com.google.common.collect.SetMultimap;
14 import java.net.URI;
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;
22 import java.util.Map;
23 import java.util.Set;
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;
41
42
43 public abstract class AbstractSchemaContext implements SchemaContext {
44
45     protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = new Supplier<TreeSet<Module>>() {
46         @Override
47         public TreeSet<Module> get() {
48             return new TreeSet<>(REVISION_COMPARATOR);
49         }
50     };
51
52     protected static final Comparator<Module> REVISION_COMPARATOR = new Comparator<Module>() {
53         @Override
54         public int compare(final Module o1, final Module o2) {
55             if (o2.getRevision() == null) {
56                 return -1;
57             }
58
59             return o2.getRevision().compareTo(o1.getRevision());
60         }
61     };
62
63     /**
64      * @return yang sources where key is ModuleIdentifier
65      */
66     protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
67
68     /**
69      * @return Map of modules where key is namespace
70      */
71     protected abstract SetMultimap<URI, Module> getNamespaceToModules();
72
73     /**
74      * @return Map of modules where key is name of module
75      */
76     protected abstract SetMultimap<String, Module> getNameToModules();
77
78     @Override
79     public Set<DataSchemaNode> getDataDefinitions() {
80         final Set<DataSchemaNode> dataDefs = new HashSet<>();
81         for (Module m : getModules()) {
82             dataDefs.addAll(m.getChildNodes());
83         }
84         return dataDefs;
85     }
86
87     @Override
88     public Set<NotificationDefinition> getNotifications() {
89         final Set<NotificationDefinition> notifications = new HashSet<>();
90         for (Module m : getModules()) {
91             notifications.addAll(m.getNotifications());
92         }
93         return notifications;
94     }
95
96     @Override
97     public Set<RpcDefinition> getOperations() {
98         final Set<RpcDefinition> rpcs = new HashSet<>();
99         for (Module m : getModules()) {
100             rpcs.addAll(m.getRpcs());
101         }
102         return rpcs;
103     }
104
105     @Override
106     public Set<ExtensionDefinition> getExtensions() {
107         final Set<ExtensionDefinition> extensions = new HashSet<>();
108         for (Module m : getModules()) {
109             extensions.addAll(m.getExtensionSchemaNodes());
110         }
111         return extensions;
112     }
113
114     @Override
115     public Module findModuleByName(final String name, final Date revision) {
116         for (final Module module : getNameToModules().get(name)) {
117             if (revision == null || revision.equals(module.getRevision())) {
118                 return module;
119             }
120         }
121
122         return null;
123     }
124
125     @Override
126     public Set<Module> findModuleByNamespace(final URI namespace) {
127         final Set<Module> ret = getNamespaceToModules().get(namespace);
128         return ret == null ? Collections.<Module>emptySet() : ret;
129     }
130
131     @Override
132     public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
133         if (namespace == null) {
134             return null;
135         }
136         for (Module module : findModuleByNamespace(namespace)) {
137             if (revision == null || revision.equals(module.getRevision())) {
138                 return module;
139             }
140         }
141         return null;
142     }
143
144     @Override
145     public boolean isAugmenting() {
146         return false;
147     }
148
149     @Override
150     public boolean isAddedByUses() {
151         return false;
152     }
153
154     @Override
155     public boolean isConfiguration() {
156         return false;
157     }
158
159     @Override
160     public ConstraintDefinition getConstraints() {
161         return null;
162     }
163
164     @Override
165     public QName getQName() {
166         return SchemaContext.NAME;
167     }
168
169     @Override
170     public SchemaPath getPath() {
171         return SchemaPath.ROOT;
172     }
173
174     @Override
175     public String getDescription() {
176         return null;
177     }
178
179     @Override
180     public String getReference() {
181         return null;
182     }
183
184     @Override
185     public Status getStatus() {
186         return Status.CURRENT;
187     }
188
189     @Override
190     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
191         final List<UnknownSchemaNode> result = new ArrayList<>();
192         for (Module module : getModules()) {
193             result.addAll(module.getUnknownSchemaNodes());
194         }
195         return Collections.unmodifiableList(result);
196     }
197
198     @Override
199     public Set<TypeDefinition<?>> getTypeDefinitions() {
200         final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
201         for (Module module : getModules()) {
202             result.addAll(module.getTypeDefinitions());
203         }
204         return Collections.unmodifiableSet(result);
205     }
206
207     @Override
208     public Set<DataSchemaNode> getChildNodes() {
209         final Set<DataSchemaNode> result = new LinkedHashSet<>();
210         for (Module module : getModules()) {
211             result.addAll(module.getChildNodes());
212         }
213         return Collections.unmodifiableSet(result);
214     }
215
216     @Override
217     public Set<GroupingDefinition> getGroupings() {
218         final Set<GroupingDefinition> result = new LinkedHashSet<>();
219         for (Module module : getModules()) {
220             result.addAll(module.getGroupings());
221         }
222         return Collections.unmodifiableSet(result);
223     }
224
225     @Override
226     public DataSchemaNode getDataChildByName(final QName name) {
227         for (Module module : getModules()) {
228             final DataSchemaNode result = module.getDataChildByName(name);
229             if (result != null) {
230                 return result;
231             }
232         }
233         return null;
234     }
235
236     @Override
237     public DataSchemaNode getDataChildByName(final String name) {
238         for (Module module : getModules()) {
239             final DataSchemaNode result = module.getDataChildByName(name);
240             if (result != null) {
241                 return result;
242             }
243         }
244         return null;
245     }
246
247     @Override
248     public Set<UsesNode> getUses() {
249         return Collections.emptySet();
250     }
251
252     @Override
253     public boolean isPresenceContainer() {
254         return false;
255     }
256
257     @Override
258     public Set<AugmentationSchema> getAvailableAugmentations() {
259         return Collections.emptySet();
260     }
261
262     //FIXME: should work for submodules too
263     @Override
264     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
265         return getIdentifiersToSources().keySet();
266     }
267
268     @Override
269     public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
270         String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
271         return Optional.fromNullable(maybeSource);
272     }
273
274 }