Factor out common operations schema/data construction
[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.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.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 {@link Module} by ietf-restconf qname from
64      * {@link Rfc8040.RestconfModule}.
65      *
66      * @return {@link Module}
67      */
68     public Module getRestconfModule() {
69         return this.findModuleByNamespaceAndRevision(Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getNamespace(),
70                 Rfc8040.RestconfModule.IETF_RESTCONF_QNAME.getRevision());
71     }
72
73     /**
74      * Find {@link Module} in {@link SchemaContext} by {@link URI} and
75      * {@link Date}.
76      *
77      * @param namespace
78      *             namespace of module
79      * @param revision
80      *             revision of module
81      * @return {@link Module}
82      */
83     public Module findModuleByNamespaceAndRevision(final URI namespace, final Optional<Revision> revision) {
84         return this.get().findModule(namespace, revision).orElse(null);
85     }
86
87     /**
88      * Find {@link Module} in {@link SchemaContext} of {@link DOMMountPoint} by
89      * {@link QName} of {@link Module}.
90      *
91      * @param mountPoint
92      *             mount point
93      * @param moduleQname
94      *             {@link QName} of module
95      * @return {@link Module}
96      */
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);
101     }
102
103     /**
104      * Find {@link Module} in {@link SchemaContext} by {@link QName}.
105      *
106      * @param moduleQname
107      *             {@link QName} of module
108      * @return {@link Module}
109      */
110     public Module findModuleByQName(final QName moduleQname) {
111         return this.findModuleByNameAndRevision(moduleQname.getLocalName(), moduleQname.getRevision());
112     }
113
114     /**
115      * Find {@link Module} in {@link SchemaContext} by {@link String} localName
116      * and {@link Date} revision.
117      *
118      * @param localName
119      *             local name of module
120      * @param revision
121      *             revision of module
122      * @return {@link Module}
123      */
124     public Module findModuleByNameAndRevision(final String localName, final Optional<Revision> revision) {
125         return this.get().findModule(localName, revision).orElse(null);
126     }
127 }