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.common.references;
10 import java.lang.ref.SoftReference;
12 import java.util.Date;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.restconf.Draft16;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 * This class creates {@link SoftReference} of actual {@link SchemaContext}
22 * object and even if the {@link SchemaContext} changes, this will be sticks
23 * reference to the old {@link SchemaContext} and provides work with the old
24 * {@link SchemaContext}.
27 public final class SchemaContextRef {
29 private final SoftReference<SchemaContext> schemaContextRef;
32 * Create {@link SoftReference} of actual {@link SchemaContext}
34 * @param schemaContext
35 * - actual {@link SchemaContext}
37 public SchemaContextRef(final SchemaContext schemaContext) {
38 this.schemaContextRef = new SoftReference<SchemaContext>(schemaContext);
42 * Get {@link SchemaContext} from reference
44 * @return {@link SchemaContext}
46 public SchemaContext get() {
47 return this.schemaContextRef.get();
51 * Get all modules like {@link Set} of {@link Module} from
52 * {@link SchemaContext}
54 * @return {@link Set} of {@link Module}
56 public Set<Module> getModules() {
57 return get().getModules();
61 * Get all modules like {@link Set} of {@link Module} from
62 * {@link SchemaContext} of {@link DOMMountPoint}
67 * @return {@link Set} of {@link Module}
69 public Set<Module> getModules(final DOMMountPoint mountPoint) {
70 final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
71 return schemaContext == null ? null : schemaContext.getModules();
75 * Get {@link Module} by ietf-restconf qname from
76 * {@link Draft16.RestconfModule}
78 * @return {@link Module}
80 public Module getRestconfModule() {
81 return this.findModuleByNamespaceAndRevision(Draft16.RestconfModule.IETF_RESTCONF_QNAME.getNamespace(),
82 Draft16.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
86 * Find {@link Module} in {@link SchemaContext} by {@link URI} and
90 * - namespace of module
92 * - revision of module
93 * @return {@link Module}
95 public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
96 return this.get().findModuleByNamespaceAndRevision(namespace, revision);
101 * Find {@link Module} in {@link SchemaContext} of {@link DOMMountPoint} by
102 * {@link QName} of {@link Module}
107 * - {@link QName} of module
108 * @return {@link Module}
110 public Module findModuleInMountPointByQName(final DOMMountPoint mountPoint, final QName moduleQname) {
111 final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
112 return schemaContext == null ? null
113 : schemaContext.findModuleByName(moduleQname.getLocalName(), moduleQname.getRevision());
117 * Find {@link Module} in {@link SchemaContext} by {@link QName}
120 * - {@link QName} of module
121 * @return {@link Module}
123 public Module findModuleByQName(final QName moduleQname) {
124 return this.findModuleByNameAndRevision(moduleQname.getLocalName(), moduleQname.getRevision());
128 * Find {@link Module} in {@link SchemaContext} by {@link String} localName
129 * and {@link Date} revision.
132 * - local name of module
134 * - revision of module
135 * @return {@link Module}
137 public Module findModuleByNameAndRevision(final String localName, final Date revision) {
138 return this.get().findModuleByName(localName, revision);