summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrConfMgr.java
blob: de1d06bf6751dc6530c63b14b945e10de66bff9a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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());
	}
}