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