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 org.onap.so.logger.LoggingAnchor;
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 SERVICE_INSTANCE_NAME = "Service-Instance-Name";
48 private static final String NO_RESPONSE_FROM_NAMING_SERVICE =
49 "Error did not receive a response from Naming Service.";
50 private static final String NULL_RESPONSE_FROM_NAMING_SERVICE =
51 "Error received a null response from Naming Service.";
52 private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
54 public String validateNameGenResponse(ResponseEntity<NameGenResponse> response) throws BadResponseException {
55 if (response == null) {
56 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(),
57 NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
58 NO_RESPONSE_FROM_NAMING_SERVICE);
59 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
62 int responseCode = response.getStatusCodeValue();
63 String generatedName = "";
64 NameGenResponse responseBody = response.getBody();
65 if (responseBody == null) {
66 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(),
67 NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
68 NULL_RESPONSE_FROM_NAMING_SERVICE);
69 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
72 if (isHttpCodeSuccess(responseCode)) {
73 logger.info("Successful Response from Naming Service");
74 List<Respelement> respList = responseBody.getElements();
76 if (respList != null) {
77 for (int i = 0; i < respList.size(); i++) {
78 Respelement respElement = respList.get(i);
79 if (respElement != null) {
80 String resourceName = respElement.getResourceName();
81 if (INSTANCE_GROUP_NAME.equals(resourceName)) {
82 generatedName = respElement.getResourceValue();
84 } else if (SERVICE_INSTANCE_NAME.equals(resourceName)) {
85 generatedName = respElement.getResourceValue();
93 NameGenResponseError error = responseBody.getError();
94 String errorMessageString = NAMING_SERVICE_ERROR;
96 errorMessageString = error.getMessage();
98 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
99 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
100 ErrorCode.DataError.getValue(), errorMessage);
101 throw new BadResponseException(errorMessage);
105 public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response)
106 throws BadResponseException {
107 if (response == null) {
108 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(),
109 NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
110 NO_RESPONSE_FROM_NAMING_SERVICE);
111 throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
114 int responseCode = response.getStatusCodeValue();
115 String responseMessage = "";
116 NameGenDeleteResponse responseBody = response.getBody();
117 if (responseBody == null) {
118 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(),
119 NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN", ErrorCode.UnknownError.getValue(),
120 NULL_RESPONSE_FROM_NAMING_SERVICE);
121 throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
124 if (isHttpCodeSuccess(responseCode)) {
125 logger.info("Successful Response from Naming Service");
126 return responseMessage;
128 String errorMessageString = NAMING_SERVICE_ERROR;
130 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
131 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
132 ErrorCode.DataError.getValue(), errorMessage);
133 throw new BadResponseException(errorMessage);
137 private boolean isHttpCodeSuccess(int code) {
138 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
141 protected String formatError(HttpStatusCodeException e) throws IOException {
142 ObjectMapper mapper = new ObjectMapper();
143 NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
144 NameGenResponseError error = errorResponse.getError();
146 String errorMessageString = null;
148 errorMessageString = error.getMessage();
150 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
151 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
152 ErrorCode.DataError.getValue(), errorMessage);