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