1 package org.onap.vid.job.command;
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;
20 import javax.inject.Inject;
24 public abstract class ServiceInstantiationCommand extends BaseRootCommand implements JobCommand {
26 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
29 protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
32 private RestMsoImplementation restMso;
34 protected String optimisticUniqueServiceInstanceName;
36 public ServiceInstantiationCommand() {
40 public NextCommand call() {
41 RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
43 requestDetailsWrapper = generateServiceInstantiationRequest();
46 //Aai return bad response while checking names uniqueness
47 catch (ExceptionWithRequestInfo exception) {
48 return handleAaiNameUniquenessBadResponse(exception);
51 //Vid reached to max retries while trying to find unique name in AAI
52 catch (MaxRetriesException exception) {
53 return handleMaxRetryInNameUniqueness(exception);
56 String path = asyncInstantiationBL.getServiceInstantiationPath(getRequest());
58 RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
59 path, RequestReferencesContainer.class);
61 return handleRootResponse(msoResponse);
66 protected ServiceInstantiation getRequest() {
67 return (ServiceInstantiation) getSharedData().getRequest();
70 protected abstract RequestDetailsWrapper<ServiceInstantiationRequestDetails> generateServiceInstantiationRequest();
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();
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);
85 public ServiceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
89 (String) commandData.get("optimisticUniqueServiceInstanceName")
93 protected ServiceInstantiationCommand init(JobSharedData sharedData, String optimisticUniqueServiceInstanceName) {
95 this.optimisticUniqueServiceInstanceName = ObjectUtils.defaultIfNull(optimisticUniqueServiceInstanceName,
96 (getRequest()).getInstanceName());
101 public Map<String, Object> getData() {
102 return ImmutableMap.of(
103 "optimisticUniqueServiceInstanceName", optimisticUniqueServiceInstanceName