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 com.google.common.base.MoreObjects;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.concepts.Immutable;
16 * Parser and holder of query parameters from uriInfo for data and datastore modification operations.
18 // FIXME: this should be a record with JDK17+
19 public final class WriteDataParams implements Immutable {
20 private static final @NonNull WriteDataParams EMPTY = new WriteDataParams(null, null);
22 private final PointParam point;
23 private final InsertParam insert;
25 private WriteDataParams(final InsertParam insert, final PointParam point) {
30 public static @NonNull WriteDataParams empty() {
34 public static @NonNull WriteDataParams of(final InsertParam insert, final PointParam point) {
40 // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.5:
41 // If the values "before" or "after" are used, then a "point" query
42 // parameter for the "insert" query parameter MUST also be present, or a
43 // "400 Bad Request" status-line is returned.
44 if (insert == InsertParam.BEFORE || insert == InsertParam.AFTER) {
45 throw new IllegalArgumentException(
46 "Insert parameter " + insert.paramValue() + " cannot be used without a Point parameter.");
49 // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.6:
50 // [when "point" parameter is present and]
51 // If the "insert" query parameter is not present or has a value other
52 // than "before" or "after", then a "400 Bad Request" status-line is
54 if (insert != InsertParam.BEFORE && insert != InsertParam.AFTER) {
55 throw new IllegalArgumentException(
56 "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
60 return new WriteDataParams(insert, point);
63 public @Nullable InsertParam insert() {
67 public @Nullable PointParam point() {
72 public String toString() {
73 final var helper = MoreObjects.toStringHelper(this).omitNullValues();
75 helper.add("insert", insert.paramValue());
78 helper.add("point", point.value());
80 return helper.toString();