a55f32aaaadad341018c86c90ddaf080d609e7eb
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2019 Nokia.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.bpmn.infrastructure.pnf.delegate;
23
24 import java.util.Map;
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.recipe.ResourceInput;
29 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder;
30 import org.onap.so.bpmn.infrastructure.pnf.dmaap.DmaapClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import java.util.HashMap;
36 import java.util.Optional;
37
38 @Component
39 public class InformDmaapClient implements JavaDelegate {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(InformDmaapClient.class);
42     private DmaapClient dmaapClient;
43
44     @Override
45     public void execute(DelegateExecution execution) {
46         String pnfCorrelationId = (String) execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID);
47         RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
48         String processBusinessKey = execution.getProcessBusinessKey();
49         dmaapClient.registerForUpdate(pnfCorrelationId,
50                 () -> runtimeService.createMessageCorrelation("WorkflowMessage")
51                         .processInstanceBusinessKey(processBusinessKey).correlateWithResult(),
52                 createUpdateInfoMap(execution));
53     }
54
55     private Map<String, String> createUpdateInfoMap(DelegateExecution execution) {
56         Map<String, String> updateInfoMap = new HashMap<>();
57         updateInfoMap.put("pnfCorrelationId",
58                 (String) execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID));
59         getResourceInput(execution).ifPresent(resourceInput -> {
60             updateInfoMap.put("globalSubscriberID", resourceInput.getGlobalSubscriberId());
61             updateInfoMap.put("serviceType", resourceInput.getServiceType());
62             updateInfoMap.put("serviceInstanceId", resourceInput.getServiceInstanceId());
63         });
64         return updateInfoMap;
65     }
66
67     private Optional<ResourceInput> getResourceInput(DelegateExecution execution) {
68         ResourceInput resourceInput = null;
69         if (execution.getVariable("resourceInput") != null) {
70             resourceInput = ResourceRequestBuilder.getJsonObject((String) execution.getVariable("resourceInput"),
71                     ResourceInput.class);
72         } else {
73             LOGGER.warn("resourceInput value is null for correlation id: {}",
74                     execution.getVariable(ExecutionVariableNames.PNF_CORRELATION_ID));
75         }
76         return Optional.ofNullable(resourceInput);
77     }
78
79     @Autowired
80     public void setDmaapClient(DmaapClient dmaapClient) {
81         this.dmaapClient = dmaapClient;
82     }
83 }