346e6e03a285bcec8188d4df58f7dd44e01f50e7
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.net.URI;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
17 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
21
22 /**
23  * A RFC8040 overlay over {@link NormalizedNodeContext}. This represents a NormalizedNode along with further messy
24  * details needed to deal with the payload.
25  */
26 public final class NormalizedNodePayload extends NormalizedNodeContext {
27     private NormalizedNodePayload(final InstanceIdentifierContext<?> context,
28             final NormalizedNode data, final QueryParameters writerParameters,
29             final ImmutableMap<String, Object> headers) {
30         super(context, data, writerParameters, headers);
31     }
32
33     public static @NonNull NormalizedNodePayload empty(final InstanceIdentifierContext<?> path) {
34         return new NormalizedNodePayload(requireNonNull(path), null, QueryParameters.empty(), ImmutableMap.of());
35     }
36
37     public static @NonNull NormalizedNodePayload of(final InstanceIdentifierContext<?> path,
38             final NormalizedNode data) {
39         return new NormalizedNodePayload(requireNonNull(path), requireNonNull(data), QueryParameters.empty(),
40             ImmutableMap.of());
41     }
42
43     public static @NonNull NormalizedNodePayload ofNullable(final InstanceIdentifierContext<?> path,
44             final NormalizedNode data) {
45         return data == null ? empty(path) : of(path, data);
46     }
47
48     public static @NonNull NormalizedNodePayload ofLocation(final InstanceIdentifierContext<?> path,
49             final NodeIdentifier leafId, final URI location) {
50         return new NormalizedNodePayload(requireNonNull(path), ImmutableNodes.leafNode(leafId, location.toString()),
51             QueryParameters.empty(), ImmutableMap.of("Location", location));
52     }
53
54     @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "Ensured via constructor")
55     @Override
56     public QueryParameters getWriterParameters() {
57         return (QueryParameters) super.getWriterParameters();
58     }
59 }