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