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