diff options
Diffstat (limited to 'dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng')
8 files changed, 743 insertions, 0 deletions
diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ActionStatus.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ActionStatus.java new file mode 100644 index 0000000..cac92f5 --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ActionStatus.java @@ -0,0 +1,48 @@ +package org.onap.sdc.dcae.errormng; + +public enum ActionStatus { + + OK, + CREATED, + NO_CONTENT, + NOT_ALLOWED, + GENERAL_ERROR, + INVALID_CONTENT, + NOT_FOUND, + CONFIGURATION_ERROR, + VES_SCHEMA_NOT_FOUND, + VES_SCHEMA_INVALID, + FLOW_TYPES_CONFIGURATION_ERROR, + CLONE_FAILED, + EMPTY_SERVICE_LIST, + MONITORING_TEMPLATE_ATTACHMENT_ERROR, + MISSING_TOSCA_FILE, + VALIDATE_TOSCA_ERROR, + SUBMIT_BLUEPRINT_ERROR, + GENERATE_BLUEPRINT_ERROR, + INVALID_RULE_FORMAT, + SAVE_RULE_FAILED, + RESOURCE_NOT_VFCMT_ERROR, + VFI_FETCH_ERROR, + USER_CONFLICT, + MISSING_RULE_DESCRIPTION, + MISSING_ACTION, + MISSING_ACTION_FIELD, + MISSING_CONCAT_VALUE, + INVALID_GROUP_CONDITION, + MISSING_CONDITION_ITEM, + MISSING_OPERAND, + INVALID_OPERATOR, + MISSING_ENTRY, + MISSING_DEFAULT_VALUE, + DUPLICATE_KEY, + ACTION_DEPENDENCY, + RULE_DEPENDENCY, + NODE_NOT_FOUND, + DELETE_RULE_FAILED, + TRANSLATE_FAILED, + CATALOG_NOT_AVAILABLE, + AUTH_ERROR, + DELETE_BLUEPRINT_FAILED, + AS_IS +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/BasicConfiguration.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/BasicConfiguration.java new file mode 100644 index 0000000..001109e --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/BasicConfiguration.java @@ -0,0 +1,5 @@ +package org.onap.sdc.dcae.errormng; + +public class BasicConfiguration { + +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/DcaeException.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/DcaeException.java new file mode 100644 index 0000000..60b8e0e --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/DcaeException.java @@ -0,0 +1,15 @@ +package org.onap.sdc.dcae.errormng; + +import org.springframework.http.HttpStatus; +import org.springframework.web.client.HttpClientErrorException; + +public class DcaeException extends BaseException { + +// public DcaeException(HttpClientErrorException theError) { +// super(theError); +// } + + public DcaeException(HttpStatus status, RequestError re){ + super(status, re); + } +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrConfMgr.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrConfMgr.java new file mode 100644 index 0000000..de1d06b --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrConfMgr.java @@ -0,0 +1,306 @@ +package org.onap.sdc.dcae.errormng; + +import org.onap.sdc.dcae.catalog.asdc.ASDCException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.*; + +public enum ErrConfMgr { + INSTANCE; + + private static EnumMap<ApiType, Map<String, String>> sdcDcaeMsgIdMap; + public static final String AS_IS = "AS_IS"; + private ResponseFormatManager responseFormatManager; + + ErrConfMgr() { + responseFormatManager = ResponseFormatManager.getInstance(); + populateSdcDcaeMsgIdMap(); + } + + private void setSdcCatalogPolicyMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("POL5000", AS_IS); + map.put("POL5001", "POL5500"); + map.put("POL5002", "POL5501"); + sdcDcaeMsgIdMap.put(ApiType.ALL_SDC_CATALOG, map); + } + + private void setGetVfcmtMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + map.put("SVC4505", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.GET_VFCMT, map); + } + + private void setCreateNewVfcmtMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4050", AS_IS); + map.put("SVC4126", AS_IS); + map.put("SVC4500", AS_IS); + map.put("SVC4062", AS_IS); + map.put("SVC4064", AS_IS); + map.put("SVC4065", AS_IS); + map.put("SVC4066", AS_IS); + map.put("SVC4067", AS_IS); + map.put("SVC4068", AS_IS); + map.put("SVC4069", AS_IS); + map.put("SVC4070", AS_IS); + map.put("SVC4071", AS_IS); + map.put("SVC4072", AS_IS); + map.put("SVC4073", AS_IS); + map.put("SVC4053", AS_IS); + map.put("POL5003", AS_IS); + // adding service referencing error handling to create scenario + map.put("SVC4063", AS_IS); + map.put("SVC4122", AS_IS); + map.put("SVC4124", AS_IS); + map.put("SVC4128", AS_IS); + map.put("SVC4125", AS_IS); + map.put("SVC4127", AS_IS); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.CREATE_NEW_VFCMT, map); + } + + private void setCloneVfcmtMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + map.put("SVC4505", AS_IS); + map.put("SVC4085", AS_IS); + map.put("SVC4080", AS_IS); + map.put("SVC4122", "SVC6010"); + map.put("SVC4124", "SVC6010"); + map.put("SVC4128", "SVC6010"); + map.put("SVC4125", AS_IS); + map.put("SVC4127", "SVC6010"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + map.put("SVC4086", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.CLONE_VFCMT, map); + } + + + private void setGetServiceMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4503", AS_IS); + map.put("SVC4642", "200"); + sdcDcaeMsgIdMap.put(ApiType.GET_SERVICE, map); + } + + private void setAttachToServiceMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", "SVC6021"); + map.put("SVC4122", "SVC6021"); + map.put("SVC4124", "SVC6021"); + map.put("SVC4128", "SVC6021"); + map.put("SVC4125", AS_IS); + map.put("SVC4127", "SVC6021"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + map.put("SVC4503", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.ATTACH_TO_SERVICE, map); + } + + private void setGetCdumpMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + map.put("SVC4505", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.GET_CDUMP, map); + } + + private void setGetModelMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + map.put("SVC4505", "SVC6031"); + sdcDcaeMsgIdMap.put(ApiType.GET_MODEL, map); + } + + private void setCheckoutResourceMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", "SVC6021"); + map.put("SVC4085", AS_IS); + map.put("SVC4080", AS_IS); + map.put("SVC4002", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.CHECK_OUT_RESOURCE, map); + } + + private void setCheckinResourceMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", "SVC6021"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + map.put("SVC4084", AS_IS); + map.put("SVC4085", AS_IS); + map.put("SVC4002", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.CHECK_IN_RESOURCE, map); + } + + private void setSaveCdumpMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", "SVC6021"); + map.put("SVC4122", "SVC6021"); + map.put("SVC4124", "SVC6021"); + map.put("SVC4128", "SVC6021"); + map.put("SVC4125", AS_IS); + map.put("SVC4127", "SVC6021"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.SAVE_CDUMP, map); + } + + private void setSubmitBlueprintMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + map.put("SVC4505", "SVC6031"); + map.put("SVC4503", AS_IS); + map.put("SVC4085", AS_IS); + map.put("SVC4080", AS_IS); + map.put("SVC4122", "SVC6033"); + map.put("SVC4124", "SVC6033"); + map.put("SVC4128", "SVC6033"); + map.put("SVC4125", AS_IS); + map.put("SVC4127", "SVC6033"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.SUBMIT_BLUEPRINT, map); + } + + private void setGetRuleMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.GET_RULE_ARTIFACT, map); + } + + private void setSaveRuleMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4063", "SVC6036"); + map.put("SVC4122", "SVC6036"); + map.put("SVC4124", "SVC6036"); + map.put("SVC4128", "SVC6036"); + map.put("SVC4125", AS_IS); + map.put("SVC4127", "SVC6036"); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + map.put("SVC4000", "SVC6036"); + sdcDcaeMsgIdMap.put(ApiType.SAVE_RULE_ARTIFACT, map); + } + + private void setGetAllVfcmtMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("SVC4642", "200"); + sdcDcaeMsgIdMap.put(ApiType.GET_ALL_VFCMTS, map); + } + + + private void setDeleteReferenceMapping(){ + Map<String, String> map = new HashMap<>(); + map.put("POL5003", AS_IS); + map.put("SVC4063", AS_IS); + map.put("POL4050", AS_IS); + map.put("SVC4086", AS_IS); + map.put("SVC4301", AS_IS); + map.put("SVC4687", AS_IS); + sdcDcaeMsgIdMap.put(ApiType.DELETE_VFCMT_REFERENCE, map); + } + + private void populateSdcDcaeMsgIdMap() { + sdcDcaeMsgIdMap = new EnumMap<>(ApiType.class); + setAttachToServiceMapping(); + setCheckinResourceMapping(); + setCheckoutResourceMapping(); + setCloneVfcmtMapping(); + setGetAllVfcmtMapping(); + setGetRuleMapping(); + setCreateNewVfcmtMapping(); + setGetCdumpMapping(); + setGetModelMapping(); + setSaveCdumpMapping(); + setSaveRuleMapping(); + setSubmitBlueprintMapping(); + setGetServiceMapping(); + setGetVfcmtMapping(); + setSdcCatalogPolicyMapping(); + setDeleteReferenceMapping(); + } + + public enum ApiType { + CREATE_NEW_VFCMT, + GET_ALL_VFCMTS, + CLONE_VFCMT, + GET_VFCMT, + GET_SERVICE, + ATTACH_TO_SERVICE, + GET_CDUMP, + GET_MODEL, + CHECK_OUT_RESOURCE, + CHECK_IN_RESOURCE, + SAVE_CDUMP, + SUBMIT_BLUEPRINT, + GET_RULE_ARTIFACT, + SAVE_RULE_ARTIFACT, + ALL_SDC_CATALOG, + DELETE_VFCMT_REFERENCE + } + + public ResponseFormat getResponseFormat(ActionStatus actionStatus, String notes, String... variables) { + return responseFormatManager.getResponseFormat(actionStatus, notes, variables); + } + + public ResponseEntity buildErrorResponse(ActionStatus actionStatus, String notes, String... variables) { + ResponseFormat response = responseFormatManager.getResponseFormat(actionStatus, notes, variables); + return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); + } + + public ResponseEntity buildErrorResponse(ActionStatus actionStatus) { + ResponseFormat response = responseFormatManager.getResponseFormat(actionStatus, ""); + return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); + } + + public ResponseEntity buildErrorResponse(BaseException baseException) { + ResponseFormat response = responseFormatManager.getResponseFormat(baseException); + return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); + } + + public ResponseEntity buildErrorArrayResponse(List<ServiceException> errors) { + ResponseFormat response = responseFormatManager.getResponseFormat(errors); + return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus())); + } + + // ActionStatus determined by sdc to dcae mapping + public ActionStatus convertToDcaeActionStatus(String messageId, ApiType apiType) { + // try the apiType's specific mapping from SDC messageId to dcaeMessageId + String dcaeMessageId = sdcDcaeMsgIdMap.get(apiType).get(messageId); + // if no specific mapping found try the general mapping + if(null == dcaeMessageId) + dcaeMessageId = sdcDcaeMsgIdMap.get(ApiType.ALL_SDC_CATALOG).get(messageId); + // if no mapping found return general error + if(null == dcaeMessageId) + return ActionStatus.GENERAL_ERROR; + // if mapped to 'AS_IS' return 'AS_IS' + if(AS_IS.equals(dcaeMessageId)) + return ActionStatus.AS_IS; + // for any other valid mapping fetch the ActionStatus by corresponding dcaeMessageId + return responseFormatManager.getMsgIdToActionStatusMap().get(dcaeMessageId); + } + + public ResponseEntity handleException(Exception e, ApiType apiType, String... variables){ + if (e instanceof ASDCException){ + ASDCException se = (ASDCException)e; + ActionStatus status = convertToDcaeActionStatus(se.getMessageId(), apiType); + switch (status) { + case AS_IS: + return buildErrorResponse(se); + case OK: + return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); + default: + return buildErrorResponse(status, se.getMessage(), variables); + } + } + //TODO refactor - don't throw DcaeException + if (e instanceof DcaeException){ + return buildErrorResponse((DcaeException)e); + } + return buildErrorResponse(ActionStatus.GENERAL_ERROR, e.getMessage()); + } +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfiguration.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfiguration.java new file mode 100644 index 0000000..8f6f5af --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfiguration.java @@ -0,0 +1,46 @@ +package org.onap.sdc.dcae.errormng; + +import java.util.Map; + +/** + * Example: + * VES_SCHEMA_INVALID: { + code: 500, + message: "Error – Failed to parse VES Schema file '%1'. [%2]", + messageId: "SVC6007" + } + + key will be "VES_SCHEMA_INVALID" + value is the json object containing code, message, messageId + */ + +import org.onap.sdc.dcae.errormng.BasicConfiguration; + +public class ErrorConfiguration extends BasicConfiguration { + + private Map<String, ErrorInfo> errors; + + public Map<String, ErrorInfo> getErrors() { + return errors; + } + + public void setErrors(Map<String, ErrorInfo> errors) { + this.errors = errors; + } + + public ErrorInfo getErrorInfo(String key) { + ErrorInfo clone = null; + ErrorInfo other = errors.get(key); + if (other != null) { + clone = new ErrorInfo(); + clone.cloneData(other); + } + return clone; + } + + @Override + public String toString() { + return "ErrorConfiguration [errors=" + errors + "]"; + } + +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfigurationLoader.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfigurationLoader.java new file mode 100644 index 0000000..8b7ab44 --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfigurationLoader.java @@ -0,0 +1,121 @@ +package org.onap.sdc.dcae.errormng; + +import org.apache.commons.lang.ArrayUtils; +import org.onap.sdc.common.onaplog.OnapLoggerDebug; +import org.onap.sdc.common.onaplog.OnapLoggerError; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.onap.sdc.common.onaplog.Enums.LogLevel; + +public class ErrorConfigurationLoader { + + private static ErrorConfigurationLoader instance; + private String jettyBase; + private ErrorConfiguration errorConfiguration = new ErrorConfiguration(); + private OnapLoggerError errLogger = OnapLoggerError.getInstance(); + private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance(); + + public ErrorConfigurationLoader(String sourcePath) { + jettyBase = sourcePath; + loadErrorConfiguration(); + instance = this; + } + + private void loadErrorConfiguration(){ + + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "ErrorConfigurationLoader: Trying to load error configuration"); + if (jettyBase == null) { + String msg = "Couldn't resolve jetty.base environmental variable"; + errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg); + throw new ExceptionInInitializerError (msg + ". Failed to load error configuration files... aborting"); + } + + String path = jettyBase + "/config/dcae-be"; + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "jetty.base={}", jettyBase); + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Configuration Path={}", path); + + File dir = new File(path); + File[] files = dir.listFiles(new FilenameFilter() { + @Override public boolean accept(File dir, String name) { + return name.equals("error-configuration.yaml"); + } + }); + + if (ArrayUtils.isEmpty(files)) { + String msg = "No error configuration files found"; + errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg); + throw new ExceptionInInitializerError (msg); + }else if (files.length>1){ + String msg = "Multiple configuration files found. Make sure only one file exists. Path: "+ path; + errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg); + throw new ExceptionInInitializerError (msg); + } + else { + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Loading error configuration file: {}", files[0].getName()); + try { + errorConfiguration = parseErrConfFileAndSaveToMap(files[0].getCanonicalPath()); +// convertToUsefulMaps(errorConfiguration); + } catch (IOException e) { + String msg = "Exception thrown while trying to read the error configuration file path. File="+files[0].getName(); + errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg); + throw new ExceptionInInitializerError (msg); + } + if(errorConfiguration == null){ + String msg = "Error configuration file couldn't be parsed"; + errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg); + throw new ExceptionInInitializerError (msg); + } + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Error Configuration: {}", errorConfiguration.toString()); + } + } + + + private ErrorConfiguration parseErrConfFileAndSaveToMap(String fullFileName) { + + Yaml yaml = new Yaml(); + + InputStream in = null; + ErrorConfiguration errorConfiguration = null; + try { + + File f = new File(fullFileName); + if (false == f.exists()) { + errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} cannot be found. Ignore reading configuration.", fullFileName); + return null; + } + in = Files.newInputStream(Paths.get(fullFileName)); + + errorConfiguration = yaml.loadAs(in, ErrorConfiguration.class); + + } catch (Exception e) { + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to convert yaml file {} to object. {}", fullFileName, e); + return null; + } + finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to close input stream {}", e.getMessage()); + } + } + } + + return errorConfiguration; + } + + ErrorConfiguration getErrorConfiguration() { + return errorConfiguration; + } + + public static ErrorConfigurationLoader getErrorConfigurationLoader() { + return instance; + } +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorInfo.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorInfo.java new file mode 100644 index 0000000..3ec3cef --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorInfo.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.dcae.errormng; + +import org.onap.sdc.common.onaplog.OnapLoggerDebug; +import org.onap.sdc.common.onaplog.OnapLoggerError; +import org.onap.sdc.common.onaplog.Enums.LogLevel; + +public class ErrorInfo { + + private Integer code; + private String message; + private String messageId; + private ErrorInfoType errorInfoType; + + private static final String SVC_PREFIX = "SVC"; + private static final String POL_PREFIX = "POL"; + + private static OnapLoggerError errLogger = OnapLoggerError.getInstance(); + private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance(); + + + public ErrorInfo() { + this.errorInfoType = ErrorInfoType.OK; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getMessageId() { + return messageId; + } + + public void setMessageId(String messageId) { + // Determining the type of error + if (messageId == null || "200".equals(messageId) || "201".equals(messageId) || "204".equals(messageId)) { + this.errorInfoType = ErrorInfoType.OK; + } else if (messageId.startsWith(SVC_PREFIX)) { + this.errorInfoType = ErrorInfoType.SERVICE_EXCEPTION; + } else if (messageId.startsWith(POL_PREFIX)) { + this.errorInfoType = ErrorInfoType.POLICY_EXCEPTION; + } else { + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Error: unexpected error message ID {}, should start with {} or {}", messageId, SVC_PREFIX, POL_PREFIX); + } + this.messageId = messageId; + } + + public ErrorInfoType getErrorInfoType() { + return this.errorInfoType; + } + + public void cloneData(ErrorInfo other) { + this.code = other.getCode(); + this.message = other.getMessage(); + this.messageId = other.getMessageId(); + this.errorInfoType = other.errorInfoType; + } + + @Override + public String toString() { + return "ErrorInfo [code=" + code + ", messageId=" + messageId + ", message=" + message + "]"; + } + + public enum ErrorInfoType { + OK, POLICY_EXCEPTION, SERVICE_EXCEPTION + } + +} diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ResponseFormatManager.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ResponseFormatManager.java new file mode 100644 index 0000000..ada790f --- /dev/null +++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ResponseFormatManager.java @@ -0,0 +1,103 @@ +package org.onap.sdc.dcae.errormng; + +import org.onap.sdc.common.onaplog.OnapLoggerDebug; +import org.onap.sdc.common.onaplog.OnapLoggerError; +import org.onap.sdc.common.onaplog.Enums.LogLevel; +import org.onap.sdc.dcae.errormng.ErrorInfo.ErrorInfoType; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ResponseFormatManager { + + private volatile static ResponseFormatManager instance; + private static ErrorConfiguration errorConfiguration; + private static Map<String, ActionStatus> msgIdToActionStatusMap = new HashMap<>(); + private static OnapLoggerError errLogger = OnapLoggerError.getInstance(); + private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance(); + + + public static ResponseFormatManager getInstance() { + if (instance == null) { + instance = init(); + } + return instance; + } + + private static synchronized ResponseFormatManager init() { + if (instance == null) { + instance = new ResponseFormatManager(); + errorConfiguration = ErrorConfigurationLoader.getErrorConfigurationLoader().getErrorConfiguration(); + convertToActionMap(); + } + return instance; + } + + ResponseFormat getResponseFormat(ActionStatus actionStatus, String notes, String... variables) { + ErrorInfo errorInfo = errorConfiguration.getErrorInfo(actionStatus.name()); + if (errorInfo == null) { + debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "failed to locate {} in error configuration", actionStatus.name()); + errorInfo = errorConfiguration.getErrorInfo(ActionStatus.GENERAL_ERROR.name()); + } + + ResponseFormat responseFormat = new ResponseFormat(errorInfo.getCode()); + String errorMessage = errorInfo.getMessage(); + String errorMessageId = errorInfo.getMessageId(); + ErrorInfoType errorInfoType = errorInfo.getErrorInfoType(); + responseFormat.setNotes(notes); + + if (errorInfoType==ErrorInfoType.SERVICE_EXCEPTION) { + responseFormat.setServiceException(new ServiceException(errorMessageId, errorMessage, variables)); + } + else if (errorInfoType==ErrorInfoType.POLICY_EXCEPTION) { + responseFormat.setPolicyException(new PolicyException(errorMessageId, errorMessage, variables)); + } + else if (errorInfoType==ErrorInfoType.OK) { + responseFormat.setOkResponseInfo(new OkResponseInfo(errorMessageId, errorMessage, variables)); + } + return responseFormat; + } + + ResponseFormat getResponseFormat(BaseException baseException) { + + ResponseFormat responseFormat = new ResponseFormat(baseException.getRawStatusCode()); + AbstractSdncException e = baseException.getRequestError().getError(); + + if (e instanceof ServiceException) { + responseFormat.setServiceException((ServiceException)e); + } + else if (e instanceof PolicyException) { + responseFormat.setPolicyException((PolicyException)e); + } + else { + responseFormat.setOkResponseInfo((OkResponseInfo)e); + } + return responseFormat; + } + + ResponseFormat getResponseFormat(List<ServiceException> errors) { + ResponseFormat responseFormat = new ResponseFormat(400); + responseFormat.setServiceExceptions(errors); + return responseFormat; + } + + public Map<String, ActionStatus> getMsgIdToActionStatusMap() { + return msgIdToActionStatusMap; + } + + private static void convertToActionMap() { + Map<String, ErrorInfo> errors = errorConfiguration.getErrors(); + + if(errors!=null){ + errors.forEach((k, v) -> { + debugLogger.log(LogLevel.DEBUG, ResponseFormatManager.class.getName(), "{}, {}", v.getMessageId(), k); + msgIdToActionStatusMap.put(v.getMessageId(), ActionStatus.valueOf(k)); + }); + } + } + + public ResponseFormatManager(){ + + } +} |