Remove "/" sign in AbstractRestconfStreamRegistry
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / DatabindContext.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.server.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
17 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
19 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
20 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 /**
24  * An immutable context holding a consistent view of things related to data bind operations.
25  */
26 public final class DatabindContext {
27     private static final VarHandle JSON_CODECS;
28     private static final VarHandle XML_CODECS;
29     private static final VarHandle SCHEMA_TREE;
30
31     static {
32         final var lookup = MethodHandles.lookup();
33         try {
34             JSON_CODECS = lookup.findVarHandle(DatabindContext.class, "jsonCodecs", JSONCodecFactory.class);
35             XML_CODECS = lookup.findVarHandle(DatabindContext.class, "xmlCodecs", XmlCodecFactory.class);
36             SCHEMA_TREE = lookup.findVarHandle(DatabindContext.class, "schemaTree", DataSchemaContextTree.class);
37         } catch (NoSuchFieldException | IllegalAccessException e) {
38             throw new ExceptionInInitializerError(e);
39         }
40     }
41
42     private final @NonNull MountPointContext mountContext;
43
44     @SuppressWarnings("unused")
45     @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
46     private volatile DataSchemaContextTree schemaTree;
47     @SuppressWarnings("unused")
48     @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
49     private volatile JSONCodecFactory jsonCodecs;
50     @SuppressWarnings("unused")
51     @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
52     private volatile XmlCodecFactory xmlCodecs;
53
54     private DatabindContext(final @NonNull MountPointContext mountContext) {
55         this.mountContext = requireNonNull(mountContext);
56     }
57
58     public static @NonNull DatabindContext ofModel(final EffectiveModelContext modelContext) {
59         return ofMountPoint(MountPointContext.of(modelContext));
60     }
61
62     public static @NonNull DatabindContext ofMountPoint(final MountPointContext mountContext) {
63         return new DatabindContext(mountContext);
64     }
65
66     public @NonNull EffectiveModelContext modelContext() {
67         return mountContext.modelContext();
68     }
69
70     public @NonNull DataSchemaContextTree schemaTree() {
71         final var existing = (DataSchemaContextTree) SCHEMA_TREE.getAcquire(this);
72         return existing != null ? existing : createSchemaTree();
73     }
74
75     private @NonNull DataSchemaContextTree createSchemaTree() {
76         final var created = DataSchemaContextTree.from(modelContext());
77         final var witness = (DataSchemaContextTree) SCHEMA_TREE.compareAndExchangeRelease(this, null, created);
78         return witness != null ? witness : created;
79     }
80
81     public @NonNull JSONCodecFactory jsonCodecs() {
82         final var existing = (JSONCodecFactory) JSON_CODECS.getAcquire(this);
83         return existing != null ? existing : createJsonCodecs();
84     }
85
86     private @NonNull JSONCodecFactory createJsonCodecs() {
87         final var created = JSONCodecFactorySupplier.RFC7951.getShared(mountContext.modelContext());
88         final var witness = (JSONCodecFactory) JSON_CODECS.compareAndExchangeRelease(this, null, created);
89         return witness != null ? witness : created;
90     }
91
92     public @NonNull XmlCodecFactory xmlCodecs() {
93         final var existing = (XmlCodecFactory) XML_CODECS.getAcquire(this);
94         return existing != null ? existing : createXmlCodecs();
95     }
96
97     private @NonNull XmlCodecFactory createXmlCodecs() {
98         final var created = XmlCodecFactory.create(mountContext);
99         final var witness = (XmlCodecFactory) XML_CODECS.compareAndExchangeRelease(this, null, created);
100         return witness != null ? witness : created;
101     }
102 }