Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / namingservice / NamingClientResponseValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.namingservice;
24
25 import java.io.IOException;
26 import java.util.List;
27 import org.apache.http.HttpStatus;
28 import org.onap.namingservice.model.NameGenDeleteResponse;
29 import org.onap.namingservice.model.NameGenResponse;
30 import org.onap.namingservice.model.NameGenResponseError;
31 import org.onap.namingservice.model.Respelement;
32 import org.onap.so.client.exception.BadResponseException;
33 import org.onap.so.logger.ErrorCode;
34 import org.onap.so.logger.MessageEnum;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Component;
39 import org.springframework.web.client.HttpStatusCodeException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @Component
43 public class NamingClientResponseValidator {
44     private static final Logger logger = LoggerFactory.getLogger(NamingClientResponseValidator.class);
45     private static final String INSTANCE_GROUP_NAME = "instance-group-name";
46     private static final String NO_RESPONSE_FROM_NAMING_SERVICE =
47             "Error did not receive a response from Naming Service.";
48     private static final String NULL_RESPONSE_FROM_NAMING_SERVICE =
49             "Error received a null response from Naming Service.";
50     private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
51
52     public String validateNameGenResponse(ResponseEntity<NameGenResponse> response) throws BadResponseException {
53         if (response == null) {
54             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), NO_RESPONSE_FROM_NAMING_SERVICE,
55                     "BPMN", ErrorCode.UnknownError.getValue(), NO_RESPONSE_FROM_NAMING_SERVICE);
56             throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
57         }
58
59         int responseCode = response.getStatusCodeValue();
60         String generatedName = "";
61         NameGenResponse responseBody = response.getBody();
62         if (responseBody == null) {
63             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(),
64                     NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
65                     NULL_RESPONSE_FROM_NAMING_SERVICE);
66             throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
67         }
68
69         if (isHttpCodeSuccess(responseCode)) {
70             logger.info("Successful Response from Naming Service");
71             List<Respelement> respList = responseBody.getElements();
72
73             if (respList != null) {
74                 for (int i = 0; i < respList.size(); i++) {
75                     Respelement respElement = respList.get(i);
76                     if (respElement != null) {
77                         String resourceName = respElement.getResourceName();
78                         if (INSTANCE_GROUP_NAME.equals(resourceName)) {
79                             generatedName = respElement.getResourceValue();
80                             break;
81                         }
82                     }
83                 }
84             }
85             return generatedName;
86         } else {
87             NameGenResponseError error = responseBody.getError();
88             String errorMessageString = NAMING_SERVICE_ERROR;
89             if (error != null) {
90                 errorMessageString = error.getMessage();
91             }
92             String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
93             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
94                     ErrorCode.DataError.getValue(), errorMessage);
95             throw new BadResponseException(errorMessage);
96         }
97     }
98
99     public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response)
100             throws BadResponseException {
101         if (response == null) {
102             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), NO_RESPONSE_FROM_NAMING_SERVICE,
103                     "BPMN", ErrorCode.UnknownError.getValue(), NO_RESPONSE_FROM_NAMING_SERVICE);
104             throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
105         }
106
107         int responseCode = response.getStatusCodeValue();
108         String responseMessage = "";
109         NameGenDeleteResponse responseBody = response.getBody();
110         if (responseBody == null) {
111             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(),
112                     NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
113                     NULL_RESPONSE_FROM_NAMING_SERVICE);
114             throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
115         }
116
117         if (isHttpCodeSuccess(responseCode)) {
118             logger.info("Successful Response from Naming Service");
119             return responseMessage;
120         } else {
121             String errorMessageString = NAMING_SERVICE_ERROR;
122
123             String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
124             logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
125                     ErrorCode.DataError.getValue(), errorMessage);
126             throw new BadResponseException(errorMessage);
127         }
128     }
129
130     private boolean isHttpCodeSuccess(int code) {
131         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
132     }
133
134     protected String formatError(HttpStatusCodeException e) throws IOException {
135         ObjectMapper mapper = new ObjectMapper();
136         NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
137         NameGenResponseError error = errorResponse.getError();
138
139         String errorMessageString = null;
140         if (error != null) {
141             errorMessageString = error.getMessage();
142         }
143         String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
144         logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
145                 ErrorCode.DataError.getValue(), errorMessage);
146         return errorMessage;
147     }
148
149 }