Update MRI projects for Aluminium
[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.Collection;
13 import java.util.Date;
14 import java.util.Optional;
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.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22
23 /**
24  * This class creates {@link SoftReference} of actual {@link EffectiveModelContext}
25  * object and even if the {@link SchemaContext} changes, this will be sticks
26  * reference to the old {@link SchemaContext} and provides work with the old
27  * {@link EffectiveModelContext}.
28  *
29  */
30 public final class SchemaContextRef {
31
32     private final SoftReference<EffectiveModelContext> schemaContextRef;
33
34     /**
35      * Create {@link SoftReference} of actual {@link EffectiveModelContext}.
36      *
37      * @param schemaContext
38      *             actual {@link EffectiveModelContext}
39      */
40     public SchemaContextRef(final EffectiveModelContext schemaContext) {
41         this.schemaContextRef = new SoftReference<>(schemaContext);
42     }
43
44     /**
45      * Get {@link EffectiveModelContext} from reference.
46      *
47      * @return {@link EffectiveModelContext}
48      */
49     public EffectiveModelContext get() {
50         return this.schemaContextRef.get();
51     }
52
53     /**
54      * Get all modules like {@link Collection} of {@link Module} from {@link SchemaContext}.
55      *
56      * @return {@link Collection} of {@link Module}
57      */
58     public Collection<? extends 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 }