2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.restconf.api.query.InsertParam;
17 import org.opendaylight.restconf.api.query.PointParam;
18 import org.opendaylight.yangtools.concepts.Immutable;
21 * Parser and holder of query parameters from uriInfo for data and datastore modification operations.
23 // FIXME: this should be a record with JDK17+
24 public final class WriteDataParams implements Immutable {
25 private static final @NonNull WriteDataParams EMPTY = new WriteDataParams(null, null);
27 private final PointParam point;
28 private final InsertParam insert;
30 private WriteDataParams(final InsertParam insert, final PointParam point) {
35 public static @NonNull WriteDataParams empty() {
39 public static @NonNull WriteDataParams of(final InsertParam insert, final PointParam point) {
45 // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.5:
46 // If the values "before" or "after" are used, then a "point" query
47 // parameter for the "insert" query parameter MUST also be present, or a
48 // "400 Bad Request" status-line is returned.
49 if (insert == InsertParam.BEFORE || insert == InsertParam.AFTER) {
50 throw new IllegalArgumentException(
51 "Insert parameter " + insert.paramValue() + " cannot be used without a Point parameter.");
54 // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.6:
55 // [when "point" parameter is present and]
56 // If the "insert" query parameter is not present or has a value other
57 // than "before" or "after", then a "400 Bad Request" status-line is
59 if (insert != InsertParam.BEFORE && insert != InsertParam.AFTER) {
60 throw new IllegalArgumentException(
61 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
65 return new WriteDataParams(insert, point);
68 public @Nullable InsertParam insert() {
72 public @Nullable PointParam point() {
77 // FIXME: it seems callers' structure should be able to cater with just point() and insert()
78 public @NonNull PointParam getPoint() {
79 return verifyNotNull(point);
83 public String toString() {
84 final var helper = MoreObjects.toStringHelper(this).omitNullValues();
86 helper.add("insert", insert.paramValue());
89 helper.add("point", point.value());
91 return helper.toString();