Add correct status code to YangPatch 02/108902/2
authorPeter Suna <peter.suna@pantheon.tech>
Tue, 7 Nov 2023 16:17:36 +0000 (17:17 +0100)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Fri, 10 Nov 2023 09:36:23 +0000 (09:36 +0000)
Set the status code based on the first global error
or the first error in the PatchStatusEntity.

JIRA: NETCONF-1103
Change-Id: Ica4e503226e9323e02efc51cf28e07868177154d
(cherry picked from commit 3a131d1ebe983754661b6ca1889e61f677998c2e)
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 57b44ccd0c9099f16d59346b6ab6df6be042beed..ec20517e3112f250429d4dd324bec3a73cdb33ab 100644 (file)
@@ -43,10 +43,12 @@ import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
+import org.opendaylight.restconf.common.ErrorTags;
 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.common.patch.PatchStatusEntity;
 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
 import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
@@ -268,7 +270,7 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         final RestconfStrategy strategy = getRestconfStrategy(iid.getMountPoint());
         final PatchStatusContext patchStatusContext = PatchDataTransactionUtil.patchData(context, strategy,
             iid.getSchemaContext());
-        return Response.status(Status.OK)
+        return Response.status(getStatusCode(patchStatusContext))
             .entity(patchStatusContext)
             .build();
     }
@@ -481,4 +483,23 @@ public class RestconfDataServiceImpl implements RestconfDataService {
             }
         }
     }
+
+    private static Status getStatusCode(final PatchStatusContext result) {
+        if (result.isOk()) {
+            return Status.OK;
+        } else {
+            if (result.getGlobalErrors() == null || result.getGlobalErrors().isEmpty()) {
+                return result.getEditCollection().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.getGlobalErrors().iterator().next();
+                return ErrorTags.statusOf(error.getErrorTag());
+            }
+        }
+    }
 }
index b3b2456efbba1764c78815a3f990c7c2b75a1d06..652b1374753744589f701ee695f49819be8eddc7 100644 (file)
@@ -506,7 +506,7 @@ public class RestconfDataServiceImplTest {
                 .when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
         doReturn(true).when(readWrite).cancel();
         final Response response = dataService.patchData(patch, uriInfo);
-        assertEquals(200, response.getStatus());
+        assertEquals(409, response.getStatus());
         final PatchStatusContext status = assertInstanceOf(PatchStatusContext.class, response.getEntity());
 
         assertFalse(status.isOk());