aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java
blob: abf60247016691fc691a27af1aea1b100733f06c (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
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdcrests.errors;

import com.amdocs.zusammen.datatypes.response.Module;

import java.util.stream.Stream;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import org.openecomp.sdc.common.errors.ErrorCode;
import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.common.errors.SdcRuntimeException;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

public class ZusammenExceptionMapper implements ExceptionMapper<SdcRuntimeException> {
    private static final String ZUSAMMEN_DB_PREFIX = Module.ZDB + "-";
    static final String VLM_VSP_VERSION_ID_DOES_NOT_EXISTS =
            ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_NOT_EXIST;
    static final String VLM_VSP_ITEM_ID_DOES_NOT_EXISTS =
            ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_DOES_NOT_EXIST;
    static final String SUB_ENTITY_ID_DOES_NOT_EXISTS =
            ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ELEMENT_GET_INFO;
    static final String FAILED_TO_SYNC =
            ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_SYNC;
    static final String FAILED_TO_PUBLISH_OUT_OF_SYNC =
            ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode
                                              .ZU_ITEM_VERSION_PUBLISH_NOT_ALLOWED;

    private static final Logger LOGGER = LoggerFactory.getLogger(ZusammenExceptionMapper.class);

    @Override
    public Response toResponse(SdcRuntimeException exception) {
        return transform(exception);
    }

    private Response transform(SdcRuntimeException exception) {
        if (Stream.of(VLM_VSP_ITEM_ID_DOES_NOT_EXISTS, VLM_VSP_VERSION_ID_DOES_NOT_EXISTS)
                  .anyMatch(exception.getMessage()::contains)) {
            return generateSdcErrorResponse(Messages.ENTITY_NOT_FOUND, Response.Status.NOT_FOUND,
                    new SdcRuntimeException(Messages.ENTITY_NOT_FOUND.getErrorMessage(), exception));
        } else if (exception.getMessage().contains(SUB_ENTITY_ID_DOES_NOT_EXISTS)) {
            return generateSdcErrorResponse(Messages.SUB_ENTITY_NOT_FOUND, Response.Status.NOT_FOUND,
                    new SdcRuntimeException(Messages.SUB_ENTITY_NOT_FOUND.getErrorMessage(), exception));
        } else if (exception.getMessage().contains(FAILED_TO_SYNC)) {
            return generateSdcErrorResponse(Messages.FAILED_TO_SYNC, Response.Status.EXPECTATION_FAILED,
                    new SdcRuntimeException(Messages.FAILED_TO_SYNC.getErrorMessage(), exception));
        } else if (exception.getMessage().contains(FAILED_TO_PUBLISH_OUT_OF_SYNC)) {
            return generateSdcErrorResponse(Messages.FAILED_TO_PUBLISH_OUT_OF_SYNC, Response.Status.EXPECTATION_FAILED,
                    new SdcRuntimeException(Messages.FAILED_TO_PUBLISH_OUT_OF_SYNC.getErrorMessage(), exception));
        }

        return genericError(exception);
    }

    private Response generateSdcErrorResponse(Messages messages, Response.Status status, Exception exception) {
        ErrorCode errorCode = new ErrorCode.ErrorCodeBuilder()
                                      .withId(messages.name())
                                      .withMessage(exception.getMessage()).build();

        LOGGER.error(errorCode.message(), exception);
        return Response.status(status).entity(new ErrorCodeAndMessage(status, errorCode)).build();
    }

    private Response genericError(Exception exception) {
        ErrorCode errorCode = new GeneralErrorBuilder().build();
        LOGGER.error(errorCode.message(), exception);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                       .entity(new ErrorCodeAndMessage(Response.Status.INTERNAL_SERVER_ERROR, errorCode)).build();
    }
}