Deprecate NormalizedNodeContext
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.opendaylight.restconf.common.context.InstanceIdentifierContext;
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
20 /**
21  * A RFC8040 overlay from our marriage to NormalizedNodeContext. This represents a NormalizedNode along with further
22  * messy details needed to deal with the payload.
23  */
24 public final class NormalizedNodePayload {
25     private final InstanceIdentifierContext<?> context;
26     private final ImmutableMap<String, Object> headers;
27     private final QueryParameters writerParameters;
28     private final NormalizedNode data;
29
30     private NormalizedNodePayload(final InstanceIdentifierContext<?> context,
31             final NormalizedNode data, final QueryParameters writerParameters,
32             final ImmutableMap<String, Object> headers) {
33         this.context = context;
34         this.data = data;
35         this.writerParameters = requireNonNull(writerParameters);
36         this.headers = requireNonNull(headers);
37     }
38
39     public static @NonNull NormalizedNodePayload empty(final InstanceIdentifierContext<?> path) {
40         return new NormalizedNodePayload(requireNonNull(path), null, QueryParameters.empty(), ImmutableMap.of());
41     }
42
43     public static @NonNull NormalizedNodePayload of(final InstanceIdentifierContext<?> path,
44             final NormalizedNode data) {
45         return new NormalizedNodePayload(requireNonNull(path), requireNonNull(data), QueryParameters.empty(),
46             ImmutableMap.of());
47     }
48
49     public static @NonNull NormalizedNodePayload ofNullable(final InstanceIdentifierContext<?> path,
50             final NormalizedNode data) {
51         return data == null ? empty(path) : of(path, data);
52     }
53
54     public static @NonNull NormalizedNodePayload ofLocation(final InstanceIdentifierContext<?> path,
55             final NodeIdentifier leafId, final URI location) {
56         return new NormalizedNodePayload(requireNonNull(path), ImmutableNodes.leafNode(leafId, location.toString()),
57             QueryParameters.empty(), ImmutableMap.of("Location", location));
58     }
59
60     public static Object ofReadData(final InstanceIdentifierContext<?> path, final NormalizedNode data,
61             final QueryParameters parameters) {
62         return new NormalizedNodePayload(requireNonNull(path), requireNonNull(data), parameters, ImmutableMap.of());
63     }
64
65     public InstanceIdentifierContext<?> getInstanceIdentifierContext() {
66         return context;
67     }
68
69     public NormalizedNode getData() {
70         return data;
71     }
72
73     /**
74      * Return headers response headers.
75      *
76      * @return map of headers
77      */
78     // FIXME: this is only used for redirect on subscribe
79     public ImmutableMap<String, Object> getNewHeaders() {
80         return headers;
81     }
82
83     public QueryParameters getWriterParameters() {
84         return writerParameters;
85     }
86 }