1 package org.onap.so.bpmn.infrastructure.adapter.cnf.tasks;
3 import java.util.ArrayList;
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;
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";
38 private ExceptionBuilder exceptionUtil;
40 private ObjectMapper mapper = new ObjectMapper();
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);
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
58 "http://so-bpmn-infra.onap:8081/mso/WorkflowMessage/" + CNF_ADAPTER_MESSAGE_TYPE + "/" + requestId;
59 HealthcheckInstanceRequest request = new HealthcheckInstanceRequest();
61 request = createStatusCheckRequest(listOfHeatStackIds, callBackUrl);
62 } catch (JsonProcessingException e) {
63 exceptionUtil.buildAndThrowWorkflowException(execution, 6822, e);
65 LOGGER.debug("request: {}", request);
67 String requestPayload = "";
69 requestPayload = mapper.writeValueAsString(request);
70 } catch (JsonProcessingException e) {
71 LOGGER.error("Error in JSON");
73 execution.setVariable("cnfRequestPayload", requestPayload);
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 "));
80 // Replace values with environment values
81 String uri = "http://so-cnf-adapter:8090";
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/";
90 LOGGER.debug("apiPath: {}", apiPath);
92 execution.setVariable("apiPath", apiPath);
95 public void processAsyncResponse(BuildingBlockExecution execution) {
96 // Value from CNF Async Handler activity
97 String asyncResponse = execution.getVariable("asyncCallbackResponse");
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 "));
104 LOGGER.debug("action: {}", action);
106 if (asyncResponse.contains("error")) {
107 exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
110 if (STATUS_CHECK_SCOPE.equals(action)) {
111 StatusCheckResponse statusCheckResponse = new StatusCheckResponse();
114 statusCheckResponse = mapper.readValue(asyncResponse, StatusCheckResponse.class);
115 } catch (JsonProcessingException e) {
116 LOGGER.error("Error in parsing JSON response");
119 LOGGER.debug("statusCheckResponse: {}", statusCheckResponse);
121 List<StatusCheckInstanceResponse> listOfStatusInstanceResponse = statusCheckResponse.getInstanceResponse();
123 for (StatusCheckInstanceResponse statusCheckInstanceResponse : listOfStatusInstanceResponse) {
124 if (!statusCheckInstanceResponse.isStatus()) {
125 exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
129 String statusCheckResponseJson = "";
131 statusCheckResponseJson = mapper.writeValueAsString(statusCheckResponse);
132 } catch (JsonProcessingException e) {
133 LOGGER.error("Error in PARSING statusCheckResponse");
136 execution.setVariable("StatusMessage", statusCheckResponseJson);
138 } else if (HEALTH_CHECK_SCOPE.equals(action)) {
139 HealthcheckResponse healthCheckResponse = new HealthcheckResponse();
141 healthCheckResponse = mapper.readValue(asyncResponse, HealthcheckResponse.class);
142 } catch (JsonProcessingException e) {
143 LOGGER.error("Error in parsing JSON");
146 List<HealthcheckInstanceResponse> listOfHealthcheckInstanceResponses =
147 healthCheckResponse.getInstanceResponse();
149 for (HealthcheckInstanceResponse healthcheckInstanceResponse : listOfHealthcheckInstanceResponses) {
150 if ("Failed".equalsIgnoreCase(healthcheckInstanceResponse.getStatus())
151 || "Unknown".equalsIgnoreCase(healthcheckInstanceResponse.getStatus())) {
152 exceptionUtil.buildAndThrowWorkflowException(execution, 500, asyncResponse, ONAPComponents.SO);
156 String healthCheckResponseJson = "";
158 healthCheckResponseJson = mapper.writeValueAsString(healthCheckResponse);
159 } catch (JsonProcessingException e) {
160 LOGGER.error("Error in PARSING statusCheckResponse");
163 execution.setVariable("StatusMessage", healthCheckResponseJson);
165 LOGGER.debug("healthCheckResponse: {}", healthCheckResponse);
170 protected HealthcheckInstanceRequest createStatusCheckRequest(List<String> listOfHeatStackIds, String callBackUrl)
171 throws JsonProcessingException {
172 HealthcheckInstanceRequest healthcheckInstanceRequest = new HealthcheckInstanceRequest();
173 List<HealthcheckInstance> listOfHealthcheckInstance = new ArrayList<>();
175 listOfHeatStackIds.stream().forEach(x -> listOfHealthcheckInstance.add(new HealthcheckInstance(x)));
176 LOGGER.debug("listOfHealthcheckInstance: {}", listOfHealthcheckInstance);
178 healthcheckInstanceRequest.setInstances(listOfHealthcheckInstance);
179 healthcheckInstanceRequest.setCallbackUrl(callBackUrl);
180 LOGGER.debug("healthcheckInstanceRequest: {}", healthcheckInstanceRequest);
182 return healthcheckInstanceRequest;