2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.vid.job.command;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.collect.ImmutableMap;
25 import io.joshworks.restclient.http.HttpResponse;
26 import org.onap.vid.aai.exceptions.InvalidAAIResponseException;
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.model.RequestReferencesContainer;
33 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
34 import org.onap.vid.mso.MsoInterface;
35 import org.onap.vid.mso.model.ServiceInstantiationRequestDetails;
36 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
37 import org.onap.vid.services.AuditService;
38 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Component;
43 import javax.inject.Inject;
45 import java.util.Objects;
46 import java.util.UUID;
50 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
51 public class ServiceInstantiationCommand implements JobCommand {
53 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
55 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
58 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
61 private AuditService auditService;
64 private MsoInterface restMso;
67 private ServiceInstantiation serviceInstantiationRequest;
68 private String userId;
70 public ServiceInstantiationCommand() {
73 public ServiceInstantiationCommand(UUID uuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
74 init(uuid, serviceInstantiationRequest, userId);
77 ServiceInstantiationCommand(AsyncInstantiationBusinessLogic asyncInstantiationBL, AuditService auditService, MsoInterface msoInterface,
78 UUID uuid, ServiceInstantiation serviceInstantiation, String userId) {
79 this(uuid, serviceInstantiation, userId);
80 this.asyncInstantiationBL = asyncInstantiationBL;
81 this.auditService = auditService;
82 this.restMso = msoInterface;
86 public NextCommand call() {
87 RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
89 requestDetailsWrapper = asyncInstantiationBL.generateServiceInstantiationRequest(
90 uuid, serviceInstantiationRequest, userId
93 //Aai return bad response while checking names uniqueness
94 catch (InvalidAAIResponseException exception) {
95 LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
96 //put the job in_progress so we will keep trying to check name uniqueness in AAI
97 //And then send the request to MSO
98 return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
101 //Vid reached to max retries while trying to find unique name in AAI
102 catch (MaxRetriesException exception) {
103 LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
104 return handleCommandFailed();
107 String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationRequest);
109 HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path,
110 requestDetailsWrapper, RequestReferencesContainer.class);
113 if (msoResponse.getStatus() >= 200 && msoResponse.getStatus() < 400) {
114 final Job.JobStatus jobStatus = Job.JobStatus.IN_PROGRESS;
115 final String requestId = msoResponse.getBody().getRequestReferences().getRequestId();
116 final String instanceId = msoResponse.getBody().getRequestReferences().getInstanceId();
117 asyncInstantiationBL.auditVidStatus(uuid, jobStatus);
118 setInitialRequestAuditStatusFromMso(requestId);
119 asyncInstantiationBL.updateServiceInfo(uuid, x-> {
120 x.setJobStatus(jobStatus);
121 x.setServiceInstanceId(instanceId);
124 return new NextCommand(jobStatus, new InProgressStatusCommand(uuid, requestId));
126 auditService.setFailedAuditStatusFromMso(uuid,null, msoResponse.getStatus(),
127 Objects.toString(msoResponse.getBody()));
128 return handleCommandFailed();
133 private void setInitialRequestAuditStatusFromMso(String requestId){
134 final String initialMsoRequestStatus = "REQUESTED";
135 asyncInstantiationBL.auditMsoStatus(uuid,initialMsoRequestStatus,requestId,null);
138 protected NextCommand handleCommandFailed() {
139 asyncInstantiationBL.handleFailedInstantiation(uuid);
140 return new NextCommand(Job.JobStatus.FAILED);
144 public ServiceInstantiationCommand init(UUID jobUuid, Map<String, Object> data) {
145 final Object request = data.get("request");
149 OBJECT_MAPPER.convertValue(request, ServiceInstantiation.class),
150 (String) data.get("userId")
154 private ServiceInstantiationCommand init(UUID jobUuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
156 this.serviceInstantiationRequest = serviceInstantiationRequest;
157 this.userId = userId;
163 public Map<String, Object> getData() {
164 return ImmutableMap.of(
166 "request", serviceInstantiationRequest,