Promote QueryParams as WriteDataParams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImpl.java
index e9b6139122ccd2d8acf57962a46b2b9a67d719c3..76b3d23cebba2407610d21f6209f70d891f9bb77 100644 (file)
@@ -26,7 +26,6 @@ import java.time.format.DateTimeFormatter;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
@@ -34,7 +33,6 @@ import javax.ws.rs.Path;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMActionException;
 import org.opendaylight.mdsal.dom.api.DOMActionResult;
@@ -51,10 +49,9 @@ import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
-import org.opendaylight.restconf.nb.rfc8040.InsertParameter;
-import org.opendaylight.restconf.nb.rfc8040.PointParameter;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
-import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.UriInfoSupport;
+import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
+import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
@@ -73,7 +70,6 @@ import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListen
 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
-import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -101,17 +97,6 @@ import org.slf4j.LoggerFactory;
  */
 @Path("/")
 public class RestconfDataServiceImpl implements RestconfDataService {
-    // FIXME: we should be able to interpret 'point' and refactor this class into a behavior
-    private static final class QueryParams implements Immutable {
-        final @Nullable PointParameter point;
-        final @Nullable InsertParameter insert;
-
-        QueryParams(final @Nullable InsertParameter insert, final @Nullable PointParameter point) {
-            this.insert = insert;
-            this.point = point;
-        }
-    }
-
     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
     private static final QName NETCONF_BASE_QNAME = SchemaContext.NAME;
@@ -231,7 +216,7 @@ public class RestconfDataServiceImpl implements RestconfDataService {
     public Response putData(final String identifier, final NormalizedNodePayload payload, final UriInfo uriInfo) {
         requireNonNull(payload);
 
-        final QueryParams checkedParms = checkQueryParameters(uriInfo);
+        final WriteDataParams checkedParms = QueryParams.newWriteDataParams(uriInfo);
 
         final InstanceIdentifierContext<? extends SchemaNode> iid = payload.getInstanceIdentifierContext();
 
@@ -244,56 +229,7 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                 ? schemaContextHandler.get() : modelContext(mountPoint);
 
         final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
-        return PutDataTransactionUtil.putData(payload, ref, strategy, checkedParms.insert, checkedParms.point);
-    }
-
-    private static QueryParams checkQueryParameters(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 = UriInfoSupport.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 = UriInfoSupport.optionalParam(uriName, paramValues);
-                if (str != null) {
-                    point = PointParameter.forUriValue(str);
-                }
-            } else {
-                throw new RestconfDocumentedException("Bad parameter for post: " + uriName,
-                    ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
-            }
-        }
-
-        // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.5:
-        //        If the values "before" or "after" are used, then a "point" query
-        //        parameter for the "insert" query parameter MUST also be present, or a
-        //        "400 Bad Request" status-line is returned.
-        if ((insert == InsertParameter.BEFORE || insert == InsertParameter.AFTER) && point == null) {
-            throw new RestconfDocumentedException(
-                "Insert parameter " + insert.uriValue() + " cannot be used without a Point parameter.",
-                ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
-        }
-        // https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.6:
-        //        If the "insert" query parameter is not present or has a value other
-        //        than "before" or "after", then a "400 Bad Request" status-line is
-        //        returned.
-        if (point != null && insert != InsertParameter.BEFORE && insert != InsertParameter.AFTER) {
-            throw new RestconfDocumentedException(
-                "Point parameter can be used only with 'after' or 'before' values of Insert parameter.",
-                ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT);
-        }
-
-        return new QueryParams(insert, point);
+        return PutDataTransactionUtil.putData(payload, ref, strategy, checkedParms.insert(), checkedParms.point());
     }
 
     @Override
@@ -308,11 +244,11 @@ public class RestconfDataServiceImpl implements RestconfDataService {
             return invokeAction(payload);
         }
 
-        final QueryParams checkedParms = checkQueryParameters(uriInfo);
+        final WriteDataParams checkedParms = QueryParams.newWriteDataParams(uriInfo);
         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
         final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
         return PostDataTransactionUtil.postData(uriInfo, payload, strategy,
-                getSchemaContext(mountPoint), checkedParms.insert, checkedParms.point);
+                getSchemaContext(mountPoint), checkedParms.insert(), checkedParms.point());
     }
 
     @Override