2 * Copyright (c) 2016 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.restconf.nb.rfc8040.references;
10 import java.lang.ref.SoftReference;
12 import java.util.Collection;
13 import java.util.Date;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
16 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 * This class creates {@link SoftReference} of actual {@link EffectiveModelContext}
25 * object and even if the {@link SchemaContext} changes, this will be sticks
26 * reference to the old {@link SchemaContext} and provides work with the old
27 * {@link EffectiveModelContext}.
30 public final class SchemaContextRef {
32 private final SoftReference<EffectiveModelContext> schemaContextRef;
35 * Create {@link SoftReference} of actual {@link EffectiveModelContext}.
37 * @param schemaContext
38 * actual {@link EffectiveModelContext}
40 public SchemaContextRef(final EffectiveModelContext schemaContext) {
41 this.schemaContextRef = new SoftReference<>(schemaContext);
45 * Get {@link EffectiveModelContext} from reference.
47 * @return {@link EffectiveModelContext}
49 public EffectiveModelContext get() {
50 return this.schemaContextRef.get();
54 * Get all modules like {@link Collection} of {@link Module} from {@link SchemaContext}.
56 * @return {@link Collection} of {@link Module}
58 public Collection<? extends Module> getModules() {
59 return get().getModules();
63 * Get {@link Module} by ietf-restconf qname from
64 * {@link Rfc8040.RestconfModule}.
66 * @return {@link Module}
68 public Module getRestconfModule() {
69 return this.findModuleByNamespaceAndRevision(Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getNamespace(),
70 Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
74 * Find {@link Module} in {@link SchemaContext} by {@link URI} and
81 * @return {@link Module}
83 public Module findModuleByNamespaceAndRevision(final URI namespace, final Optional<Revision> revision) {
84 return this.get().findModule(namespace, revision).orElse(null);
88 * Find {@link Module} in {@link SchemaContext} of {@link DOMMountPoint} by
89 * {@link QName} of {@link Module}.
94 * {@link QName} of module
95 * @return {@link Module}
97 public Module findModuleInMountPointByQName(final DOMMountPoint mountPoint, final QName moduleQname) {
98 final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
99 return schemaContext == null ? null
100 : schemaContext.findModule(moduleQname.getLocalName(), moduleQname.getRevision()).orElse(null);
104 * Find {@link Module} in {@link SchemaContext} by {@link QName}.
107 * {@link QName} of module
108 * @return {@link Module}
110 public Module findModuleByQName(final QName moduleQname) {
111 return this.findModuleByNameAndRevision(moduleQname.getLocalName(), moduleQname.getRevision());
115 * Find {@link Module} in {@link SchemaContext} by {@link String} localName
116 * and {@link Date} revision.
119 * local name of module
122 * @return {@link Module}
124 public Module findModuleByNameAndRevision(final String localName, final Optional<Revision> revision) {
125 return this.get().findModule(localName, revision).orElse(null);