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.Date;
13 import java.util.Optional;
15 import org.opendaylight.controller.md.sal.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.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 * This class creates {@link SoftReference} of actual {@link SchemaContext}
24 * object and even if the {@link SchemaContext} changes, this will be sticks
25 * reference to the old {@link SchemaContext} and provides work with the old
26 * {@link SchemaContext}.
29 public final class SchemaContextRef {
31 private final SoftReference<SchemaContext> schemaContextRef;
34 * Create {@link SoftReference} of actual {@link SchemaContext}.
36 * @param schemaContext
37 * actual {@link SchemaContext}
39 public SchemaContextRef(final SchemaContext schemaContext) {
40 this.schemaContextRef = new SoftReference<>(schemaContext);
44 * Get {@link SchemaContext} from reference.
46 * @return {@link SchemaContext}
48 public SchemaContext get() {
49 return this.schemaContextRef.get();
53 * Get all modules like {@link Set} of {@link Module} from
54 * {@link SchemaContext}.
56 * @return {@link Set} of {@link Module}
58 public Set<Module> getModules() {
59 return get().getModules();
63 * Get all modules like {@link Set} of {@link Module} from
64 * {@link SchemaContext} of {@link DOMMountPoint}.
69 * @return {@link Set} of {@link Module}
71 public Set<Module> getModules(final DOMMountPoint mountPoint) {
72 final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
73 return schemaContext == null ? null : schemaContext.getModules();
77 * Get {@link Module} by ietf-restconf qname from
78 * {@link Rfc8040.RestconfModule}.
80 * @return {@link Module}
82 public Module getRestconfModule() {
83 return this.findModuleByNamespaceAndRevision(Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getNamespace(),
84 Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
88 * Find {@link Module} in {@link SchemaContext} by {@link URI} and
95 * @return {@link Module}
97 public Module findModuleByNamespaceAndRevision(final URI namespace, final Optional<Revision> revision) {
98 return this.get().findModule(namespace, revision).orElse(null);
102 * Find {@link Module} in {@link SchemaContext} of {@link DOMMountPoint} by
103 * {@link QName} of {@link Module}.
108 * {@link QName} of module
109 * @return {@link Module}
111 public Module findModuleInMountPointByQName(final DOMMountPoint mountPoint, final QName moduleQname) {
112 final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
113 return schemaContext == null ? null
114 : schemaContext.findModule(moduleQname.getLocalName(), moduleQname.getRevision()).orElse(null);
118 * Find {@link Module} in {@link SchemaContext} by {@link QName}.
121 * {@link QName} of module
122 * @return {@link Module}
124 public Module findModuleByQName(final QName moduleQname) {
125 return this.findModuleByNameAndRevision(moduleQname.getLocalName(), moduleQname.getRevision());
129 * Find {@link Module} in {@link SchemaContext} by {@link String} localName
130 * and {@link Date} revision.
133 * local name of module
136 * @return {@link Module}
138 public Module findModuleByNameAndRevision(final String localName, final Optional<Revision> revision) {
139 return this.get().findModule(localName, revision).orElse(null);