1 package org.onap.sdc.dcae.errormng;
3 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
4 import org.onap.sdc.common.onaplog.OnapLoggerError;
5 import org.onap.sdc.common.onaplog.Enums.LogLevel;
6 import org.onap.sdc.dcae.errormng.ErrorInfo.ErrorInfoType;
8 import java.util.HashMap;
12 public class ResponseFormatManager {
14 private volatile static ResponseFormatManager instance;
15 private static ErrorConfiguration errorConfiguration;
16 private static Map<String, ActionStatus> msgIdToActionStatusMap = new HashMap<>();
17 private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
18 private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
21 public static ResponseFormatManager getInstance() {
22 if (instance == null) {
28 private static synchronized ResponseFormatManager init() {
29 if (instance == null) {
30 instance = new ResponseFormatManager();
31 errorConfiguration = ErrorConfigurationLoader.getErrorConfigurationLoader().getErrorConfiguration();
37 ResponseFormat getResponseFormat(ActionStatus actionStatus, String notes, String... variables) {
38 ErrorInfo errorInfo = errorConfiguration.getErrorInfo(actionStatus.name());
39 if (errorInfo == null) {
40 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "failed to locate {} in error configuration", actionStatus.name());
41 errorInfo = errorConfiguration.getErrorInfo(ActionStatus.GENERAL_ERROR.name());
44 ResponseFormat responseFormat = new ResponseFormat(errorInfo.getCode());
45 String errorMessage = errorInfo.getMessage();
46 String errorMessageId = errorInfo.getMessageId();
47 ErrorInfoType errorInfoType = errorInfo.getErrorInfoType();
48 responseFormat.setNotes(notes);
50 if (errorInfoType==ErrorInfoType.SERVICE_EXCEPTION) {
51 responseFormat.setServiceException(new ServiceException(errorMessageId, errorMessage, variables));
53 else if (errorInfoType==ErrorInfoType.POLICY_EXCEPTION) {
54 responseFormat.setPolicyException(new PolicyException(errorMessageId, errorMessage, variables));
56 else if (errorInfoType==ErrorInfoType.OK) {
57 responseFormat.setOkResponseInfo(new OkResponseInfo(errorMessageId, errorMessage, variables));
59 return responseFormat;
62 ResponseFormat getResponseFormat(BaseException baseException) {
64 ResponseFormat responseFormat = new ResponseFormat(baseException.getRawStatusCode());
65 AbstractSdncException e = baseException.getRequestError().getError();
67 if (e instanceof ServiceException) {
68 responseFormat.setServiceException((ServiceException)e);
70 else if (e instanceof PolicyException) {
71 responseFormat.setPolicyException((PolicyException)e);
74 responseFormat.setOkResponseInfo((OkResponseInfo)e);
76 return responseFormat;
79 ResponseFormat getResponseFormat(List<ServiceException> errors) {
80 ResponseFormat responseFormat = new ResponseFormat(400);
81 responseFormat.setServiceExceptions(errors);
82 return responseFormat;
85 public Map<String, ActionStatus> getMsgIdToActionStatusMap() {
86 return msgIdToActionStatusMap;
89 private static void convertToActionMap() {
90 Map<String, ErrorInfo> errors = errorConfiguration.getErrors();
93 errors.forEach((k, v) -> {
94 debugLogger.log(LogLevel.DEBUG, ResponseFormatManager.class.getName(), "{}, {}", v.getMessageId(), k);
95 msgIdToActionStatusMap.put(v.getMessageId(), ActionStatus.valueOf(k));
100 public ResponseFormatManager(){