re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ResponseFormatManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import org.openecomp.sdc.be.config.ConfigurationManager;
24 import org.openecomp.sdc.be.config.ErrorConfiguration;
25 import org.openecomp.sdc.be.config.ErrorInfo;
26 import org.openecomp.sdc.be.config.ErrorInfo.ErrorInfoType;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import org.openecomp.sdc.exception.OkResponseInfo;
30 import org.openecomp.sdc.exception.PolicyException;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.openecomp.sdc.exception.ServiceException;
33
34 public class ResponseFormatManager {
35
36     private volatile static ResponseFormatManager instance;
37     private static ConfigurationManager configurationManager;
38     private static final Logger log = Logger.getLogger(ResponseFormatManager.class);
39
40     public static ResponseFormatManager getInstance() {
41         if (instance == null) {
42
43             instance = init();
44         }
45         return instance;
46     }
47
48     private static synchronized ResponseFormatManager init() {
49         if (instance == null) {
50             instance = new ResponseFormatManager();
51             configurationManager = ConfigurationManager.getConfigurationManager();
52         }
53         return instance;
54     }
55
56     public ResponseFormat getResponseFormat(ActionStatus responseEnum, String... variables) {
57         ErrorConfiguration errorConfiguration = configurationManager.getErrorConfiguration();
58         ErrorInfo errorInfo = errorConfiguration.getErrorInfo(responseEnum.name());
59         if (errorInfo == null) {
60             log.debug("failed to locate {} in error configuration", responseEnum);
61             errorInfo = errorConfiguration.getErrorInfo(ActionStatus.GENERAL_ERROR.name());
62         }
63         ResponseFormat errorResponseWrapper = new ResponseFormat(errorInfo.getCode());
64         String errorMessage = errorInfo.getMessage();
65         String errorMessageId = errorInfo.getMessageId();
66         ErrorInfoType errorInfoType = errorInfo.getErrorInfoType();
67         if (errorInfoType.equals(ErrorInfoType.SERVICE_EXCEPTION)) {
68             errorResponseWrapper.setServiceException(new ServiceException(errorMessageId, errorMessage, variables));
69         } else if (errorInfoType.equals(ErrorInfoType.POLICY_EXCEPTION)) {
70             errorResponseWrapper.setPolicyException(new PolicyException(errorMessageId, errorMessage, variables));
71         } else if (errorInfoType.equals(ErrorInfoType.OK)) {
72             errorResponseWrapper.setOkResponseInfo(new OkResponseInfo(errorMessageId, errorMessage, variables));
73         }
74         return errorResponseWrapper;
75     }
76 }