4100cc5bf76eb4048803897ec7759f49d048527b
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdcrests.errors;
18
19 import com.amdocs.zusammen.datatypes.response.Module;
20
21 import java.util.stream.Stream;
22
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.ext.ExceptionMapper;
25
26 import org.openecomp.sdc.common.errors.ErrorCode;
27 import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
28 import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
29 import org.openecomp.sdc.common.errors.Messages;
30 import org.openecomp.sdc.common.errors.SdcRuntimeException;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33
34 public class ZusammenExceptionMapper implements ExceptionMapper<SdcRuntimeException> {
35     private static final String ZUSAMMEN_DB_PREFIX = Module.ZDB + "-";
36     private static final String VLM_VSP_VERSION_ID_DOES_NOT_EXISTS =
37             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_NOT_EXIST;
38     private static final String VLM_VSP_ITEM_ID_DOES_NOT_EXISTS =
39             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_DOES_NOT_EXIST;
40     private static final String SUB_ENTITY_ID_DOES_NOT_EXISTS =
41             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ELEMENT_GET_INFO;
42     private static final String FAILED_TO_SYNC =
43             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_SYNC;
44     private static final String FAILED_TO_PUBLISH_OUT_OF_SYNC =
45             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode
46                                               .ZU_ITEM_VERSION_PUBLISH_NOT_ALLOWED;
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ZusammenExceptionMapper.class);
49
50     @Override
51     public Response toResponse(SdcRuntimeException exception) {
52         return transform(exception);
53     }
54
55     private Response transform(SdcRuntimeException exception) {
56         if (Stream.of(VLM_VSP_ITEM_ID_DOES_NOT_EXISTS, VLM_VSP_VERSION_ID_DOES_NOT_EXISTS)
57                   .anyMatch(exception.getMessage()::contains)) {
58             return generateSdcErrorResponse(Messages.ENTITY_NOT_FOUND, Response.Status.NOT_FOUND,
59                     new SdcRuntimeException(Messages.ENTITY_NOT_FOUND.getErrorMessage(), exception));
60         } else if (exception.getMessage().contains(SUB_ENTITY_ID_DOES_NOT_EXISTS)) {
61             return generateSdcErrorResponse(Messages.SUB_ENTITY_NOT_FOUND, Response.Status.NOT_FOUND,
62                     new SdcRuntimeException(Messages.SUB_ENTITY_NOT_FOUND.getErrorMessage(), exception));
63         } else if (exception.getMessage().contains(FAILED_TO_SYNC)) {
64             return generateSdcErrorResponse(Messages.FAILED_TO_SYNC, Response.Status.EXPECTATION_FAILED,
65                     new SdcRuntimeException(Messages.FAILED_TO_SYNC.getErrorMessage(), exception));
66         } else if (exception.getMessage().contains(FAILED_TO_PUBLISH_OUT_OF_SYNC)) {
67             return generateSdcErrorResponse(Messages.FAILED_TO_PUBLISH_OUT_OF_SYNC, Response.Status.EXPECTATION_FAILED,
68                     new SdcRuntimeException(Messages.FAILED_TO_PUBLISH_OUT_OF_SYNC.getErrorMessage(), exception));
69         }
70
71         return genericError(exception);
72     }
73
74     private Response generateSdcErrorResponse(Messages messages, Response.Status status, Exception exception) {
75         ErrorCode errorCode = new ErrorCode.ErrorCodeBuilder()
76                                       .withId(messages.name())
77                                       .withMessage(exception.getMessage()).build();
78
79         LOGGER.error(errorCode.message(), exception);
80         return Response.status(status).entity(new ErrorCodeAndMessage(status, errorCode)).build();
81     }
82
83     private Response genericError(Exception exception) {
84         ErrorCode errorCode = new GeneralErrorBuilder().build();
85         LOGGER.error(errorCode.message(), exception);
86         return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
87                        .entity(new ErrorCodeAndMessage(Response.Status.INTERNAL_SERVER_ERROR, errorCode)).build();
88     }
89 }