2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.server.api;
10 import static java.util.Objects.requireNonNull;
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;
24 * An immutable context holding a consistent view of things related to data bind operations.
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;
32 final var lookup = MethodHandles.lookup();
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);
42 private final @NonNull MountPointContext mountContext;
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;
54 private DatabindContext(final @NonNull MountPointContext mountContext) {
55 this.mountContext = requireNonNull(mountContext);
58 public static @NonNull DatabindContext ofModel(final EffectiveModelContext modelContext) {
59 return ofMountPoint(MountPointContext.of(modelContext));
62 public static @NonNull DatabindContext ofMountPoint(final MountPointContext mountContext) {
63 return new DatabindContext(mountContext);
66 public @NonNull EffectiveModelContext modelContext() {
67 return mountContext.modelContext();
70 public @NonNull DataSchemaContextTree schemaTree() {
71 final var existing = (DataSchemaContextTree) SCHEMA_TREE.getAcquire(this);
72 return existing != null ? existing : createSchemaTree();
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;
81 public @NonNull JSONCodecFactory jsonCodecs() {
82 final var existing = (JSONCodecFactory) JSON_CODECS.getAcquire(this);
83 return existing != null ? existing : createJsonCodecs();
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;
92 public @NonNull XmlCodecFactory xmlCodecs() {
93 final var existing = (XmlCodecFactory) XML_CODECS.getAcquire(this);
94 return existing != null ? existing : createXmlCodecs();
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;