Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / VnfInProgressStatusCommand.java
1 package org.onap.vid.job.command;
2
3 import org.apache.commons.collections.MapUtils;
4 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
5 import org.onap.vid.asdc.AsdcCatalogException;
6 import org.onap.vid.job.Job;
7 import org.onap.vid.job.JobType;
8 import org.onap.vid.job.NextCommand;
9 import org.onap.vid.job.command.CommandParentData.CommandDataKey;
10 import org.onap.vid.job.impl.JobSharedData;
11 import org.onap.vid.model.serviceInstantiation.BaseResource;
12 import org.onap.vid.model.serviceInstantiation.VfModule;
13 import org.onap.vid.model.serviceInstantiation.Vnf;
14 import org.onap.vid.properties.Features;
15 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
16 import org.springframework.context.annotation.Scope;
17 import org.springframework.stereotype.Component;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.stream.Collectors;
23
24 @Component
25 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
26 public class VnfInProgressStatusCommand extends ResourceWithChildrenInProgressCommand {
27     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VnfInProgressStatusCommand.class);
28
29     public VnfInProgressStatusCommand(JobSharedData sharedData,
30                                            String requestId,
31                                            String instanceId,
32                                            CommandParentData commandParentData) {
33         super(sharedData, requestId, instanceId, commandParentData);
34      }
35
36     public VnfInProgressStatusCommand() {
37     }
38
39     @Override
40     protected NextCommand processJobStatus(Job.JobStatus jobStatus) {
41         if (jobStatus == Job.JobStatus.FAILED) {
42             return new NextCommand(jobStatus);
43         }
44
45         Vnf request = (Vnf) getSharedData().getRequest();
46
47         if (isNeedToCreateChildJobs(jobStatus, request)) {
48             commandParentData.addInstanceId(CommandDataKey.VNF_INSTANCE_ID, instanceId);
49             commandParentData.addModelInfo(CommandDataKey.VNF_MODEL_INFO, request.getModelInfo());
50             //create volume group of base module job
51             Map<String, Object> dataForChild = buildDataForChild();
52             List<VfModule> vfModules = request.getVfModules().values().stream().flatMap(vfKey -> vfKey.values().stream()).collect(Collectors.toList());
53             List<String> vgBaseJobs = new ArrayList<>();
54             for( VfModule vfModule : vfModules){
55                 try {
56                     if(commandUtils.isVfModuleBaseModule(commandParentData.getModelInfo(CommandDataKey.SERVICE_MODEL_INFO).getModelVersionId(), vfModule.getModelInfo().getModelVersionId())) {
57                         vgBaseJobs.add(jobsBrokerService.add(
58                                 jobAdapter.createChildJob(JobType.VolumeGroupInstantiation, Job.JobStatus.CREATING, vfModule, getSharedData(), dataForChild)).toString());
59                     }
60                 } catch (AsdcCatalogException e) {
61                     LOG.error("Failed to retrieve service definitions from SDC, for VfModule is BaseModule. Error: "+e.getMessage() , e);
62                     return new NextCommand(Job.JobStatus.COMPLETED_WITH_ERRORS);
63                 }
64             }
65             return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, new WatchingCommandBaseModule(getSharedData(), vgBaseJobs, false, commandParentData));
66         }
67
68         //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
69         if (jobStatus == Job.JobStatus.PAUSE) {
70             return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, this);
71         }
72         return new NextCommand(jobStatus, this);
73     }
74
75
76     protected boolean isNeedToCreateChildJobs(Job.JobStatus jobStatus, BaseResource request) {
77         return  featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VFMODULE) &&
78                 jobStatus == Job.JobStatus.COMPLETED &&
79                 MapUtils.isNotEmpty(((Vnf)request).getVfModules());
80     }
81
82
83     @Override
84     protected ExpiryChecker getExpiryChecker() {
85         return x->false;
86     }
87 }