5bf531753e02ba564ad9436078deccf05fb3ac95
[so.git] /
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 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;
42
43 @Component
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";
52
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);
59         }
60
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);
69         }
70
71         if (isHttpCodeSuccess(responseCode)) {
72             logger.info("Successful Response from Naming Service");
73             List<Respelement> respList = responseBody.getElements();
74
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();
82                             break;
83                         }
84                     }
85                 }
86             }
87             return generatedName;
88         } else {
89             NameGenResponseError error = responseBody.getError();
90             String errorMessageString = NAMING_SERVICE_ERROR;
91             if (error != null) {
92                 errorMessageString = error.getMessage();
93             }
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);
98         }
99     }
100
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);
108         }
109
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);
118         }
119
120         if (isHttpCodeSuccess(responseCode)) {
121             logger.info("Successful Response from Naming Service");
122             return responseMessage;
123         } else {
124             String errorMessageString = NAMING_SERVICE_ERROR;
125
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);
130         }
131     }
132
133     private boolean isHttpCodeSuccess(int code) {
134         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
135     }
136
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();
141
142         String errorMessageString = null;
143         if (error != null) {
144             errorMessageString = error.getMessage();
145         }
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);
149         return errorMessage;
150     }
151
152 }