Sync Integ to Master
[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.exception.OkResponseInfo;
29 import org.openecomp.sdc.exception.PolicyException;
30 import org.openecomp.sdc.exception.ResponseFormat;
31 import org.openecomp.sdc.exception.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class ResponseFormatManager {
36
37     private volatile static ResponseFormatManager instance;
38     private static ConfigurationManager configurationManager;
39     private static final Logger log = LoggerFactory.getLogger(ResponseFormatManager.class);
40
41     public static ResponseFormatManager getInstance() {
42         if (instance == null) {
43
44             instance = init();
45         }
46         return instance;
47     }
48
49     private static synchronized ResponseFormatManager init() {
50         if (instance == null) {
51             instance = new ResponseFormatManager();
52             configurationManager = ConfigurationManager.getConfigurationManager();
53         }
54         return instance;
55     }
56
57     public ResponseFormat getResponseFormat(ActionStatus responseEnum, String... variables) {
58         ErrorConfiguration errorConfiguration = configurationManager.getErrorConfiguration();
59         ErrorInfo errorInfo = errorConfiguration.getErrorInfo(responseEnum.name());
60         if (errorInfo == null) {
61             log.debug("failed to locate {} in error configuration", responseEnum);
62             errorInfo = errorConfiguration.getErrorInfo(ActionStatus.GENERAL_ERROR.name());
63         }
64         ResponseFormat errorResponseWrapper = new ResponseFormat(errorInfo.getCode());
65         String errorMessage = errorInfo.getMessage();
66         String errorMessageId = errorInfo.getMessageId();
67         ErrorInfoType errorInfoType = errorInfo.getErrorInfoType();
68         if (errorInfoType.equals(ErrorInfoType.SERVICE_EXCEPTION)) {
69             errorResponseWrapper.setServiceException(new ServiceException(errorMessageId, errorMessage, variables));
70         } else if (errorInfoType.equals(ErrorInfoType.POLICY_EXCEPTION)) {
71             errorResponseWrapper.setPolicyException(new PolicyException(errorMessageId, errorMessage, variables));
72         } else if (errorInfoType.equals(ErrorInfoType.OK)) {
73             errorResponseWrapper.setOkResponseInfo(new OkResponseInfo(errorMessageId, errorMessage, variables));
74         }
75         return errorResponseWrapper;
76     }
77 }