Remove restricted notice from TOSCA file
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ServiceInProgressStatusCommand.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.vid.job.Job;
25 import org.onap.vid.job.Job.JobStatus;
26 import org.onap.vid.job.JobCommand;
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.ServiceInstantiation;
32 import org.onap.vid.properties.Features;
33 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Component;
36
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.Collectors;
40 import java.util.stream.Stream;
41
42 @Component
43 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
44 public class ServiceInProgressStatusCommand extends BaseInProgressStatusCommand {
45
46     public ServiceInProgressStatusCommand() {
47     }
48
49     ServiceInProgressStatusCommand(JobSharedData sharedData, MsoResourceIds msoResourceIds) {
50         init(sharedData, msoResourceIds.getRequestId(), msoResourceIds.getInstanceId());
51     }
52
53     @Override
54     protected ExpiryChecker getExpiryChecker() {
55         return new ServiceExpiryChecker();
56     }
57
58     protected NextCommand processJobStatus(Job.JobStatus jobStatus) {
59         JobCommand jobCommand = this;
60         Job.JobStatus nextJobStatus = jobStatus;
61         switch (jobStatus) {
62             case FAILED:
63                 asyncInstantiationBL.handleFailedInstantiation(getSharedData().getJobUuid());
64                 return new NextCommand(nextJobStatus, jobCommand);
65             case PAUSE:
66                 nextJobStatus = Job.JobStatus.IN_PROGRESS;
67                 break;
68             case COMPLETED:
69                 ServiceInstantiation request = (ServiceInstantiation) getSharedData().getRequest();
70                 if (isNeedToCreateChildJobs(request)) {
71                     List<String> childrenJobs = getChildJobs(request);
72                     nextJobStatus = Job.JobStatus.IN_PROGRESS;
73                     jobCommand = new WatchingCommand(getSharedData(), childrenJobs, true);
74                     return new NextCommand(nextJobStatus, jobCommand);
75                 }
76                 break;
77                 default: // for sonar
78         }
79         asyncInstantiationBL.updateServiceInfoAndAuditStatus(getSharedData().getJobUuid(), jobStatus);
80         return new NextCommand(nextJobStatus, jobCommand);
81     }
82
83     private List<String> getChildJobs(ServiceInstantiation request) {
84         Map<String, Object> dataForChild = buildDataForChild(request);
85
86         Stream<String> vnfJobs = request.getVnfs().values().stream().map(
87                 vnf -> jobsBrokerService.add(
88                         jobAdapter.createChildJob(JobType.VnfInstantiation, JobStatus.CREATING , vnf, getSharedData(), dataForChild)).toString()
89         );
90
91         Stream<String> networkJobs = request.getNetworks().values().stream().map(
92                 network -> jobsBrokerService.add(
93                         jobAdapter.createChildJob(JobType.NetworkInstantiation, JobStatus.CREATING , network, getSharedData(), dataForChild)).toString()
94         );
95
96         Stream<String> instanceGroupJobs = request.getVnfGroups().values().stream().map(
97                 instanceGroup -> jobsBrokerService.add(
98                         jobAdapter.createChildJob(JobType.InstanceGroupInstantiation, JobStatus.CREATING , instanceGroup, getSharedData(), dataForChild)).toString()
99         );
100
101         return Stream.of(vnfJobs, networkJobs, instanceGroupJobs)
102                 .reduce(Stream::concat)
103                 .orElseGet(Stream::empty)
104                 .collect(Collectors.toList());
105     }
106
107     public boolean isNeedToCreateChildJobs(ServiceInstantiation request) {
108         return featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF) && request.isALaCarte() &&
109                     (
110                             MapUtils.isNotEmpty(request.getVnfs()) || MapUtils.isNotEmpty(request.getNetworks()) ||
111                             (featureManager.isActive(Features.FLAG_1902_VNF_GROUPING) && MapUtils.isNotEmpty(request.getVnfGroups()))
112                     );
113     }
114
115     protected Map<String, Object> buildDataForChild(ServiceInstantiation request) {
116         commandParentData.addInstanceId(CommandDataKey.SERVICE_INSTANCE_ID, this.instanceId);
117         commandParentData.addModelInfo(CommandDataKey.SERVICE_MODEL_INFO, request.getModelInfo());
118         return commandParentData.getParentData();
119     }
120 }