Removed MsoLogger class
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / namingservice / NamingClientResponseValidator.java
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
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
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 @Component
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";
51         
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);
58                 }
59                        
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);
68                 }             
69                 
70                 if (isHttpCodeSuccess(responseCode)) {
71                         logger.info("Successful Response from Naming Service");
72                         List<Respelement> respList = responseBody.getElements();
73                         
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();
81                                                         break;
82                                                 }
83                                         }
84                                 }
85                         }                       
86                         return generatedName;
87                 } else {
88                         NameGenResponseError error = responseBody.getError();
89                         String errorMessageString = NAMING_SERVICE_ERROR;
90                         if (error != null) {
91                                 errorMessageString = error.getMessage();
92                         }
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);
97                 }               
98         }
99         
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);
106                 }
107                        
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);
116                 }             
117                 
118                 if (isHttpCodeSuccess(responseCode)) {
119                         logger.info("Successful Response from Naming Service");
120                         return responseMessage;
121                 } else {
122                         String errorMessageString = NAMING_SERVICE_ERROR;
123                         
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);
128                 }               
129         }
130         
131         private boolean isHttpCodeSuccess(int code) {
132         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
133     }
134         
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();
139                 
140                 String errorMessageString = null;
141                 if (error != null) {
142                         errorMessageString = error.getMessage();
143                 }
144                 String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
145                 logger.error("{} {} {} {} {}", MessageEnum.RA_GENERAL_EXCEPTION.toString(), errorMessage, "BPMN",
146                                 ErrorCode.DataError.getValue(), errorMessage);
147                 return errorMessage;
148         }
149
150 }