Promote QueryParams as WriteDataParams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / jaxrs / QueryParams.java
@@ -19,15 +19,18 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.FilterParameter;
+import org.opendaylight.restconf.nb.rfc8040.InsertParameter;
 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
+import org.opendaylight.restconf.nb.rfc8040.PointParameter;
 import org.opendaylight.restconf.nb.rfc8040.StartTimeParameter;
 import org.opendaylight.restconf.nb.rfc8040.StopTimeParameter;
+import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 
 @Beta
-public final class UriInfoSupport {
-    private UriInfoSupport() {
+public final class QueryParams {
+    private QueryParams() {
         // Utility class
     }
 
@@ -68,12 +71,46 @@ public final class UriInfoSupport {
         }
     }
 
+    public static @NonNull WriteDataParams newWriteDataParams(final UriInfo uriInfo) {
+        InsertParameter insert = null;
+        PointParameter point = null;
+
+        for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
+            final String uriName = entry.getKey();
+            final List<String> paramValues = entry.getValue();
+            if (uriName.equals(InsertParameter.uriName())) {
+                final String str = optionalParam(uriName, paramValues);
+                if (str != null) {
+                    insert = InsertParameter.forUriValue(str);
+                    if (insert == null) {
+                        throw new RestconfDocumentedException("Unrecognized insert parameter value '" + str + "'",
+                            ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
+                    }
+                }
+            } else if (PointParameter.uriName().equals(uriName)) {
+                final String str = optionalParam(uriName, paramValues);
+                if (str != null) {
+                    point = PointParameter.forUriValue(str);
+                }
+            } else {
+                throw new RestconfDocumentedException("Bad parameter for post: " + uriName,
+                    ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
+            }
+        }
+
+        try {
+            return WriteDataParams.of(insert, point);
+        } catch (IllegalArgumentException e) {
+            throw new RestconfDocumentedException("Invalid query parameters: " + e.getMessage(), e);
+        }
+    }
+
     public static @Nullable String getSingleParameter(final MultivaluedMap<String, String> params, final String name) {
         final var values = params.get(name);
         return values == null ? null : optionalParam(name, values);
     }
 
-    public static @Nullable String optionalParam(final String name, final List<String> values) {
+    private static @Nullable String optionalParam(final String name, final List<String> values) {
         switch (values.size()) {
             case 0:
                 return null;