[SO] Code improvement in bpmn-infra supporting kafka change
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / pnf / delegate / RegisterForPnfReadyEvent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Nokia.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.pnf.delegate;
22
23 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION;
24 import com.google.common.base.Strings;
25 import org.camunda.bpm.engine.RuntimeService;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.camunda.bpm.engine.delegate.JavaDelegate;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.infrastructure.pnf.kafka.KafkaClient;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
33 import org.onap.so.client.exception.BBObjectNotFoundException;
34 import org.onap.so.client.exception.ExceptionBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.stereotype.Component;
40
41 /**
42  * This class is designed to be used within WaitForPnfReadyBB
43  */
44 @Component
45 public class RegisterForPnfReadyEvent implements JavaDelegate {
46
47     private static final String ERROR_MESSAGE_PNF_NOT_FOUND =
48             "pnf resource not found in buildingBlockExecution while registering to kafka listener";
49     private static final Logger LOGGER = LoggerFactory.getLogger(RegisterForPnfReadyEvent.class);
50
51     private KafkaClient kafkaClient;
52     private ExtractPojosForBB extractPojosForBB;
53     private ExceptionBuilder exceptionBuilder;
54     private String pnfEntryNotificationTimeout;
55
56     @Autowired
57     public RegisterForPnfReadyEvent(KafkaClient kafkaClient, ExtractPojosForBB extractPojosForBB,
58             ExceptionBuilder exceptionBuilder,
59             @Value("${aai.pnfEntryNotificationTimeout}") String pnfEntryNotificationTimeout) {
60         this.kafkaClient = kafkaClient;
61         this.extractPojosForBB = extractPojosForBB;
62         this.exceptionBuilder = exceptionBuilder;
63         this.pnfEntryNotificationTimeout = pnfEntryNotificationTimeout;
64     }
65
66     @Override
67     public void execute(DelegateExecution execution) {
68         try {
69             String pnfName = getPnfName(execution);
70             fillExecution(execution, pnfName);
71             RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
72             kafkaClient.registerForUpdate(pnfName, () -> runtimeService.createMessageCorrelation("WorkflowMessage")
73                     .processInstanceId(execution.getProcessInstanceId()).correlateWithResult());
74         } catch (BBObjectNotFoundException e) {
75             LOGGER.error(ERROR_MESSAGE_PNF_NOT_FOUND);
76             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ERROR_MESSAGE_PNF_NOT_FOUND);
77         }
78     }
79
80     private void fillExecution(DelegateExecution execution, String pnfName) {
81         execution.setVariable(ExecutionVariableNames.PNF_CORRELATION_ID, pnfName);
82         if (Strings.isNullOrEmpty(pnfEntryNotificationTimeout)) {
83             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
84                     "pnfEntryNotificationTimeout value not defined");
85         }
86         execution.setVariable(TIMEOUT_FOR_NOTIFICATION, pnfEntryNotificationTimeout);
87     }
88
89     private String getPnfName(DelegateExecution execution) throws BBObjectNotFoundException {
90         BuildingBlockExecution buildingBlockExecution =
91                 (BuildingBlockExecution) execution.getVariable("gBuildingBlockExecution");
92         Pnf pnf = extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.PNF);
93         String pnfName = pnf.getPnfName();
94         if (Strings.isNullOrEmpty(pnfName)) {
95             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "pnf name is not set");
96         }
97         return pnfName;
98     }
99 }