Remove restricted notice from TOSCA file
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / VnfInProgressStatusCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
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.vid.job.command;
22
23 import org.apache.commons.collections.MapUtils;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.vid.asdc.AsdcCatalogException;
26 import org.onap.vid.job.Job;
27 import org.onap.vid.job.JobType;
28 import org.onap.vid.job.NextCommand;
29 import org.onap.vid.job.command.CommandParentData.CommandDataKey;
30 import org.onap.vid.job.impl.JobSharedData;
31 import org.onap.vid.model.serviceInstantiation.BaseResource;
32 import org.onap.vid.model.serviceInstantiation.VfModule;
33 import org.onap.vid.model.serviceInstantiation.Vnf;
34 import org.onap.vid.properties.Features;
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Component;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.stream.Collectors;
43
44 @Component
45 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46 public class VnfInProgressStatusCommand extends ResourceWithChildrenInProgressCommand {
47     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VnfInProgressStatusCommand.class);
48
49     public VnfInProgressStatusCommand(JobSharedData sharedData,
50                                            String requestId,
51                                            String instanceId,
52                                            CommandParentData commandParentData) {
53         super(sharedData, requestId, instanceId, commandParentData);
54      }
55
56     public VnfInProgressStatusCommand() {
57     }
58
59     @Override
60     protected NextCommand processJobStatus(Job.JobStatus jobStatus) {
61         if (jobStatus == Job.JobStatus.FAILED) {
62             return new NextCommand(jobStatus);
63         }
64
65         Vnf request = (Vnf) getSharedData().getRequest();
66
67         if (isNeedToCreateChildJobs(jobStatus, request)) {
68             commandParentData.addInstanceId(CommandDataKey.VNF_INSTANCE_ID, instanceId);
69             commandParentData.addModelInfo(CommandDataKey.VNF_MODEL_INFO, request.getModelInfo());
70             //create volume group of base module job
71             Map<String, Object> dataForChild = buildDataForChild();
72             List<VfModule> vfModules = request.getVfModules().values().stream().flatMap(vfKey -> vfKey.values().stream()).collect(Collectors.toList());
73             List<String> vgBaseJobs = new ArrayList<>();
74             for( VfModule vfModule : vfModules){
75                 try {
76                     if(commandUtils.isVfModuleBaseModule(commandParentData.getModelInfo(CommandDataKey.SERVICE_MODEL_INFO).getModelVersionId(), vfModule.getModelInfo().getModelVersionId())) {
77                         vgBaseJobs.add(jobsBrokerService.add(
78                                 jobAdapter.createChildJob(JobType.VolumeGroupInstantiation, Job.JobStatus.CREATING, vfModule, getSharedData(), dataForChild)).toString());
79                     }
80                 } catch (AsdcCatalogException e) {
81                     LOG.error("Failed to retrieve service definitions from SDC, for VfModule is BaseModule. Error: "+e.getMessage() , e);
82                     return new NextCommand(Job.JobStatus.COMPLETED_WITH_ERRORS);
83                 }
84             }
85             return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, new WatchingCommandBaseModule(getSharedData(), vgBaseJobs, false, commandParentData));
86         }
87
88         //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
89         if (jobStatus == Job.JobStatus.PAUSE) {
90             return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, this);
91         }
92         return new NextCommand(jobStatus, this);
93     }
94
95
96     protected boolean isNeedToCreateChildJobs(Job.JobStatus jobStatus, BaseResource request) {
97         return  featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VFMODULE) &&
98                 jobStatus == Job.JobStatus.COMPLETED &&
99                 MapUtils.isNotEmpty(((Vnf)request).getVfModules());
100     }
101
102
103     @Override
104     protected ExpiryChecker getExpiryChecker() {
105         return x->false;
106     }
107 }