Clean up {Post,Put}DataTransactionUtil.submitData()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / WriteDataParams.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;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
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;
19
20 /**
21  * Parser and holder of query parameters from uriInfo for data and datastore modification operations.
22  */
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);
26
27     private final PointParam point;
28     private final InsertParam insert;
29
30     private WriteDataParams(final InsertParam insert, final PointParam point) {
31         this.insert = insert;
32         this.point = point;
33     }
34
35     public static @NonNull WriteDataParams empty() {
36         return EMPTY;
37     }
38
39     public static @NonNull WriteDataParams of(final InsertParam insert, final PointParam point) {
40         if (point == null) {
41             if (insert == null) {
42                 return empty();
43             }
44
45             // https://www.rfc-editor.org/rfc/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.");
52             }
53         } else {
54             // https://www.rfc-editor.org/rfc/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
58             //        returned.
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.");
62             }
63         }
64
65         return new WriteDataParams(insert, point);
66     }
67
68     public @Nullable InsertParam insert() {
69         return insert;
70     }
71
72     public @Nullable PointParam point() {
73         return point;
74     }
75
76     @Beta
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);
80     }
81
82     @Override
83     public String toString() {
84         final var helper = MoreObjects.toStringHelper(this).omitNullValues();
85         if (insert != null) {
86             helper.add("insert", insert.paramValue());
87         }
88         if (point != null) {
89             helper.add("point", point.value());
90         }
91         return helper.toString();
92     }
93 }