From c7190c8cced3b56410e6cf57d637be1accd2c1d0 Mon Sep 17 00:00:00 2001 From: Peter Suna Date: Tue, 7 Nov 2023 17:17:36 +0100 Subject: [PATCH] Add correct status code to YangPatch 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 --- .../impl/RestconfDataServiceImpl.java | 23 ++++++++++++++++++- .../impl/RestconfDataServiceImplTest.java | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java index 57b44ccd0c..ec20517e31 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java @@ -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()); + } + } + } } diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java index b3b2456efb..652b137475 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java @@ -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()); -- 2.36.6