DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / errormng / ResponseFormatManager.java
1 package org.onap.sdc.dcae.errormng;
2
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;
7
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 public class ResponseFormatManager {
13
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();
19
20
21         public static ResponseFormatManager getInstance() {
22                 if (instance == null) {
23                         instance = init();
24                 }
25                 return instance;
26         }
27
28         private static synchronized ResponseFormatManager init() {
29                 if (instance == null) {
30                         instance = new ResponseFormatManager();
31                         errorConfiguration = ErrorConfigurationLoader.getErrorConfigurationLoader().getErrorConfiguration();
32                         convertToActionMap();
33                 }
34                 return instance;
35         }
36
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());
42                 }
43                 
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);
49                 
50                 if (errorInfoType==ErrorInfoType.SERVICE_EXCEPTION) {
51                         responseFormat.setServiceException(new ServiceException(errorMessageId, errorMessage, variables));
52                 } 
53                 else if (errorInfoType==ErrorInfoType.POLICY_EXCEPTION) {
54                         responseFormat.setPolicyException(new PolicyException(errorMessageId, errorMessage, variables));
55                 } 
56                 else if (errorInfoType==ErrorInfoType.OK) {
57                         responseFormat.setOkResponseInfo(new OkResponseInfo(errorMessageId, errorMessage, variables));
58                 }
59                 return responseFormat;
60         }
61
62         ResponseFormat getResponseFormat(BaseException baseException) {
63
64                 ResponseFormat responseFormat = new ResponseFormat(baseException.getRawStatusCode());
65                 AbstractSdncException e = baseException.getRequestError().getError();
66
67                 if (e instanceof ServiceException) {
68                         responseFormat.setServiceException((ServiceException)e);
69                 }
70                 else if (e instanceof PolicyException) {
71                         responseFormat.setPolicyException((PolicyException)e);
72                 }
73                 else  {
74                         responseFormat.setOkResponseInfo((OkResponseInfo)e);
75                 }
76                 return responseFormat;
77         }
78
79         ResponseFormat getResponseFormat(List<ServiceException> errors) {
80                 ResponseFormat responseFormat = new ResponseFormat(400);
81                 responseFormat.setServiceExceptions(errors);
82                 return responseFormat;
83         }
84
85         public Map<String, ActionStatus> getMsgIdToActionStatusMap() {
86                 return msgIdToActionStatusMap;
87         }
88
89         private static void convertToActionMap() {
90                 Map<String, ErrorInfo> errors = errorConfiguration.getErrors();
91
92                 if(errors!=null){
93                         errors.forEach((k, v) -> {
94                                 debugLogger.log(LogLevel.DEBUG, ResponseFormatManager.class.getName(), "{}, {}", v.getMessageId(), k);
95                                 msgIdToActionStatusMap.put(v.getMessageId(), ActionStatus.valueOf(k));
96                         });
97                 }
98         }
99
100         public ResponseFormatManager(){
101
102         }
103 }