2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.client.namingservice;
25 import java.io.IOException;
26 import java.util.List;
27 import com.google.common.base.Strings;
28 import org.apache.http.HttpStatus;
29 import org.onap.namingservice.model.NameGenDeleteResponse;
30 import org.onap.namingservice.model.NameGenResponse;
31 import org.onap.namingservice.model.NameGenResponseError;
32 import org.onap.namingservice.model.Respelement;
33 import org.onap.so.client.exception.BadResponseException;
34 import org.onap.so.logger.ErrorCode;
35 import org.onap.so.logger.MessageEnum;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Component;
40 import org.springframework.web.client.HttpStatusCodeException;
41 import com.fasterxml.jackson.databind.ObjectMapper;
44 public class NamingClientResponseValidator {
45 private static final Logger logger = LoggerFactory.getLogger(NamingClientResponseValidator.class);
46 private static final String INSTANCE_GROUP_NAME = "instance-group-name";
47 private static final String NO_RESPONSE_FROM_NAMING_SERVICE =
48 "Error did not receive a response from Naming Service.";
49 private static final String NULL_RESPONSE_FROM_NAMING_SERVICE =
50 "Error received a null response from Naming Service.";
51 private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
53 public String validateNameGenResponse(ResponseEntity<NameGenResponse> response) throws BadResponseException {
54 if (response == null) {
55 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(),
56 NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
57 NO_RESPONSE_FROM_NAMING_SERVICE);
58 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
61 int responseCode = response.getStatusCodeValue();
62 String generatedName = "";
63 NameGenResponse responseBody = response.getBody();
64 if (responseBody == null) {
65 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(),
66 NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
67 NULL_RESPONSE_FROM_NAMING_SERVICE);
68 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
71 if (isHttpCodeSuccess(responseCode)) {
72 logger.info("Successful Response from Naming Service");
73 List<Respelement> respList = responseBody.getElements();
75 if (respList != null) {
76 for (int i = 0; i < respList.size(); i++) {
77 Respelement respElement = respList.get(i);
78 if (respElement != null) {
79 String resourceName = respElement.getResourceName();
80 if (INSTANCE_GROUP_NAME.equals(resourceName)) {
81 generatedName = respElement.getResourceValue();
89 NameGenResponseError error = responseBody.getError();
90 String errorMessageString = NAMING_SERVICE_ERROR;
92 errorMessageString = error.getMessage();
94 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
95 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
96 ErrorCode.DataError.getValue(), errorMessage);
97 throw new BadResponseException(errorMessage);
101 public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response)
102 throws BadResponseException {
103 if (response == null) {
104 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(),
105 NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
106 NO_RESPONSE_FROM_NAMING_SERVICE);
107 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
110 int responseCode = response.getStatusCodeValue();
111 String responseMessage = "";
112 NameGenDeleteResponse responseBody = response.getBody();
113 if (responseBody == null) {
114 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(),
115 NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
116 NULL_RESPONSE_FROM_NAMING_SERVICE);
117 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
120 if (isHttpCodeSuccess(responseCode)) {
121 logger.info("Successful Response from Naming Service");
122 return responseMessage;
124 String errorMessageString = NAMING_SERVICE_ERROR;
126 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
127 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
128 ErrorCode.DataError.getValue(), errorMessage);
129 throw new BadResponseException(errorMessage);
133 private boolean isHttpCodeSuccess(int code) {
134 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
137 protected String formatError(HttpStatusCodeException e) throws IOException {
138 ObjectMapper mapper = new ObjectMapper();
139 NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
140 NameGenResponseError error = errorResponse.getError();
142 String errorMessageString = null;
144 errorMessageString = error.getMessage();
146 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
147 logger.error(Strings.repeat("{} ", 5), MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
148 ErrorCode.DataError.getValue(), errorMessage);