From 104c0766e075d7d3c529f4032d47594f9034afbd Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 21 Apr 2021 00:37:56 +0200 Subject: Fix issue in log + Pdp payload Fix log issue that prevent the error to be reported properly + fix the pdp payload construction Issue-ID: POLICY-3205 Signed-off-by: sebdet Change-Id: Ic80ea4f679719378fc8134f7282b361306d797b0 --- .../onap/policy/clamp/clds/util/LoggingUtils.java | 92 +++++++++++----------- .../clamp/policy/pdpgroup/PdpGroupPayload.java | 18 +++-- .../policy/pdp-group-multi-policies-payload.json | 30 ++----- .../example/policy/pdp-group-policy-payload.json | 14 +--- 4 files changed, 66 insertions(+), 88 deletions(-) diff --git a/src/main/java/org/onap/policy/clamp/clds/util/LoggingUtils.java b/src/main/java/org/onap/policy/clamp/clds/util/LoggingUtils.java index b5f9837b0..4145844a2 100644 --- a/src/main/java/org/onap/policy/clamp/clds/util/LoggingUtils.java +++ b/src/main/java/org/onap/policy/clamp/clds/util/LoggingUtils.java @@ -58,13 +58,19 @@ public class LoggingUtils { private static final String DATE_FORMATTER_ISO = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; - /** String constant for messages ENTERING, EXITING, etc. */ + /** + * String constant for messages ENTERING, EXITING, etc. + */ private static final String EMPTY_MESSAGE = ""; - /** Logger delegate. */ + /** + * Logger delegate. + */ private final Logger mlogger; - /** Automatic UUID, overrideable per adapter or per invocation. */ + /** + * Automatic UUID, overrideable per adapter or per invocation. + */ private static final UUID sInstanceUUID = UUID.randomUUID(); /** @@ -98,7 +104,7 @@ public class LoggingUtils { * Set time related logging variables in thread local data via MDC. * * @param beginTimeStamp Start time - * @param endTimeStamp End time + * @param endTimeStamp End time */ public static void setTimeContext(@NotNull Date beginTimeStamp, @NotNull Date endTimeStamp) { MDC.put("EntryTimestamp", generateTimestampStr(beginTimeStamp)); @@ -109,9 +115,9 @@ public class LoggingUtils { /** * Set response related logging variables in thread local data via MDC. * - * @param code Response code ("0" indicates success) + * @param code Response code ("0" indicates success) * @param description Response description - * @param className class name of invoking class + * @param className class name of invoking class */ public static void setResponseContext(String code, String description, String className) { MDC.put("ResponseCode", code); @@ -123,7 +129,7 @@ public class LoggingUtils { /** * Set target related logging variables in thread local data via MDC. * - * @param targetEntity Target entity (an external/sub component, for ex. "sdc") + * @param targetEntity Target entity (an external/sub component, for ex. "sdc") * @param targetServiceName Target service name (name of API invoked on target) */ public static void setTargetContext(String targetEntity, String targetServiceName) { @@ -134,7 +140,7 @@ public class LoggingUtils { /** * Set error related logging variables in thread local data via MDC. * - * @param code Error code + * @param code Error code * @param description Error description */ public static void setErrorContext(String code, String description) { @@ -175,7 +181,7 @@ public class LoggingUtils { /** * Report ENTERING marker. * - * @param request non-null incoming request (wrapper) + * @param request non-null incoming request (wrapper) * @param serviceName service name */ public void entering(HttpServletRequest request, String serviceName) { @@ -183,16 +189,16 @@ public class LoggingUtils { checkNotNull(request); // Extract MDC values from standard HTTP headers. final String requestId = - defaultToUuid(request.getHeader(OnapLogConstants.Headers.REQUEST_ID)); + defaultToUuid(request.getHeader(OnapLogConstants.Headers.REQUEST_ID)); final String invocationId = - defaultToUuid(request.getHeader(OnapLogConstants.Headers.INVOCATION_ID)); + defaultToUuid(request.getHeader(OnapLogConstants.Headers.INVOCATION_ID)); final String partnerName = - defaultToEmpty(request.getHeader(OnapLogConstants.Headers.PARTNER_NAME)); + defaultToEmpty(request.getHeader(OnapLogConstants.Headers.PARTNER_NAME)); // Default the partner name to the user name used to login to clamp if (partnerName.equalsIgnoreCase(EMPTY_MESSAGE)) { MDC.put(OnapLogConstants.Mdcs.PARTNER_NAME, - AuthorizationController.getPrincipalName(SecurityContextHolder.getContext())); + AuthorizationController.getPrincipalName(SecurityContextHolder.getContext())); } // Set standard MDCs. Override this entire method if you want to set @@ -200,7 +206,7 @@ public class LoggingUtils { // depending on where you need them to appear, OR extend the // ServiceDescriptor to add them. MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC) - .format(DateTimeFormatter.ofPattern(DATE_FORMATTER_ISO))); + .format(DateTimeFormatter.ofPattern(DATE_FORMATTER_ISO))); MDC.put(OnapLogConstants.Mdcs.REQUEST_ID, requestId); MDC.put(OnapLogConstants.Mdcs.INVOCATION_ID, invocationId); MDC.put(OnapLogConstants.Mdcs.CLIENT_IP_ADDRESS, defaultToEmpty(request.getRemoteAddr())); @@ -217,7 +223,7 @@ public class LoggingUtils { // Set the Response Status code to in progress MDC.put(OnapLogConstants.Mdcs.RESPONSE_STATUS_CODE, - OnapLogConstants.ResponseStatus.INPROGRESS.toString()); + OnapLogConstants.ResponseStatus.INPROGRESS.toString()); setElapsedTime(); this.mlogger.info(OnapLogConstants.Markers.ENTRY, "Entering"); @@ -226,18 +232,17 @@ public class LoggingUtils { /** * Report EXITING marker. * - * - * @param code response code - * @param descrption response description - * @param severity response severity - * @param status response status code + * @param code response code + * @param description response description + * @param severity response severity + * @param status response status code */ - public void exiting(int code, String descrption, Level severity, - OnapLogConstants.ResponseStatus status) { + public void exiting(int code, String description, Level severity, + OnapLogConstants.ResponseStatus status) { try { MDC.put(OnapLogConstants.Mdcs.RESPONSE_CODE, defaultToEmpty(code)); - MDC.put(OnapLogConstants.Mdcs.RESPONSE_DESCRIPTION, defaultToEmpty(descrption)); + MDC.put(OnapLogConstants.Mdcs.RESPONSE_DESCRIPTION, defaultToEmpty(description)); MDC.put(OnapLogConstants.Mdcs.RESPONSE_SEVERITY, defaultToEmpty(severity)); MDC.put(OnapLogConstants.Mdcs.RESPONSE_STATUS_CODE, defaultToEmpty(status)); @@ -249,12 +254,11 @@ public class LoggingUtils { } private void setElapsedTime() { - ZonedDateTime startTime = - ZonedDateTime.parse(MDC.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP), - DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC)); - ZonedDateTime endTime = ZonedDateTime.now(ZoneOffset.UTC); - long duration = ChronoUnit.MILLIS.between(startTime, endTime); - MDC.put(OnapLogConstants.Mdcs.ELAPSED_TIME, String.valueOf(duration)); + String entryTimestamp = MDC.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP); + MDC.put(OnapLogConstants.Mdcs.ELAPSED_TIME, String.valueOf(ChronoUnit.MILLIS + .between(ZonedDateTime.parse(entryTimestamp != null ? entryTimestamp : ZonedDateTime.now(ZoneOffset.UTC) + .format(DateTimeFormatter.ofPattern(DATE_FORMATTER_ISO)), + DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC)), ZonedDateTime.now(ZoneOffset.UTC)))); } /** @@ -271,13 +275,13 @@ public class LoggingUtils { * Report pending invocation with INVOKE marker, * setting standard ONAP logging headers automatically. * - * @param con The HTTP url connection - * @param targetEntity The target entity + * @param con The HTTP url connection + * @param targetEntity The target entity * @param targetServiceName The target service name * @return The HTTP url connection */ public HttpURLConnection invoke(final HttpURLConnection con, String targetEntity, - String targetServiceName) { + String targetServiceName) { return this.invokeGeneric(con, targetEntity, targetServiceName); } @@ -285,7 +289,7 @@ public class LoggingUtils { * Report pending invocation with INVOKE marker, * setting standard ONAP logging headers automatically. * - * @param targetEntity The target entity + * @param targetEntity The target entity * @param targetServiceName The target service name */ public void invoke(String targetEntity, String targetServiceName) { @@ -304,13 +308,13 @@ public class LoggingUtils { * Report pending invocation with INVOKE marker, * setting standard ONAP logging headers automatically. * - * @param con The HTTPS url connection - * @param targetEntity The target entity + * @param con The HTTPS url connection + * @param targetEntity The target entity * @param targetServiceName The target service name * @return The HTTPS url connection */ public HttpsURLConnection invokeHttps(final HttpsURLConnection con, String targetEntity, - String targetServiceName) { + String targetServiceName) { return this.invokeGeneric(con, targetEntity, targetServiceName); } @@ -319,7 +323,7 @@ public class LoggingUtils { */ public void invokeReturn() { MDC.put(OnapLogConstants.Mdcs.RESPONSE_STATUS_CODE, - OnapLogConstants.ResponseStatus.COMPLETE.toString()); + OnapLogConstants.ResponseStatus.COMPLETE.toString()); // Add the Invoke-return marker and clear the needed MDC this.mlogger.info(OnapLogConstants.Markers.INVOKE_RETURN, "INVOKE-RETURN"); invokeReturnContext(); @@ -328,7 +332,7 @@ public class LoggingUtils { /** * Dependency-free nullcheck. * - * @param in to be checked + * @param in to be checked * @param argument (and return) type * @return input arg */ @@ -368,16 +372,16 @@ public class LoggingUtils { /** * Set target related logging variables in thread local data via MDC. * - * @param targetEntity Target entity (an external/sub component, for ex. "sdc") + * @param targetEntity Target entity (an external/sub component, for ex. "sdc") * @param targetServiceName Target service name (name of API invoked on target) - * @param invocationId The invocation ID + * @param invocationId The invocation ID */ private void invokeContext(String targetEntity, String targetServiceName, String invocationId) { MDC.put(OnapLogConstants.Mdcs.TARGET_ENTITY, defaultToEmpty(targetEntity)); MDC.put(OnapLogConstants.Mdcs.TARGET_SERVICE_NAME, defaultToEmpty(targetServiceName)); MDC.put(OnapLogConstants.Mdcs.INVOCATIONID_OUT, invocationId); MDC.put(OnapLogConstants.Mdcs.INVOKE_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC) - .format(DateTimeFormatter.ofPattern(DATE_FORMATTER_ISO))); + .format(DateTimeFormatter.ofPattern(DATE_FORMATTER_ISO))); } /** @@ -392,15 +396,15 @@ public class LoggingUtils { } private T invokeGeneric(final T con, String targetEntity, - String targetServiceName) { + String targetServiceName) { final String invocationId = UUID.randomUUID().toString(); // Set standard HTTP headers on (southbound request) builder. con.setRequestProperty(OnapLogConstants.Headers.REQUEST_ID, - defaultToEmpty(MDC.get(OnapLogConstants.Mdcs.REQUEST_ID))); + defaultToEmpty(MDC.get(OnapLogConstants.Mdcs.REQUEST_ID))); con.setRequestProperty(OnapLogConstants.Headers.INVOCATION_ID, invocationId); con.setRequestProperty(OnapLogConstants.Headers.PARTNER_NAME, - defaultToEmpty(MDC.get(OnapLogConstants.Mdcs.PARTNER_NAME))); + defaultToEmpty(MDC.get(OnapLogConstants.Mdcs.PARTNER_NAME))); invokeContext(targetEntity, targetServiceName, invocationId); diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java index a10f6df7d..c6b44076f 100644 --- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java @@ -62,7 +62,7 @@ public class PdpGroupPayload { * Constructor that takes a list of actions in input. * * @param listOfPdpActions The list of actions that needs to be done. - * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} + * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} * @throws PdpGroupPayloadException in case of issues to read the listOfActions */ public PdpGroupPayload(final JsonElement listOfPdpActions) throws PdpGroupPayloadException { @@ -74,7 +74,7 @@ public class PdpGroupPayload { * This method converts the list of actions directly to the pdp payload query as String. * * @param listOfPdpActions The list of actions that needs to be done. - * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} + * e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]} * @return The string containing the PDP payload that can be sent directly * @throws PdpGroupPayloadException in case of issues to read the listOfActions */ @@ -115,12 +115,16 @@ public class PdpGroupPayload { newSubGroup.setPdpType(pdpSubGroup); newSubGroup.setAction(DeploymentSubGroup.Action.valueOf(action)); newSubGroup.setPolicies(Arrays.asList(new ToscaConceptIdentifier(policyName, policyVersion))); - // Then the group - DeploymentGroup newGroup = new DeploymentGroup(); - newGroup.setName(pdpGroup); - newGroup.setDeploymentSubgroups(Arrays.asList(newSubGroup)); // Add to deployment Groups structure - this.deploymentGroups.getGroups().add(newGroup); + this.deploymentGroups.getGroups().stream().filter(group -> + group.getName().equals(pdpGroup)).findFirst() + .ifPresentOrElse(group -> group.getDeploymentSubgroups().add(newSubGroup), + () -> { + DeploymentGroup newGroup = new DeploymentGroup(); + newGroup.setName(pdpGroup); + newGroup.setDeploymentSubgroups(new ArrayList<>(Arrays.asList(newSubGroup))); + this.deploymentGroups.getGroups().add(newGroup); + }); } /** diff --git a/src/test/resources/example/policy/pdp-group-multi-policies-payload.json b/src/test/resources/example/policy/pdp-group-multi-policies-payload.json index f8625ebbb..5e9c77f45 100644 --- a/src/test/resources/example/policy/pdp-group-multi-policies-payload.json +++ b/src/test/resources/example/policy/pdp-group-multi-policies-payload.json @@ -12,12 +12,7 @@ "version": "1.0.0" } ] - } - ] - }, - { - "name": "pdpgroup1", - "deploymentSubgroups": [ + }, { "pdpType": "pdpsubgroup1", "action": "POST", @@ -27,12 +22,7 @@ "version": "1.0.0" } ] - } - ] - }, - { - "name": "pdpgroup1", - "deploymentSubgroups": [ + }, { "pdpType": "pdpsubgroup1", "action": "POST", @@ -57,12 +47,7 @@ "version": "1.0.0" } ] - } - ] - }, - { - "name": "pdpgroup2", - "deploymentSubgroups": [ + }, { "pdpType": "pdpsubgroup2", "action": "POST", @@ -72,12 +57,7 @@ "version": "2.0.0" } ] - } - ] - }, - { - "name": "pdpgroup2", - "deploymentSubgroups": [ + }, { "pdpType": "pdpsubgroup2", "action": "DELETE", @@ -91,4 +71,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/src/test/resources/example/policy/pdp-group-policy-payload.json b/src/test/resources/example/policy/pdp-group-policy-payload.json index 44a48dacb..897fb43be 100644 --- a/src/test/resources/example/policy/pdp-group-policy-payload.json +++ b/src/test/resources/example/policy/pdp-group-policy-payload.json @@ -12,12 +12,7 @@ "version": "1.0.0" } ] - } - ] - }, - { - "name": "pdpGroup2", - "deploymentSubgroups": [ + }, { "pdpType": "pdpSubgroup2", "action": "POST", @@ -27,12 +22,7 @@ "version": "1.0.0" } ] - } - ] - }, - { - "name": "pdpGroup2", - "deploymentSubgroups": [ + }, { "pdpType": "pdpSubgroup1", "action": "POST", -- cgit 1.2.3-korg