From 3a131d1ebe983754661b6ca1889e61f677998c2e Mon Sep 17 00:00:00 2001 From: Peter Suna Date: Tue, 7 Nov 2023 17:17:36 +0100 Subject: [PATCH 1/1] 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 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 cd40c87096..ef20881549 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 @@ -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 { 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 5de287c519..8c0516387e 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 @@ -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()); -- 2.36.6