Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ServiceInstantiationCommand.java
1 package org.onap.vid.job.command;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.apache.commons.lang3.ObjectUtils;
5 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.onap.vid.aai.ExceptionWithRequestInfo;
7 import org.onap.vid.changeManagement.RequestDetailsWrapper;
8 import org.onap.vid.exceptions.MaxRetriesException;
9 import org.onap.vid.job.Job;
10 import org.onap.vid.job.JobCommand;
11 import org.onap.vid.job.NextCommand;
12 import org.onap.vid.job.impl.JobSharedData;
13 import org.onap.vid.model.RequestReferencesContainer;
14 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
15 import org.onap.vid.mso.RestMsoImplementation;
16 import org.onap.vid.mso.RestObject;
17 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
18 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
19
20 import javax.inject.Inject;
21 import java.util.Map;
22
23
24 public abstract class ServiceInstantiationCommand extends BaseRootCommand implements JobCommand {
25
26     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
27
28     @Inject
29     protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
30
31     @Inject
32     private RestMsoImplementation restMso;
33
34     protected String optimisticUniqueServiceInstanceName;
35
36     public ServiceInstantiationCommand() {
37     }
38
39     @Override
40     public NextCommand call() {
41         RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
42         try {
43             requestDetailsWrapper = generateServiceInstantiationRequest();
44         }
45
46         //Aai return bad response while checking names uniqueness
47         catch (ExceptionWithRequestInfo exception) {
48             return handleAaiNameUniquenessBadResponse(exception);
49         }
50
51         //Vid reached to max retries while trying to find unique name in AAI
52         catch (MaxRetriesException exception) {
53             return handleMaxRetryInNameUniqueness(exception);
54         }
55
56         String path = asyncInstantiationBL.getServiceInstantiationPath(getRequest());
57
58         RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
59                 path, RequestReferencesContainer.class);
60
61         return handleRootResponse(msoResponse);
62
63     }
64
65     @Override
66     protected ServiceInstantiation getRequest() {
67         return (ServiceInstantiation) getSharedData().getRequest();
68     }
69
70     protected abstract RequestDetailsWrapper<ServiceInstantiationRequestDetails> generateServiceInstantiationRequest();
71
72     private NextCommand handleMaxRetryInNameUniqueness(MaxRetriesException exception) {
73         LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
74         return handleCommandFailed();
75     }
76
77     private NextCommand handleAaiNameUniquenessBadResponse(ExceptionWithRequestInfo exception) {
78         LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
79         //put the job in_progress so we will keep trying to check name uniqueness in AAI
80         //And then send the request to MSO
81         return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
82     }
83
84     @Override
85     public ServiceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
86
87         return init(
88                 sharedData,
89                 (String) commandData.get("optimisticUniqueServiceInstanceName")
90         );
91     }
92
93     protected ServiceInstantiationCommand init(JobSharedData sharedData, String optimisticUniqueServiceInstanceName) {
94         init(sharedData);
95         this.optimisticUniqueServiceInstanceName = ObjectUtils.defaultIfNull(optimisticUniqueServiceInstanceName,
96                 (getRequest()).getInstanceName());
97         return this;
98     }
99
100     @Override
101     public Map<String, Object> getData() {
102         return ImmutableMap.of(
103                 "optimisticUniqueServiceInstanceName", optimisticUniqueServiceInstanceName
104         );
105     }
106 }