2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.namingservice;
23 import java.io.IOException;
24 import java.util.List;
26 import org.apache.http.HttpStatus;
27 import org.onap.namingservice.model.NameGenDeleteResponse;
28 import org.onap.namingservice.model.NameGenResponse;
29 import org.onap.namingservice.model.NameGenResponseError;
30 import org.onap.namingservice.model.Respelement;
31 import org.onap.so.client.exception.BadResponseException;
32 import org.onap.so.logger.MessageEnum;
33 import org.onap.so.logger.MsoLogger;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
36 import org.springframework.web.client.HttpStatusCodeException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
41 public class NamingClientResponseValidator {
42 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, NamingClientResponseValidator.class);
43 private static final String INSTANCE_GROUP_NAME = "instance-group-name";
44 private static final String NO_RESPONSE_FROM_NAMING_SERVICE = "Error did not receive a response from Naming Service.";
45 private static final String NULL_RESPONSE_FROM_NAMING_SERVICE = "Error received a null response from Naming Service.";
46 private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
48 public String validateNameGenResponse(ResponseEntity<NameGenResponse> response) throws BadResponseException {
49 if (response == null) {
50 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
51 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_NAMING_SERVICE);
52 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
55 int responseCode = response.getStatusCodeValue();
56 String generatedName = "";
57 NameGenResponse responseBody = response.getBody();
58 if (responseBody == null) {
59 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
60 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NULL_RESPONSE_FROM_NAMING_SERVICE);
61 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
64 if (isHttpCodeSuccess(responseCode)) {
65 msoLogger.info("Successful Response from Naming Service");
66 List<Respelement> respList = responseBody.getElements();
68 if (respList != null) {
69 for (int i=0; i < respList.size(); i++) {
70 Respelement respElement = respList.get(i);
71 if (respElement != null) {
72 String resourceName = respElement.getResourceName();
73 if (INSTANCE_GROUP_NAME.equals(resourceName)) {
74 generatedName = respElement.getResourceValue();
82 NameGenResponseError error = responseBody.getError();
83 String errorMessageString = NAMING_SERVICE_ERROR;
85 errorMessageString = error.getMessage();
87 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
88 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
89 MsoLogger.ErrorCode.DataError, errorMessage);
90 throw new BadResponseException(errorMessage);
94 public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response) throws BadResponseException {
95 if (response == null) {
96 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
97 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_NAMING_SERVICE);
98 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
101 int responseCode = response.getStatusCodeValue();
102 String responseMessage = "";
103 NameGenDeleteResponse responseBody = response.getBody();
104 if (responseBody == null) {
105 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
106 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NULL_RESPONSE_FROM_NAMING_SERVICE);
107 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
110 if (isHttpCodeSuccess(responseCode)) {
111 msoLogger.info("Successful Response from Naming Service");
112 return responseMessage;
114 String errorMessageString = NAMING_SERVICE_ERROR;
116 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
117 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
118 MsoLogger.ErrorCode.DataError, errorMessage);
119 throw new BadResponseException(errorMessage);
123 private boolean isHttpCodeSuccess(int code) {
124 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
127 protected String formatError(HttpStatusCodeException e) throws IOException {
128 ObjectMapper mapper = new ObjectMapper();
129 NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
130 NameGenResponseError error = errorResponse.getError();
132 String errorMessageString = null;
134 errorMessageString = error.getMessage();
136 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
137 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
138 MsoLogger.ErrorCode.DataError, errorMessage);