Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / WatchingCommandBaseModule.java
1 package org.onap.vid.job.command;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
5 import org.onap.vid.asdc.AsdcCatalogException;
6 import org.onap.vid.job.*;
7 import org.onap.vid.job.command.CommandParentData.CommandDataKey;
8 import org.onap.vid.job.impl.JobSharedData;
9 import org.onap.vid.model.serviceInstantiation.VfModule;
10 import org.onap.vid.model.serviceInstantiation.Vnf;
11 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
12 import org.springframework.context.annotation.Scope;
13 import org.springframework.stereotype.Component;
14
15 import javax.inject.Inject;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.stream.Collectors;
20
21 @Component
22 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
23 public class WatchingCommandBaseModule extends BaseWatchingCommand {
24     @Inject
25     protected JobsBrokerService jobsBrokerService;
26
27     @Inject
28     protected JobAdapter jobAdapter;
29     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(WatchingCommandBaseModule.class);
30
31     public WatchingCommandBaseModule(
32             JobSharedData sharedData,
33             List<String> childrenJobsIds,
34             boolean isService,
35             CommandParentData commandParentData) {
36        super(sharedData, childrenJobsIds, isService);
37         this.commandParentData = commandParentData;
38     }
39
40     public WatchingCommandBaseModule() {
41
42     }
43
44     @Override
45     protected NextCommand getNextCommand(Job.JobStatus cumulativeJobsStatus) {
46
47         if (cumulativeJobsStatus== Job.JobStatus.IN_PROGRESS) {
48             return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, this);
49         }
50
51         if(cumulativeJobsStatus==Job.JobStatus.FAILED || cumulativeJobsStatus==Job.JobStatus.COMPLETED_WITH_ERRORS){
52             return new NextCommand(Job.JobStatus.COMPLETED_WITH_ERRORS);
53         }
54         Vnf request = (Vnf) getSharedData().getRequest();
55         Map<String, Object> dataForChild = buildDataForChild();
56         //Create non-base Volume groups job
57         List<VfModule> vfModules = request.getVfModules().values().stream().flatMap(vfKey -> vfKey.values().stream()).collect(Collectors.toList());
58         List<String> vgNonBaseJobs = new ArrayList<>();
59         for( VfModule vfModule : vfModules){
60             try {
61                 if(!commandUtils.isVfModuleBaseModule(commandParentData.getModelInfo(CommandDataKey.SERVICE_MODEL_INFO).getModelVersionId(), vfModule.getModelInfo().getModelVersionId())) {
62                     vgNonBaseJobs.add(jobsBrokerService.add(
63                             jobAdapter.createChildJob(JobType.VolumeGroupInstantiation, Job.JobStatus.CREATING, vfModule, getSharedData(), dataForChild)).toString());
64                 }
65             } catch (AsdcCatalogException e) {
66                 LOG.error("Failed to retrieve service definitions from SDC, for VfModule is BaseModule. Error: "+e.getMessage() , e);
67                 return new NextCommand(Job.JobStatus.COMPLETED_WITH_ERRORS);
68             }
69         }
70         return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, new WatchingCommand(getSharedData(), vgNonBaseJobs, false));
71     }
72
73     @Override
74     public WatchingCommandBaseModule init(JobSharedData sharedData, Map<String, Object> commandData) {
75         super.init(sharedData, commandData);
76         commandParentData.initParentData(commandData);
77         return this;
78     }
79
80     protected Map<String, Object> buildDataForChild() {
81         return commandParentData.getParentData();
82     }
83
84     @Override
85     public Map<String, Object> getData() {
86         return ImmutableMap.<String, Object>builder()
87                 .putAll(super.getData())
88                 .putAll(commandParentData.getParentData())
89                 .build();
90     }
91
92
93 }