6ed4fcc5d92bdabde62e9dc28be3bf15d9e8e5f5
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / 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.nb.rfc8040.databind;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
14 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
15 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
16 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
17 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19
20 /**
21  * An immutable context holding a consistent view of things related to data bind operations.
22  */
23 public record DatabindContext(
24         @NonNull MountPointContext mountContext,
25         @NonNull JSONCodecFactory jsonCodecs,
26         @NonNull XmlCodecFactory xmlCodecs) {
27     public DatabindContext {
28         requireNonNull(mountContext);
29         requireNonNull(jsonCodecs);
30         requireNonNull(xmlCodecs);
31     }
32
33     public static @NonNull DatabindContext ofModel(final EffectiveModelContext modelContext) {
34         return ofMountPoint(new EmptyMountPointContext(modelContext));
35     }
36
37     public static @NonNull DatabindContext ofMountPoint(final MountPointContext mountContext) {
38         return new DatabindContext(mountContext,
39             JSONCodecFactorySupplier.RFC7951.getShared(mountContext.getEffectiveModelContext()),
40             XmlCodecFactory.create(mountContext));
41     }
42
43     public @NonNull EffectiveModelContext modelContext() {
44         return mountContext.getEffectiveModelContext();
45     }
46 }