[SO] Code improvement in bpmn-infra supporting kafka change
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / PnfEBBLoader.java
1 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
2
3 import org.camunda.bpm.engine.delegate.DelegateExecution;
4 import org.javatuples.Pair;
5 import org.onap.aai.domain.yang.Relationship;
6 import org.onap.aai.domain.yang.RelationshipData;
7 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
8 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
9 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
10 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
11 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
12 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
13 import org.onap.so.client.exception.ExceptionBuilder;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.stereotype.Component;
17 import java.util.List;
18 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
19
20 @Component
21 public class PnfEBBLoader {
22
23     private static final Logger logger = LoggerFactory.getLogger(PnfEBBLoader.class);
24
25     private final BBInputSetupUtils bbInputSetupUtils;
26     private final BBInputSetup bbInputSetup;
27     private final WorkflowActionExtractResourcesAAI workflowActionUtils;
28     private final ExceptionBuilder exceptionBuilder;
29
30     PnfEBBLoader(BBInputSetupUtils bbInputSetupUtils, BBInputSetup bbInputSetup,
31             WorkflowActionExtractResourcesAAI workflowActionUtils, ExceptionBuilder exceptionBuilder) {
32         this.bbInputSetupUtils = bbInputSetupUtils;
33         this.bbInputSetup = bbInputSetup;
34         this.workflowActionUtils = workflowActionUtils;
35         this.exceptionBuilder = exceptionBuilder;
36     }
37
38
39     public void traverseAAIPnf(DelegateExecution execution, List<Resource> resourceList, String serviceId, String pnfId,
40             List<Pair<WorkflowType, String>> aaiResourceIds) {
41         try {
42             String pN = null;
43             org.onap.aai.domain.yang.ServiceInstance serviceInstanceAAI =
44                     bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
45             List<Relationship> relationship = serviceInstanceAAI.getRelationshipList().getRelationship();
46             List<RelationshipData> relationshipData;
47             org.onap.aai.domain.yang.Pnf pnf = null;
48             outerLoop: for (Relationship r : relationship) {
49                 if (r.getRelatedTo().equalsIgnoreCase("pnf")) {
50                     relationshipData = r.getRelationshipData();
51                     for (RelationshipData rd : relationshipData) {
52                         if (rd.getRelationshipKey().equalsIgnoreCase("pnf.pnf-name")) {
53                             pN = rd.getRelationshipValue();
54                             pnf = bbInputSetupUtils.getAAIPnf(pN);
55                             if (pnf.getPnfId().equalsIgnoreCase(pnfId)) {
56                                 break outerLoop;
57                             }
58                         }
59                     }
60                 }
61
62             }
63             ServiceInstance serviceInstanceMSO = bbInputSetup.getExistingServiceInstance(serviceInstanceAAI);
64             Resource serviceResource =
65                     new Resource(WorkflowType.SERVICE, serviceInstanceAAI.getServiceInstanceId(), false, null);
66             resourceList.add(serviceResource);
67             if (serviceInstanceMSO.getPnfs() != null) {
68                 findPnfWithGivenId(serviceInstanceMSO, pN, aaiResourceIds, resourceList, serviceResource);
69             }
70         } catch (Exception ex) {
71             logger.error("Exception in traverseAAIPnf", ex);
72             buildAndThrowException(execution,
73                     "Could not find existing Pnf or related Instances to execute the request on.");
74         }
75     }
76
77
78     private void findPnfWithGivenId(ServiceInstance serviceInstanceMSO, String pName,
79             List<Pair<WorkflowType, String>> aaiResourceIds, List<Resource> resourceList, Resource serviceResource) {
80         for (Pnf pnf : serviceInstanceMSO.getPnfs()) {
81             if (pnf.getPnfName().equals(pName)) {
82                 aaiResourceIds.add(new Pair<>(WorkflowType.PNF, pnf.getPnfId()));
83                 Resource pnfResource = new Resource(WorkflowType.PNF, pnf.getPnfId(), false, serviceResource);
84                 org.onap.aai.domain.yang.Pnf aaiPnf = bbInputSetupUtils.getAAIPnf(pnf.getPnfName());
85                 pnfResource.setInstanceName(pnf.getPnfName());
86                 pnfResource.setModelCustomizationId(aaiPnf.getModelCustomizationId());
87                 pnfResource.setModelVersionId(aaiPnf.getModelVersionId());
88                 resourceList.add(pnfResource);
89                 break;
90             }
91         }
92     }
93
94
95     private void buildAndThrowException(DelegateExecution execution, String msg) {
96         logger.error(msg);
97         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
98         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
99     }
100
101 }