43a048e855cad603cd9ef905b828600e05ccc086
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / legacy / NormalizedNodePayload.java
1 /*
2  * Copyright (c) 2021 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.legacy;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.net.URI;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
20
21 /**
22  * A RFC8040 overlay from our marriage to NormalizedNodeContext. This represents a NormalizedNode along with further
23  * messy details needed to deal with the payload.
24  */
25 public final class NormalizedNodePayload {
26     private final @NonNull ImmutableMap<String, Object> headers;
27     private final @NonNull QueryParameters writerParameters;
28     private final @NonNull Inference inference;
29     private final NormalizedNode data;
30
31     private NormalizedNodePayload(final Inference inference, final NormalizedNode data,
32             final QueryParameters writerParameters, final ImmutableMap<String, Object> headers) {
33         this.inference = requireNonNull(inference);
34         this.data = data;
35         this.writerParameters = requireNonNull(writerParameters);
36         this.headers = requireNonNull(headers);
37     }
38
39     public static @NonNull NormalizedNodePayload empty(final Inference inference) {
40         return new NormalizedNodePayload(inference, null, QueryParameters.empty(), ImmutableMap.of());
41     }
42
43     public static @NonNull NormalizedNodePayload of(final Inference inference, final NormalizedNode data) {
44         return new NormalizedNodePayload(inference, requireNonNull(data), QueryParameters.empty(), ImmutableMap.of());
45     }
46
47     public static @NonNull NormalizedNodePayload ofNullable(final Inference inference, final NormalizedNode data) {
48         return data == null ? empty(inference) : of(inference, data);
49     }
50
51     // FIXME: can we get rid of this, please? Whoever is using this should be setting a Response instead
52     @Deprecated
53     public static @NonNull NormalizedNodePayload ofLocation(final Inference inference, final NodeIdentifier leafId,
54             final URI location) {
55         return new NormalizedNodePayload(inference, ImmutableNodes.leafNode(leafId, location.toString()),
56             QueryParameters.empty(), ImmutableMap.of("Location", location));
57     }
58
59     public static Object ofReadData(final Inference inference, final NormalizedNode data,
60             final QueryParameters parameters) {
61         return new NormalizedNodePayload(inference, requireNonNull(data), parameters, ImmutableMap.of());
62     }
63
64     public @NonNull Inference inference() {
65         return inference;
66     }
67
68     public @Nullable NormalizedNode getData() {
69         return data;
70     }
71
72     /**
73      * Return headers response headers.
74      *
75      * @return map of headers
76      */
77     // FIXME: this is only used for redirect on subscribe
78     public @NonNull ImmutableMap<String, Object> getNewHeaders() {
79         return headers;
80     }
81
82     public @NonNull QueryParameters getWriterParameters() {
83         return writerParameters;
84     }
85 }