Add correct status code to YangPatch 80/108880/4
authorPeter Suna <peter.suna@pantheon.tech>
Tue, 7 Nov 2023 16:17:36 +0000 (17:17 +0100)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Thu, 9 Nov 2023 08:23:27 +0000 (08:23 +0000)
Set the status code based on the first global error
or the first error in the PatchStatusEntity.

JIRA: NETCONF-1103
Change-Id: Ica4e503226e9323e02efc51cf28e07868177154d
Signed-off-by: Peter Suna <peter.suna@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java

index cd40c87096b9fe0feaec8e7973c176e097e5d90e..ef2088154986e60dcf86dbb45022556c071738fd 100644 (file)
@@ -52,6 +52,7 @@ import org.opendaylight.restconf.common.errors.RestconfFuture;
 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
+import org.opendaylight.restconf.common.patch.PatchStatusEntity;
 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
 import org.opendaylight.restconf.nb.rfc8040.databind.ChildBody;
@@ -68,6 +69,7 @@ import org.opendaylight.restconf.nb.rfc8040.databind.XmlOperationInputBody;
 import org.opendaylight.restconf.nb.rfc8040.databind.XmlPatchBody;
 import org.opendaylight.restconf.nb.rfc8040.databind.XmlResourceBody;
 import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams;
+import org.opendaylight.restconf.nb.rfc8040.legacy.ErrorTags;
 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy.CreateOrReplaceResult;
@@ -697,11 +699,30 @@ public final class RestconfDataServiceImpl {
             .addCallback(new JaxRsRestconfCallback<>(ar) {
                 @Override
                 Response transform(final PatchStatusContext result) {
-                    return Response.ok().entity(result).build();
+                    return Response.status(getStatusCode(result)).entity(result).build();
                 }
             });
     }
 
+    private static Status getStatusCode(final PatchStatusContext result) {
+        if (result.ok()) {
+            return Status.OK;
+        } else {
+            if (result.globalErrors() == null || result.globalErrors().isEmpty()) {
+                return result.editCollection().stream()
+                    .filter(patchStatus -> !patchStatus.isOk() && !patchStatus.getEditErrors().isEmpty())
+                    .findFirst()
+                    .map(PatchStatusEntity::getEditErrors)
+                    .flatMap(errors -> errors.stream().findFirst())
+                    .map(error -> ErrorTags.statusOf(error.getErrorTag()))
+                    .orElse(Status.INTERNAL_SERVER_ERROR);
+            } else {
+                final var error = result.globalErrors().iterator().next();
+                return ErrorTags.statusOf(error.getErrorTag());
+            }
+        }
+    }
+
     private static @NonNull PatchContext parsePatchBody(final @NonNull EffectiveModelContext context,
             final @NonNull YangInstanceIdentifier urlPath, final @NonNull PatchBody body) {
         try {
index 5de287c5191c5063b3be1ac2d1f412dc86166289..8c0516387e3663c50fd8f96172dca75c94c7efbb 100644 (file)
@@ -473,7 +473,7 @@ public class RestconfDataServiceImplTest extends AbstractJukeboxTest {
         doReturn(true).when(asyncResponse).resume(responseCaptor.capture());
         dataService.yangPatchData(JUKEBOX_SCHEMA, patch, null, asyncResponse);
         final var response = responseCaptor.getValue();
-        assertEquals(200, response.getStatus());
+        assertEquals(409, response.getStatus());
         final var status = assertInstanceOf(PatchStatusContext.class, response.getEntity());
 
         assertFalse(status.ok());