84fb1bd8246db8bab2608044b14521c7a26af016
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / common / references / SchemaContextRef.java
1 /*
2  * Copyright (c) 2016 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.restconf.common.references;
9
10 import java.lang.ref.SoftReference;
11 import java.net.URI;
12 import java.util.Date;
13 import java.util.Set;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.restconf.Draft15;
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;
19
20 /**
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}.
25  *
26  */
27 public final class SchemaContextRef {
28
29     private final SoftReference<SchemaContext> schemaContextRef;
30
31     /**
32      * Create {@link SoftReference} of actual {@link SchemaContext}
33      *
34      * @param schemaContext
35      *            - actual {@link SchemaContext}
36      */
37     public SchemaContextRef(final SchemaContext schemaContext) {
38         this.schemaContextRef = new SoftReference<SchemaContext>(schemaContext);
39     }
40
41     /**
42      * Get {@link SchemaContext} from reference
43      *
44      * @return {@link SchemaContext}
45      */
46     public SchemaContext get() {
47         return this.schemaContextRef.get();
48     }
49
50     /**
51      * Get all modules like {@link Set} of {@link Module} from
52      * {@link SchemaContext}
53      *
54      * @return {@link Set} of {@link Module}
55      */
56     public Set<Module> getModules() {
57         return get().getModules();
58     }
59
60     /**
61      * Get all modules like {@link Set} of {@link Module} from
62      * {@link SchemaContext} of {@link DOMMountPoint}
63      *
64      * @param mountPoint
65      *            - mount point
66      *
67      * @return {@link Set} of {@link Module}
68      */
69     public Set<Module> getModules(final DOMMountPoint mountPoint) {
70         final SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
71         return schemaContext == null ? null : schemaContext.getModules();
72     }
73
74     /**
75      * Get {@link Module} by ietf-restconf qname from
76      * {@link Draft15.RestconfModule}
77      *
78      * @return {@link Module}
79      */
80     public Module getRestconfModule() {
81         return this.findModuleByNamespaceAndRevision(Draft15.RestconfModule.IETF_RESTCONF_QNAME.getNamespace(),
82                 Draft15.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
83     }
84
85     /**
86      * Find {@link Module} in {@link SchemaContext} by {@link URI} and
87      * {@link Date}
88      *
89      * @param namespace
90      *            - namespace of module
91      * @param revision
92      *            - revision of module
93      * @return {@link Module}
94      */
95     public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
96         return this.get().findModuleByNamespaceAndRevision(namespace, revision);
97     }
98
99
100     /**
101      * Find {@link Module} in {@link SchemaContext} of {@link DOMMountPoint} by
102      * {@link QName} of {@link Module}
103      *
104      * @param mountPoint
105      *            - mount point
106      * @param moduleQname
107      *            - {@link QName} of module
108      * @return {@link Module}
109      */
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());
114     }
115
116     /**
117      * Find {@link Module} in {@link SchemaContext} by {@link QName}
118      *
119      * @param moduleQname
120      *            - {@link QName} of module
121      * @return {@link Module}
122      */
123     public Module findModuleByQName(final QName moduleQname) {
124         return this.findModuleByNameAndRevision(moduleQname.getLocalName(), moduleQname.getRevision());
125     }
126
127     /**
128      * Find {@link Module} in {@link SchemaContext} by {@link String} localName
129      * and {@link Date} revision.
130      *
131      * @param localName
132      *            - local name of module
133      * @param revision
134      *            - revision of module
135      * @return {@link Module}
136      */
137     public Module findModuleByNameAndRevision(final String localName, final Date revision) {
138         return this.get().findModuleByName(localName, revision);
139     }
140 }