2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command;
23 import com.google.common.collect.ImmutableMap;
24 import org.apache.commons.lang3.ObjectUtils;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.aai.ExceptionWithRequestInfo;
27 import org.onap.vid.changeManagement.RequestDetailsWrapper;
28 import org.onap.vid.exceptions.MaxRetriesException;
29 import org.onap.vid.job.Job;
30 import org.onap.vid.job.JobCommand;
31 import org.onap.vid.job.NextCommand;
32 import org.onap.vid.job.impl.JobSharedData;
33 import org.onap.vid.model.RequestReferencesContainer;
34 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
35 import org.onap.vid.mso.RestMsoImplementation;
36 import org.onap.vid.mso.RestObject;
37 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
38 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
40 import javax.inject.Inject;
44 public abstract class ServiceInstantiationCommand extends BaseRootCommand implements JobCommand {
46 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
49 protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
52 private RestMsoImplementation restMso;
54 protected String optimisticUniqueServiceInstanceName;
56 public ServiceInstantiationCommand() {
60 public NextCommand call() {
61 RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
63 requestDetailsWrapper = generateServiceInstantiationRequest();
66 //Aai return bad response while checking names uniqueness
67 catch (ExceptionWithRequestInfo exception) {
68 return handleAaiNameUniquenessBadResponse(exception);
71 //Vid reached to max retries while trying to find unique name in AAI
72 catch (MaxRetriesException exception) {
73 return handleMaxRetryInNameUniqueness(exception);
76 String path = asyncInstantiationBL.getServiceInstantiationPath(getRequest());
78 RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
79 path, RequestReferencesContainer.class);
81 return handleRootResponse(msoResponse);
86 protected ServiceInstantiation getRequest() {
87 return (ServiceInstantiation) getSharedData().getRequest();
90 protected abstract RequestDetailsWrapper<ServiceInstantiationRequestDetails> generateServiceInstantiationRequest();
92 private NextCommand handleMaxRetryInNameUniqueness(MaxRetriesException exception) {
93 LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
94 return handleCommandFailed();
97 private NextCommand handleAaiNameUniquenessBadResponse(ExceptionWithRequestInfo exception) {
98 LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
99 //put the job in_progress so we will keep trying to check name uniqueness in AAI
100 //And then send the request to MSO
101 return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
105 public ServiceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
109 (String) commandData.get("optimisticUniqueServiceInstanceName")
113 protected ServiceInstantiationCommand init(JobSharedData sharedData, String optimisticUniqueServiceInstanceName) {
115 this.optimisticUniqueServiceInstanceName = ObjectUtils.defaultIfNull(optimisticUniqueServiceInstanceName,
116 (getRequest()).getInstanceName());
121 public Map<String, Object> getData() {
122 return ImmutableMap.of(
123 "optimisticUniqueServiceInstanceName", optimisticUniqueServiceInstanceName