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