107f98b5f1ac7c4a5b93a2e9c55578bea66cca90
[sdnc/apps.git] /
1 /*
2  * ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.servicedecomposition.exception;
19
20 import javax.ws.rs.core.Response.Status;
21
22 public class DiscoveryException extends Exception {
23     public static enum Error {
24         FETCH_RESOURCE_FAILED("SD.0001", "A&AI query failed: %s"),
25         RELATIONSHIP_LINK_PARSE_ERROR("SD.0002", "Error in parsing relationship link"),
26         SERVICE_INSTANCE_NOT_FOUND("SD.0003", "Service Instance not Found"),
27         SERVICE_RELATIONSHIP_PARSE_ERROR("SD.0004", "Error in parsing service-instance relationships: %s"),
28         INVALID_URL("SD.0005", "Invalid request URL, missing parameter: service-instance-id"),
29         MISSING_HEADER("SD.0006", "Missing header parameter: %s"),
30         GENERAL_FAILURE("SD.0007", "An error occurred: %s"),
31         UNAUTHORIZED("SD.00008", "Unauthorized");
32
33         private final String responseCode;
34         private final String message;
35
36         private Error(String responseCode, String message) {
37             this.responseCode = responseCode;
38             this.message = message;
39         }
40
41         public String getMessage(Object... args) {
42             if (args == null || args.length == 0) {
43                 return this.message;
44             }
45             return String.format(this.message, args);
46         }
47
48         public String getResponseCode() {
49             return this.responseCode;
50         }
51     }
52
53     private static final long serialVersionUID = -4874149714911165454L;
54
55     private final Status httpStatus;
56     private String responseCode;
57
58     public DiscoveryException(Error error, Status httpStatus, Object... args) {
59         super(error.getMessage(args));
60         if (httpStatus == null) {
61             throw new NullPointerException("httpStatus");
62         }
63         this.responseCode = error.getResponseCode();
64         this.httpStatus = httpStatus;
65     }
66
67     public DiscoveryException(Error error, Exception cause, Object... args) {
68         super(error.getMessage(args), cause);
69         this.httpStatus = Status.INTERNAL_SERVER_ERROR;
70     }
71
72     public String getResponseCode() {
73         return this.responseCode;
74     }
75
76     public Status getHttpStatus() {
77         return this.httpStatus;
78     }
79 }