From c1c9cc614de08b728d173a56c8c460814c18616e Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Tue, 27 Aug 2019 14:29:12 +0900 Subject: Fix issue in mso-api-handler-infra Either log or rethrow this exception Define a constant instead of duplicating this literal Immediately throw this expression instead of assigning it to the temporary variable Remove the declaration of thrown exception 'org.json.JSONException' which is a runtime exception Issue-ID: SO-1841 Change-Id: I193f21ec2c4214863c8b51faf25fc3561d6be818 Signed-off-by: Parshad Patel --- .../infra/rest/handler/AbstractRestHandler.java | 4 +- .../tenantisolation/TenantIsolationRequest.java | 2 + .../tenantisolation/helpers/SDCClientHelper.java | 70 ++++++++++++---------- .../ActivateVnfStatusOperationalEnvironment.java | 19 +++--- 4 files changed, 50 insertions(+), 45 deletions(-) (limited to 'mso-api-handlers/mso-api-handler-infra/src') diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java index e11732ddc4..20e27a5265 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java @@ -111,7 +111,7 @@ public abstract class AbstractRestHandler { String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException { InfraActiveRequests dup = null; try { - if (!(instanceName == null && requestScope.equals("service") && (action == Action.createInstance + if (!(instanceName == null && "service".equals(requestScope) && (action == Action.createInstance || action == Action.activateInstance || action == Action.assignInstance))) { dup = infraActiveRequestsClient.checkInstanceNameDuplicate(instanceIdMap, instanceName, requestScope); } @@ -175,7 +175,7 @@ public abstract class AbstractRestHandler { selfLinkUrl = Optional.of(new URL(aUrl.getProtocol(), aUrl.getHost(), aUrl.getPort(), selfLinkPath)); } catch (Exception e) { selfLinkUrl = Optional.empty(); // ignore - logger.error(e.getMessage()); + logger.error("Exception in buildSelfLinkUrl", e); } return selfLinkUrl; } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/TenantIsolationRequest.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/TenantIsolationRequest.java index efdc52b837..6449b0bb75 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/TenantIsolationRequest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/TenantIsolationRequest.java @@ -92,6 +92,7 @@ public class TenantIsolationRequest { requestJSON = mapper.writeValueAsString(request.getRequestDetails()); } catch (JsonProcessingException e) { + logger.error("Exception in JSON processing", e); throw new ValidationException("Parse ServiceInstanceRequest to JSON string", true); } @@ -325,6 +326,7 @@ public class TenantIsolationRequest { orchestrationFilterParams.put(queryParam, value); } } catch (Exception e) { + logger.error("Exception in getOrchestrationFilters", e); throw new ValidationException(e.getMessage(), true); } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/helpers/SDCClientHelper.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/helpers/SDCClientHelper.java index b5ba9281a6..18ca6d3086 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/helpers/SDCClientHelper.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/helpers/SDCClientHelper.java @@ -50,12 +50,19 @@ public class SDCClientHelper { private static Logger logger = LoggerFactory.getLogger(SDCClientHelper.class); private static final String SDC_CONTENT_TYPE = "application/json"; private static final String SDC_ACCEPT_TYPE = "application/json"; - private static String PARTIAL_SDC_URI = "/sdc/v1/catalog/services/"; + private static final String PARTIAL_SDC_URI = "/sdc/v1/catalog/services/"; - private static String MESSAGE_UNDEFINED_ERROR = "Undefined Error Message!"; - private static String MESSAGE_UNEXPECTED_FORMAT = "Unexpected response format from SDC."; + private static final String MESSAGE_UNDEFINED_ERROR = "Undefined Error Message!"; + private static final String MESSAGE_UNEXPECTED_FORMAT = "Unexpected response format from SDC."; private final HttpClientFactory httpClientFactory = new HttpClientFactory(); + private static final String STATUS_CODE = "statusCode"; + private static final String MESSAGE = "message"; + private static final String MESSAGE_ID = "messageId"; + private static final String REQUEST_ERROR = "requestError"; + private static final String SERVICE_EXCEPTION = "serviceException"; + private static final String POLICY_EXCEPTION = "policyException"; + @Value("${mso.sdc.endpoint}") private String sdcEndpoint; @Value("${mso.sdc.activate.userid}") @@ -89,12 +96,11 @@ public class SDCClientHelper { ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError) .build(); - ValidateException validateException = new ValidateException.Builder( + + throw new ValidateException.Builder( " SDC credentials 'mso.sdc.client.auth' not setup in properties file!", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo) .build(); - - throw validateException; } URL url = new URL(urlString); @@ -114,12 +120,12 @@ public class SDCClientHelper { sdcResponseJsonObj = enhanceJsonResponse(new JSONObject(responseData), statusCode); } catch (Exception ex) { - logger.debug("calling SDC Exception message: {}", ex.getMessage()); + logger.debug("calling SDC Exception message:", ex); String errorMessage = " Encountered Error while calling SDC POST Activate. " + ex.getMessage(); logger.debug(errorMessage); - sdcResponseJsonObj.put("statusCode", String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); - sdcResponseJsonObj.put("messageId", ""); - sdcResponseJsonObj.put("message", errorMessage); + sdcResponseJsonObj.put(STATUS_CODE, String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); + sdcResponseJsonObj.put(MESSAGE_ID, ""); + sdcResponseJsonObj.put(MESSAGE, errorMessage); } return sdcResponseJsonObj; @@ -139,11 +145,9 @@ public class SDCClientHelper { ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError) .build(); - ValidateException validateException = - new ValidateException.Builder("Bad request could not post payload", HttpStatus.SC_BAD_REQUEST, - ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(ex).errorInfo(errorLoggerInfo).build(); - throw validateException; + throw new ValidateException.Builder("Bad request could not post payload", HttpStatus.SC_BAD_REQUEST, + ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(ex).errorInfo(errorLoggerInfo).build(); } } @@ -154,7 +158,7 @@ public class SDCClientHelper { * @param statusCode - int * @return enhancedAsdcResponseJsonObj - JSONObject object */ - public JSONObject enhanceJsonResponse(JSONObject sdcResponseJsonObj, int statusCode) throws JSONException { + public JSONObject enhanceJsonResponse(JSONObject sdcResponseJsonObj, int statusCode) { JSONObject enhancedAsdcResponseJsonObj = new JSONObject(); @@ -163,31 +167,31 @@ public class SDCClientHelper { if (statusCode == Response.Status.ACCEPTED.getStatusCode()) { // Accepted enhancedAsdcResponseJsonObj.put("distributionId", sdcResponseJsonObj.get("distributionId")); - enhancedAsdcResponseJsonObj.put("statusCode", Integer.toString(statusCode)); - enhancedAsdcResponseJsonObj.put("messageId", ""); - enhancedAsdcResponseJsonObj.put("message", "Success"); + enhancedAsdcResponseJsonObj.put(STATUS_CODE, Integer.toString(statusCode)); + enhancedAsdcResponseJsonObj.put(MESSAGE_ID, ""); + enhancedAsdcResponseJsonObj.put(MESSAGE, "Success"); } else { // error - if (sdcResponseJsonObj.has("requestError")) { - JSONObject requestErrorObj = sdcResponseJsonObj.getJSONObject("requestError"); - if (sdcResponseJsonObj.getJSONObject("requestError").has("serviceException")) { - message = requestErrorObj.getJSONObject("serviceException").getString("text"); - messageId = requestErrorObj.getJSONObject("serviceException").getString("messageId"); + if (sdcResponseJsonObj.has(REQUEST_ERROR)) { + JSONObject requestErrorObj = sdcResponseJsonObj.getJSONObject(REQUEST_ERROR); + if (sdcResponseJsonObj.getJSONObject(REQUEST_ERROR).has(SERVICE_EXCEPTION)) { + message = requestErrorObj.getJSONObject(SERVICE_EXCEPTION).getString("text"); + messageId = requestErrorObj.getJSONObject(SERVICE_EXCEPTION).getString(MESSAGE_ID); } - if (sdcResponseJsonObj.getJSONObject("requestError").has("policyException")) { - message = requestErrorObj.getJSONObject("policyException").getString("text"); - messageId = requestErrorObj.getJSONObject("policyException").getString("messageId"); + if (sdcResponseJsonObj.getJSONObject(REQUEST_ERROR).has(POLICY_EXCEPTION)) { + message = requestErrorObj.getJSONObject(POLICY_EXCEPTION).getString("text"); + messageId = requestErrorObj.getJSONObject(POLICY_EXCEPTION).getString(MESSAGE_ID); } - enhancedAsdcResponseJsonObj.put("statusCode", Integer.toString(statusCode)); - enhancedAsdcResponseJsonObj.put("messageId", messageId); - enhancedAsdcResponseJsonObj.put("message", message); + enhancedAsdcResponseJsonObj.put(STATUS_CODE, Integer.toString(statusCode)); + enhancedAsdcResponseJsonObj.put(MESSAGE_ID, messageId); + enhancedAsdcResponseJsonObj.put(MESSAGE, message); } else { // unexpected format - enhancedAsdcResponseJsonObj.put("statusCode", + enhancedAsdcResponseJsonObj.put(STATUS_CODE, String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); - enhancedAsdcResponseJsonObj.put("messageId", MESSAGE_UNDEFINED_ERROR); - enhancedAsdcResponseJsonObj.put("message", MESSAGE_UNEXPECTED_FORMAT); + enhancedAsdcResponseJsonObj.put(MESSAGE_ID, MESSAGE_UNDEFINED_ERROR); + enhancedAsdcResponseJsonObj.put(MESSAGE, MESSAGE_UNEXPECTED_FORMAT); } } return enhancedAsdcResponseJsonObj; @@ -214,7 +218,7 @@ public class SDCClientHelper { * @return String json * @throws JSONException */ - public String buildJsonWorkloadContext(String workloadContext) throws JSONException { + public String buildJsonWorkloadContext(String workloadContext) { return new JSONObject().put("workloadContext", workloadContext).toString(); } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/process/ActivateVnfStatusOperationalEnvironment.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/process/ActivateVnfStatusOperationalEnvironment.java index 3005abaf9b..3226a0c313 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/process/ActivateVnfStatusOperationalEnvironment.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/process/ActivateVnfStatusOperationalEnvironment.java @@ -61,16 +61,14 @@ public class ActivateVnfStatusOperationalEnvironment { private OperationalEnvServiceModelStatus queryServiceModelResponse = null; private boolean isOverallSuccess = false; - private final int RETRY_COUNT_ZERO = 0; - private final String ERROR_REASON_ABORTED = "ABORTED"; - private final String RECOVERY_ACTION_RETRY = "RETRY"; - private final String RECOVERY_ACTION_ABORT = "ABORT"; - private final String RECOVERY_ACTION_SKIP = "SKIP"; - private final String DISTRIBUTION_STATUS_OK = DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString(); - private final String DISTRIBUTION_STATUS_ERROR = DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString(); - private final String DISTRIBUTION_STATUS_SENT = "SENT"; - - private final String MESSAGE_UNDEFINED_ID = "Undefined Error Message!"; + private static final int RETRY_COUNT_ZERO = 0; + private static final String ERROR_REASON_ABORTED = "ABORTED"; + private static final String RECOVERY_ACTION_RETRY = "RETRY"; + private static final String RECOVERY_ACTION_ABORT = "ABORT"; + private static final String RECOVERY_ACTION_SKIP = "SKIP"; + private static final String DISTRIBUTION_STATUS_OK = DistributionStatus.DISTRIBUTION_COMPLETE_OK.toString(); + private static final String DISTRIBUTION_STATUS_ERROR = DistributionStatus.DISTRIBUTION_COMPLETE_ERROR.toString(); + private static final String DISTRIBUTION_STATUS_SENT = "SENT"; @Autowired private ActivateVnfDBHelper dbHelper; @@ -130,6 +128,7 @@ public class ActivateVnfStatusOperationalEnvironment { } } catch (Exception e) { + logger.error("Exception in execute", e); requestDb.updateInfraFailureCompletion(e.getMessage(), this.origRequestId, this.queryServiceModelResponse.getVnfOperationalEnvId()); } -- cgit 1.2.3-korg