summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ResponseFormatManager.java
blob: ada790f83b810a2a5b200f128362980a68d40267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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(){

	}
}