Cleanup use of Guava library
[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.collect.SetMultimap;
12 import java.net.URI;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.Date;
17 import java.util.HashSet;
18 import java.util.LinkedHashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import javax.annotation.Nonnull;
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 public abstract class AbstractSchemaContext implements SchemaContext {
43     protected static final Comparator<Module> REVISION_COMPARATOR = (o1, o2) -> {
44         if (o2.getRevision() == null) {
45             return -1;
46         }
47
48         return o2.getRevision().compareTo(o1.getRevision());
49     };
50
51     protected static final TreeSet<Module> createModuleSet() {
52         return new TreeSet<>(REVISION_COMPARATOR);
53     }
54
55     /**
56      * Returns the ModuleIdentifier-to-YANG source mapping.
57      *
58      * @return yang sources where key is ModuleIdentifier
59      */
60     protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
61
62     /**
63      * Returns the namespace-to-module mapping.
64      *
65      * @return Map of modules where key is namespace
66      */
67     protected abstract SetMultimap<URI, Module> getNamespaceToModules();
68
69     /**
70      * Returns the module name-to-module mapping.
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.emptySet() : ret;
127     }
128
129     @Override
130     public boolean isAugmenting() {
131         return false;
132     }
133
134     @Override
135     public boolean isAddedByUses() {
136         return false;
137     }
138
139     @Override
140     public boolean isConfiguration() {
141         return false;
142     }
143
144     @Override
145     public ConstraintDefinition getConstraints() {
146         return null;
147     }
148
149     @Nonnull
150     @Override
151     public QName getQName() {
152         return SchemaContext.NAME;
153     }
154
155     @Nonnull
156     @Override
157     public SchemaPath getPath() {
158         return SchemaPath.ROOT;
159     }
160
161     @Override
162     public String getDescription() {
163         return null;
164     }
165
166     @Override
167     public String getReference() {
168         return null;
169     }
170
171     @Nonnull
172     @Override
173     public Status getStatus() {
174         return Status.CURRENT;
175     }
176
177     @Nonnull
178     @Override
179     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
180         final List<UnknownSchemaNode> result = new ArrayList<>();
181         for (Module module : getModules()) {
182             result.addAll(module.getUnknownSchemaNodes());
183         }
184         return Collections.unmodifiableList(result);
185     }
186
187     @Override
188     public Set<TypeDefinition<?>> getTypeDefinitions() {
189         final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
190         for (Module module : getModules()) {
191             result.addAll(module.getTypeDefinitions());
192         }
193         return Collections.unmodifiableSet(result);
194     }
195
196     @Override
197     public Set<DataSchemaNode> getChildNodes() {
198         final Set<DataSchemaNode> result = new LinkedHashSet<>();
199         for (Module module : getModules()) {
200             result.addAll(module.getChildNodes());
201         }
202         return Collections.unmodifiableSet(result);
203     }
204
205     @Override
206     public Set<GroupingDefinition> getGroupings() {
207         final Set<GroupingDefinition> result = new LinkedHashSet<>();
208         for (Module module : getModules()) {
209             result.addAll(module.getGroupings());
210         }
211         return Collections.unmodifiableSet(result);
212     }
213
214     @Override
215     public DataSchemaNode getDataChildByName(final QName name) {
216         for (Module module : getModules()) {
217             final DataSchemaNode result = module.getDataChildByName(name);
218             if (result != null) {
219                 return result;
220             }
221         }
222         return null;
223     }
224
225     @Override
226     public Set<UsesNode> getUses() {
227         return Collections.emptySet();
228     }
229
230     @Override
231     public boolean isPresenceContainer() {
232         return false;
233     }
234
235     @Override
236     public Set<AugmentationSchema> getAvailableAugmentations() {
237         return Collections.emptySet();
238     }
239
240     //FIXME: should work for submodules too
241     @Override
242     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
243         return getIdentifiersToSources().keySet();
244     }
245
246     @Override
247     public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
248         return Optional.ofNullable(getIdentifiersToSources().get(moduleIdentifier));
249     }
250 }