Implementation of HealthCheckBB
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnf / tasks / CnfHealthCheckTasks.java
1 package org.onap.so.bpmn.infrastructure.adapter.cnf.tasks;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Optional;
6 import java.util.stream.Collectors;
7 import org.onap.logging.filter.base.ONAPComponents;
8 import org.onap.so.bpmn.common.BuildingBlockExecution;
9 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
10 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
11 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
12 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
13 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
14 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
15 import org.onap.so.client.adapter.cnf.entities.HealthcheckInstance;
16 import org.onap.so.client.adapter.cnf.entities.HealthcheckInstanceRequest;
17 import org.onap.so.client.adapter.cnf.entities.HealthcheckInstanceResponse;
18 import org.onap.so.client.adapter.cnf.entities.HealthcheckResponse;
19 import org.onap.so.client.adapter.cnf.entities.StatusCheckInstanceResponse;
20 import org.onap.so.client.adapter.cnf.entities.StatusCheckResponse;
21 import org.onap.so.client.exception.ExceptionBuilder;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Component;
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 @Component
30 public class CnfHealthCheckTasks {
31     private static final Logger LOGGER = LoggerFactory.getLogger(CnfHealthCheckTasks.class);
32     private static final String BUILDING_BLOCK = "buildingBlock";
33     private static final String HEALTH_CHECK_SCOPE = "health-check";
34     private static final String STATUS_CHECK_SCOPE = "status-check";
35     private static final String CNF_ADAPTER_MESSAGE_TYPE = "CNFCallback";
36
37     @Autowired
38     private ExceptionBuilder exceptionUtil;
39
40     private ObjectMapper mapper = new ObjectMapper();
41
42     public void prepareCnfAdaperRequest(BuildingBlockExecution execution) {
43         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
44         ServiceInstance serviceInstance = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0);
45         GenericVnf genericVnf = serviceInstance.getVnfs().get(0);
46         List<VfModule> listOfVfModules = genericVnf.getVfModules();
47         List<String> listOfHeatStackIds =
48                 listOfVfModules.stream().map(x -> x.getHeatStackId()).collect(Collectors.toList());
49         LOGGER.debug("listOfHeatStackIds from prepareCnfAdaperRequest: {}", listOfHeatStackIds);
50
51         // Prepare values to pass in execution variable for CNF Adapter async Handling
52         String requestId = execution.getVariable("mso-request-id");
53         execution.setVariable("messageType", CNF_ADAPTER_MESSAGE_TYPE);
54         execution.setVariable("correlator", requestId);
55         execution.setVariable("timeout", "PT30M");
56         // Replace with environment values
57         String callBackUrl =
58                 "http://so-bpmn-infra.onap:8081/mso/WorkflowMessage/" + CNF_ADAPTER_MESSAGE_TYPE + "/" + requestId;
59         HealthcheckInstanceRequest request = new HealthcheckInstanceRequest();
60         try {
61             request = createStatusCheckRequest(listOfHeatStackIds, callBackUrl);
62         } catch (JsonProcessingException e) {
63             exceptionUtil.buildAndThrowWorkflowException(execution, 6822, e);
64         }
65         LOGGER.debug("request: {}", request);
66
67         String requestPayload = "";
68         try {
69             requestPayload = mapper.writeValueAsString(request);
70         } catch (JsonProcessingException e) {
71             LOGGER.error("Error in JSON");
72         }
73         execution.setVariable("cnfRequestPayload", requestPayload);
74
75         ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
76         BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
77         String action = Optional.ofNullable(buildingBlock.getBpmnAction()).orElseThrow(
78                 () -> new NullPointerException("BPMN Action is NULL in the orchestration_flow_reference table "));
79
80         // Replace values with environment values
81         String uri = "http://so-cnf-adapter:8090";
82         String apiPath = "";
83
84         if (STATUS_CHECK_SCOPE.equals(action)) {
85             apiPath = uri + "/api/cnf-adapter/v1/statuscheck/";
86         } else if (HEALTH_CHECK_SCOPE.equals(action)) {
87             apiPath = uri + "/api/cnf-adapter/v1/healthcheck/";
88         }
89
90         LOGGER.debug("apiPath: {}", apiPath);
91
92         execution.setVariable("apiPath", apiPath);
93     }
94
95     public void processAsyncResponse(BuildingBlockExecution execution) {
96         // Value from CNF Async Handler activity
97         String asyncResponse = execution.getVariable("asyncCallbackResponse");
98
99         ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
100         BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
101         String action = Optional.ofNullable(buildingBlock.getBpmnAction()).orElseThrow(
102                 () -> new NullPointerException("BPMN Action is NULL in the orchestration_flow_reference table "));
103
104         LOGGER.debug("action: {}", action);
105
106         if (asyncResponse.contains("error")) {
107             exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
108         }
109
110         if (STATUS_CHECK_SCOPE.equals(action)) {
111             StatusCheckResponse statusCheckResponse = new StatusCheckResponse();
112
113             try {
114                 statusCheckResponse = mapper.readValue(asyncResponse, StatusCheckResponse.class);
115             } catch (JsonProcessingException e) {
116                 LOGGER.error("Error in parsing JSON response");
117             }
118
119             LOGGER.debug("statusCheckResponse: {}", statusCheckResponse);
120
121             List<StatusCheckInstanceResponse> listOfStatusInstanceResponse = statusCheckResponse.getInstanceResponse();
122
123             for (StatusCheckInstanceResponse statusCheckInstanceResponse : listOfStatusInstanceResponse) {
124                 if (!statusCheckInstanceResponse.isStatus()) {
125                     exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
126                 }
127             }
128
129             String statusCheckResponseJson = "";
130             try {
131                 statusCheckResponseJson = mapper.writeValueAsString(statusCheckResponse);
132             } catch (JsonProcessingException e) {
133                 LOGGER.error("Error in PARSING statusCheckResponse");
134             }
135
136             execution.setVariable("StatusMessage", statusCheckResponseJson);
137
138         } else if (HEALTH_CHECK_SCOPE.equals(action)) {
139             HealthcheckResponse healthCheckResponse = new HealthcheckResponse();
140             try {
141                 healthCheckResponse = mapper.readValue(asyncResponse, HealthcheckResponse.class);
142             } catch (JsonProcessingException e) {
143                 LOGGER.error("Error in parsing JSON");
144             }
145
146             List<HealthcheckInstanceResponse> listOfHealthcheckInstanceResponses =
147                     healthCheckResponse.getInstanceResponse();
148
149             for (HealthcheckInstanceResponse healthcheckInstanceResponse : listOfHealthcheckInstanceResponses) {
150                 if ("Failed".equalsIgnoreCase(healthcheckInstanceResponse.getStatus())
151                         || "Unknown".equalsIgnoreCase(healthcheckInstanceResponse.getStatus())) {
152                     exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
153                 }
154             }
155
156             String healthCheckResponseJson = "";
157             try {
158                 healthCheckResponseJson = mapper.writeValueAsString(healthCheckResponse);
159             } catch (JsonProcessingException e) {
160                 LOGGER.error("Error in PARSING statusCheckResponse");
161             }
162
163             execution.setVariable("StatusMessage", healthCheckResponseJson);
164
165             LOGGER.debug("healthCheckResponse: {}", healthCheckResponse);
166         }
167
168     }
169
170     protected HealthcheckInstanceRequest createStatusCheckRequest(List<String> listOfHeatStackIds, String callBackUrl)
171             throws JsonProcessingException {
172         HealthcheckInstanceRequest healthcheckInstanceRequest = new HealthcheckInstanceRequest();
173         List<HealthcheckInstance> listOfHealthcheckInstance = new ArrayList<>();
174
175         listOfHeatStackIds.stream().forEach(x -> listOfHealthcheckInstance.add(new HealthcheckInstance(x)));
176         LOGGER.debug("listOfHealthcheckInstance: {}", listOfHealthcheckInstance);
177
178         healthcheckInstanceRequest.setInstances(listOfHealthcheckInstance);
179         healthcheckInstanceRequest.setCallbackUrl(callBackUrl);
180         LOGGER.debug("healthcheckInstanceRequest: {}", healthcheckInstanceRequest);
181
182         return healthcheckInstanceRequest;
183     }
184
185 }