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;
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;
42 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class NamingClientResponseValidator {
46 private static final Logger logger = LoggerFactory.getLogger(NamingClientResponseValidator.class);
47 private static final String INSTANCE_GROUP_NAME = "instance-group-name";
48 private static final String NO_RESPONSE_FROM_NAMING_SERVICE = "Error did not receive a response from Naming Service.";
49 private static final String NULL_RESPONSE_FROM_NAMING_SERVICE = "Error received a null response from Naming Service.";
50 private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
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(),
56 NO_RESPONSE_FROM_NAMING_SERVICE);
57 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
60 int responseCode = response.getStatusCodeValue();
61 String generatedName = "";
62 NameGenResponse responseBody = response.getBody();
63 if (responseBody == null) {
64 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), NULL_RESPONSE_FROM_NAMING_SERVICE,
65 "BPMN", ErrorCode.UnknownError.getValue(),
66 NULL_RESPONSE_FROM_NAMING_SERVICE);
67 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
70 if (isHttpCodeSuccess(responseCode)) {
71 logger.info("Successful Response from Naming Service");
72 List<Respelement> respList = responseBody.getElements();
74 if (respList != null) {
75 for (int i=0; i < respList.size(); i++) {
76 Respelement respElement = respList.get(i);
77 if (respElement != null) {
78 String resourceName = respElement.getResourceName();
79 if (INSTANCE_GROUP_NAME.equals(resourceName)) {
80 generatedName = respElement.getResourceValue();
88 NameGenResponseError error = responseBody.getError();
89 String errorMessageString = NAMING_SERVICE_ERROR;
91 errorMessageString = error.getMessage();
93 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
94 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
95 ErrorCode.DataError.getValue(), errorMessage);
96 throw new BadResponseException(errorMessage);
100 public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response) throws BadResponseException {
101 if (response == null) {
102 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), NO_RESPONSE_FROM_NAMING_SERVICE,
103 "BPMN", ErrorCode.UnknownError.getValue(),
104 NO_RESPONSE_FROM_NAMING_SERVICE);
105 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
108 int responseCode = response.getStatusCodeValue();
109 String responseMessage = "";
110 NameGenDeleteResponse responseBody = response.getBody();
111 if (responseBody == null) {
112 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), NULL_RESPONSE_FROM_NAMING_SERVICE,
113 "BPMN", ErrorCode.UnknownError.getValue(),
114 NULL_RESPONSE_FROM_NAMING_SERVICE);
115 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
118 if (isHttpCodeSuccess(responseCode)) {
119 logger.info("Successful Response from Naming Service");
120 return responseMessage;
122 String errorMessageString = NAMING_SERVICE_ERROR;
124 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
125 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
126 ErrorCode.DataError.getValue(), errorMessage);
127 throw new BadResponseException(errorMessage);
131 private boolean isHttpCodeSuccess(int code) {
132 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
135 protected String formatError(HttpStatusCodeException e) throws IOException {
136 ObjectMapper mapper = new ObjectMapper();
137 NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
138 NameGenResponseError error = errorResponse.getError();
140 String errorMessageString = null;
142 errorMessageString = error.getMessage();
144 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
145 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
146 ErrorCode.DataError.getValue(), errorMessage);