e790fa4b720055b6aece1a553a4604ab72d04d6e
[so.git] /
1 package org.onap.so.client.namingservice;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.http.HttpStatus;
7 import org.onap.namingservice.model.NameGenDeleteResponse;
8 import org.onap.namingservice.model.NameGenResponse;
9 import org.onap.namingservice.model.NameGenResponseError;
10 import org.onap.namingservice.model.Respelement;
11 import org.onap.so.client.exception.BadResponseException;
12 import org.onap.so.logger.MessageEnum;
13 import org.onap.so.logger.MsoLogger;
14 import org.springframework.http.ResponseEntity;
15 import org.springframework.stereotype.Component;
16 import org.springframework.web.client.HttpStatusCodeException;
17
18 import com.fasterxml.jackson.databind.ObjectMapper;
19
20 @Component
21 public class NamingClientResponseValidator {
22         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, NamingClientResponseValidator.class);
23         private static final String INSTANCE_GROUP_NAME = "instance-group-name";
24         private static final String NO_RESPONSE_FROM_NAMING_SERVICE = "Error did not receive a response from Naming Service.";
25         private static final String NULL_RESPONSE_FROM_NAMING_SERVICE = "Error received a null response from Naming Service.";
26         private static final String NAMING_SERVICE_ERROR = "Error from Naming Service: %s";
27         
28         public String validateNameGenResponse(ResponseEntity<NameGenResponse> response) throws BadResponseException {
29                 if (response == null) {
30                         msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
31                                         MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_NAMING_SERVICE);
32                         throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
33                 }
34                        
35         int responseCode = response.getStatusCodeValue();
36         String generatedName = "";
37         NameGenResponse responseBody = response.getBody();
38         if (responseBody == null) {
39                 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
40                                         MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NULL_RESPONSE_FROM_NAMING_SERVICE);
41                         throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
42                 }             
43                 
44                 if (isHttpCodeSuccess(responseCode)) {
45                         msoLogger.info("Successful Response from Naming Service");                      
46                         List<Respelement> respList = responseBody.getElements();
47                         
48                         if (respList != null) {
49                                 for (int i=0; i < respList.size(); i++) {
50                                         Respelement respElement = respList.get(i);
51                                         if (respElement != null) {
52                                                 String resourceName = respElement.getResourceName();
53                                                 if (INSTANCE_GROUP_NAME.equals(resourceName)) {
54                                                         generatedName = respElement.getResourceValue();
55                                                         break;
56                                                 }
57                                         }
58                                 }
59                         }                       
60                         return generatedName;
61                 } else {
62                         NameGenResponseError error = responseBody.getError();
63                         String errorMessageString = NAMING_SERVICE_ERROR;
64                         if (error != null) {
65                                 errorMessageString = error.getMessage();
66                         }
67                         String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
68                         msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
69                                         MsoLogger.ErrorCode.DataError, errorMessage);
70                         throw new BadResponseException(errorMessage);
71                 }               
72         }
73         
74         public String validateNameGenDeleteResponse(ResponseEntity<NameGenDeleteResponse> response) throws BadResponseException {
75                 if (response == null) {
76                         msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NO_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
77                                         MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_NAMING_SERVICE);
78                         throw new BadResponseException(NO_RESPONSE_FROM_NAMING_SERVICE);
79                 }
80                        
81         int responseCode = response.getStatusCodeValue();
82         String responseMessage = "";
83         NameGenDeleteResponse responseBody = response.getBody();
84         if (responseBody == null) {
85                 msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, NULL_RESPONSE_FROM_NAMING_SERVICE, "BPMN",
86                                         MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NULL_RESPONSE_FROM_NAMING_SERVICE);
87                         throw new BadResponseException(NULL_RESPONSE_FROM_NAMING_SERVICE);
88                 }             
89                 
90                 if (isHttpCodeSuccess(responseCode)) {
91                         msoLogger.info("Successful Response from Naming Service");
92                         return responseMessage;
93                 } else {
94                         String errorMessageString = NAMING_SERVICE_ERROR;
95                         
96                         String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
97                         msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
98                                         MsoLogger.ErrorCode.DataError, errorMessage);
99                         throw new BadResponseException(errorMessage);
100                 }               
101         }
102         
103         private boolean isHttpCodeSuccess(int code) {
104         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
105     }
106         
107         protected String formatError(HttpStatusCodeException e) throws IOException {
108                 ObjectMapper mapper = new ObjectMapper();
109                 NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
110                 NameGenResponseError error = errorResponse.getError();
111                 
112                 String errorMessageString = null;
113                 if (error != null) {
114                         errorMessageString = error.getMessage();
115                 }
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                 return errorMessage;
120         }
121
122 }