Add DatabindContext.schemaTree() 71/109071/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 26 Nov 2023 16:35:53 +0000 (17:35 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 26 Nov 2023 16:44:26 +0000 (17:44 +0100)
We need DataSchemaContextTree during binding of the URL. Retain a
reference in DatabindContext

JIRA: NETCONF-1157
Change-Id: If50b1880299f8b233f60e4198b77f7f11047e1dc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/databind/DatabindContext.java

index d3d0352559a775dab42b70482d87dc950f76f807..7938821f00c2a2a6d1660e611ada2544485deb89 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
@@ -58,12 +59,14 @@ public final class DatabindContext {
 
     private static final VarHandle JSON_CODECS;
     private static final VarHandle XML_CODECS;
+    private static final VarHandle SCHEMA_TREE;
 
     static {
         final var lookup = MethodHandles.lookup();
         try {
             JSON_CODECS = lookup.findVarHandle(DatabindContext.class, "jsonCodecs", JSONCodecFactory.class);
             XML_CODECS = lookup.findVarHandle(DatabindContext.class, "xmlCodecs", XmlCodecFactory.class);
+            SCHEMA_TREE = lookup.findVarHandle(DatabindContext.class, "schemaTree", DataSchemaContextTree.class);
         } catch (NoSuchFieldException | IllegalAccessException e) {
             throw new ExceptionInInitializerError(e);
         }
@@ -72,6 +75,8 @@ public final class DatabindContext {
     private final @NonNull MountPointContext mountContext;
     private final SourceResolver sourceResolver;
 
+    @SuppressWarnings("unused")
+    private volatile DataSchemaContextTree schemaTree;
     @SuppressWarnings("unused")
     private volatile JSONCodecFactory jsonCodecs;
     @SuppressWarnings("unused")
@@ -105,6 +110,17 @@ public final class DatabindContext {
         return mountContext.getEffectiveModelContext();
     }
 
+    public @NonNull DataSchemaContextTree schemaTree() {
+        final var existing = (DataSchemaContextTree) SCHEMA_TREE.getAcquire(this);
+        return existing != null ? existing : createSchemaTree();
+    }
+
+    private @NonNull DataSchemaContextTree createSchemaTree() {
+        final var created = DataSchemaContextTree.from(modelContext());
+        final var witness = (DataSchemaContextTree) SCHEMA_TREE.compareAndExchangeRelease(this, null, created);
+        return witness != null ? witness : created;
+    }
+
     public @NonNull JSONCodecFactory jsonCodecs() {
         final var existing = (JSONCodecFactory) JSON_CODECS.getAcquire(this);
         return existing != null ? existing : createJsonCodecs();