1 package org.onap.sdc.dcae.errormng;
3 import org.onap.sdc.dcae.catalog.asdc.ASDCException;
4 import org.springframework.http.HttpStatus;
5 import org.springframework.http.ResponseEntity;
9 public enum ErrConfMgr {
12 private static EnumMap<ApiType, Map<String, String>> sdcDcaeMsgIdMap;
13 public static final String AS_IS = "AS_IS";
14 private ResponseFormatManager responseFormatManager;
17 responseFormatManager = ResponseFormatManager.getInstance();
18 populateSdcDcaeMsgIdMap();
21 private void setSdcCatalogPolicyMapping(){
22 Map<String, String> map = new HashMap<>();
23 map.put("POL5000", AS_IS);
24 map.put("POL5001", "POL5500");
25 map.put("POL5002", "POL5501");
26 sdcDcaeMsgIdMap.put(ApiType.ALL_SDC_CATALOG, map);
29 private void setGetVfcmtMapping(){
30 Map<String, String> map = new HashMap<>();
31 map.put("SVC4063", AS_IS);
32 map.put("SVC4505", AS_IS);
33 sdcDcaeMsgIdMap.put(ApiType.GET_VFCMT, map);
36 private void setCreateNewVfcmtMapping(){
37 Map<String, String> map = new HashMap<>();
38 map.put("SVC4050", AS_IS);
39 map.put("SVC4126", AS_IS);
40 map.put("SVC4500", AS_IS);
41 map.put("SVC4062", AS_IS);
42 map.put("SVC4064", AS_IS);
43 map.put("SVC4065", AS_IS);
44 map.put("SVC4066", AS_IS);
45 map.put("SVC4067", AS_IS);
46 map.put("SVC4068", AS_IS);
47 map.put("SVC4069", AS_IS);
48 map.put("SVC4070", AS_IS);
49 map.put("SVC4071", AS_IS);
50 map.put("SVC4072", AS_IS);
51 map.put("SVC4073", AS_IS);
52 map.put("SVC4053", AS_IS);
53 map.put("POL5003", AS_IS);
54 // adding service referencing error handling to create scenario
55 map.put("SVC4063", AS_IS);
56 map.put("SVC4122", AS_IS);
57 map.put("SVC4124", AS_IS);
58 map.put("SVC4128", AS_IS);
59 map.put("SVC4125", AS_IS);
60 map.put("SVC4127", AS_IS);
61 map.put("SVC4086", AS_IS);
62 map.put("SVC4301", AS_IS);
63 sdcDcaeMsgIdMap.put(ApiType.CREATE_NEW_VFCMT, map);
66 private void setCloneVfcmtMapping(){
67 Map<String, String> map = new HashMap<>();
68 map.put("SVC4063", AS_IS);
69 map.put("SVC4505", AS_IS);
70 map.put("SVC4085", AS_IS);
71 map.put("SVC4080", AS_IS);
72 map.put("SVC4122", "SVC6010");
73 map.put("SVC4124", "SVC6010");
74 map.put("SVC4128", "SVC6010");
75 map.put("SVC4125", AS_IS);
76 map.put("SVC4127", "SVC6010");
77 map.put("SVC4086", AS_IS);
78 map.put("SVC4301", AS_IS);
79 map.put("SVC4086", AS_IS);
80 sdcDcaeMsgIdMap.put(ApiType.CLONE_VFCMT, map);
84 private void setGetServiceMapping(){
85 Map<String, String> map = new HashMap<>();
86 map.put("SVC4503", AS_IS);
87 map.put("SVC4642", "200");
88 sdcDcaeMsgIdMap.put(ApiType.GET_SERVICE, map);
91 private void setAttachToServiceMapping(){
92 Map<String, String> map = new HashMap<>();
93 map.put("SVC4063", "SVC6021");
94 map.put("SVC4122", "SVC6021");
95 map.put("SVC4124", "SVC6021");
96 map.put("SVC4128", "SVC6021");
97 map.put("SVC4125", AS_IS);
98 map.put("SVC4127", "SVC6021");
99 map.put("SVC4086", AS_IS);
100 map.put("SVC4301", AS_IS);
101 map.put("SVC4503", AS_IS);
102 sdcDcaeMsgIdMap.put(ApiType.ATTACH_TO_SERVICE, map);
105 private void setGetCdumpMapping(){
106 Map<String, String> map = new HashMap<>();
107 map.put("SVC4063", AS_IS);
108 map.put("SVC4505", AS_IS);
109 sdcDcaeMsgIdMap.put(ApiType.GET_CDUMP, map);
112 private void setGetModelMapping(){
113 Map<String, String> map = new HashMap<>();
114 map.put("SVC4063", AS_IS);
115 map.put("SVC4505", "SVC6031");
116 sdcDcaeMsgIdMap.put(ApiType.GET_MODEL, map);
119 private void setCheckoutResourceMapping(){
120 Map<String, String> map = new HashMap<>();
121 map.put("SVC4063", "SVC6021");
122 map.put("SVC4085", AS_IS);
123 map.put("SVC4080", AS_IS);
124 map.put("SVC4002", AS_IS);
125 sdcDcaeMsgIdMap.put(ApiType.CHECK_OUT_RESOURCE, map);
128 private void setCheckinResourceMapping(){
129 Map<String, String> map = new HashMap<>();
130 map.put("SVC4063", "SVC6021");
131 map.put("SVC4086", AS_IS);
132 map.put("SVC4301", AS_IS);
133 map.put("SVC4084", AS_IS);
134 map.put("SVC4085", AS_IS);
135 map.put("SVC4002", AS_IS);
136 sdcDcaeMsgIdMap.put(ApiType.CHECK_IN_RESOURCE, map);
139 private void setSaveCdumpMapping(){
140 Map<String, String> map = new HashMap<>();
141 map.put("SVC4063", "SVC6021");
142 map.put("SVC4122", "SVC6021");
143 map.put("SVC4124", "SVC6021");
144 map.put("SVC4128", "SVC6021");
145 map.put("SVC4125", AS_IS);
146 map.put("SVC4127", "SVC6021");
147 map.put("SVC4086", AS_IS);
148 map.put("SVC4301", AS_IS);
149 sdcDcaeMsgIdMap.put(ApiType.SAVE_CDUMP, map);
152 private void setSubmitBlueprintMapping(){
153 Map<String, String> map = new HashMap<>();
154 map.put("SVC4063", AS_IS);
155 map.put("SVC4505", "SVC6031");
156 map.put("SVC4503", AS_IS);
157 map.put("SVC4085", AS_IS);
158 map.put("SVC4080", AS_IS);
159 map.put("SVC4122", "SVC6033");
160 map.put("SVC4124", "SVC6033");
161 map.put("SVC4128", "SVC6033");
162 map.put("SVC4125", AS_IS);
163 map.put("SVC4127", "SVC6033");
164 map.put("SVC4086", AS_IS);
165 map.put("SVC4301", AS_IS);
166 sdcDcaeMsgIdMap.put(ApiType.SUBMIT_BLUEPRINT, map);
169 private void setGetRuleMapping(){
170 Map<String, String> map = new HashMap<>();
171 map.put("SVC4063", AS_IS);
172 sdcDcaeMsgIdMap.put(ApiType.GET_RULE_ARTIFACT, map);
175 private void setSaveRuleMapping(){
176 Map<String, String> map = new HashMap<>();
177 map.put("SVC4063", "SVC6036");
178 map.put("SVC4122", "SVC6036");
179 map.put("SVC4124", "SVC6036");
180 map.put("SVC4128", "SVC6036");
181 map.put("SVC4125", AS_IS);
182 map.put("SVC4127", "SVC6036");
183 map.put("SVC4086", AS_IS);
184 map.put("SVC4301", AS_IS);
185 map.put("SVC4000", "SVC6036");
186 sdcDcaeMsgIdMap.put(ApiType.SAVE_RULE_ARTIFACT, map);
189 private void setGetAllVfcmtMapping(){
190 Map<String, String> map = new HashMap<>();
191 map.put("SVC4642", "200");
192 sdcDcaeMsgIdMap.put(ApiType.GET_ALL_VFCMTS, map);
196 private void setDeleteReferenceMapping(){
197 Map<String, String> map = new HashMap<>();
198 map.put("POL5003", AS_IS);
199 map.put("SVC4063", AS_IS);
200 map.put("POL4050", AS_IS);
201 map.put("SVC4086", AS_IS);
202 map.put("SVC4301", AS_IS);
203 map.put("SVC4687", AS_IS);
204 sdcDcaeMsgIdMap.put(ApiType.DELETE_VFCMT_REFERENCE, map);
207 private void populateSdcDcaeMsgIdMap() {
208 sdcDcaeMsgIdMap = new EnumMap<>(ApiType.class);
209 setAttachToServiceMapping();
210 setCheckinResourceMapping();
211 setCheckoutResourceMapping();
212 setCloneVfcmtMapping();
213 setGetAllVfcmtMapping();
215 setCreateNewVfcmtMapping();
216 setGetCdumpMapping();
217 setGetModelMapping();
218 setSaveCdumpMapping();
219 setSaveRuleMapping();
220 setSubmitBlueprintMapping();
221 setGetServiceMapping();
222 setGetVfcmtMapping();
223 setSdcCatalogPolicyMapping();
224 setDeleteReferenceMapping();
227 public enum ApiType {
243 DELETE_VFCMT_REFERENCE
246 public ResponseFormat getResponseFormat(ActionStatus actionStatus, String notes, String... variables) {
247 return responseFormatManager.getResponseFormat(actionStatus, notes, variables);
250 public ResponseEntity buildErrorResponse(ActionStatus actionStatus, String notes, String... variables) {
251 ResponseFormat response = responseFormatManager.getResponseFormat(actionStatus, notes, variables);
252 return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus()));
255 public ResponseEntity buildErrorResponse(ActionStatus actionStatus) {
256 ResponseFormat response = responseFormatManager.getResponseFormat(actionStatus, "");
257 return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus()));
260 public ResponseEntity buildErrorResponse(BaseException baseException) {
261 ResponseFormat response = responseFormatManager.getResponseFormat(baseException);
262 return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus()));
265 public ResponseEntity buildErrorArrayResponse(List<ServiceException> errors) {
266 ResponseFormat response = responseFormatManager.getResponseFormat(errors);
267 return new ResponseEntity<>(response, HttpStatus.valueOf(response.getStatus()));
270 // ActionStatus determined by sdc to dcae mapping
271 public ActionStatus convertToDcaeActionStatus(String messageId, ApiType apiType) {
272 // try the apiType's specific mapping from SDC messageId to dcaeMessageId
273 String dcaeMessageId = sdcDcaeMsgIdMap.get(apiType).get(messageId);
274 // if no specific mapping found try the general mapping
275 if(null == dcaeMessageId)
276 dcaeMessageId = sdcDcaeMsgIdMap.get(ApiType.ALL_SDC_CATALOG).get(messageId);
277 // if no mapping found return general error
278 if(null == dcaeMessageId)
279 return ActionStatus.GENERAL_ERROR;
280 // if mapped to 'AS_IS' return 'AS_IS'
281 if(AS_IS.equals(dcaeMessageId))
282 return ActionStatus.AS_IS;
283 // for any other valid mapping fetch the ActionStatus by corresponding dcaeMessageId
284 return responseFormatManager.getMsgIdToActionStatusMap().get(dcaeMessageId);
287 public ResponseEntity handleException(Exception e, ApiType apiType, String... variables){
288 if (e instanceof ASDCException){
289 ASDCException se = (ASDCException)e;
290 ActionStatus status = convertToDcaeActionStatus(se.getMessageId(), apiType);
293 return buildErrorResponse(se);
295 return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
297 return buildErrorResponse(status, se.getMessage(), variables);
300 //TODO refactor - don't throw DcaeException
301 if (e instanceof DcaeException){
302 return buildErrorResponse((DcaeException)e);
304 return buildErrorResponse(ActionStatus.GENERAL_ERROR, e.getMessage());