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