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.UUID;
49 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
50 public class ServiceInstantiationCommand implements JobCommand {
52 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
54 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
57 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
60 private AuditService auditService;
63 private MsoInterface restMso;
66 private ServiceInstantiation serviceInstantiationRequest;
67 private String userId;
69 public ServiceInstantiationCommand() {
72 public ServiceInstantiationCommand(UUID uuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
73 init(uuid, serviceInstantiationRequest, userId);
77 public NextCommand call() {
78 RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
80 requestDetailsWrapper = asyncInstantiationBL.generateServiceInstantiationRequest(
81 uuid, serviceInstantiationRequest, userId
85 //Aai return bad response while checking names uniqueness
86 catch (InvalidAAIResponseException exception) {
87 LOGGER.error("Failed to check name uniqueness in AAI. VID will try again later", exception);
88 //put the job in_progress so we will keep trying to check name uniqueness in AAI
89 //And then send the request to MSO
90 return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
93 //Vid reached to max retries while trying to find unique name in AAI
94 catch (MaxRetriesException exception) {
95 LOGGER.error("Failed to find unused name in AAI. Set the job to FAILED ", exception);
96 return handleCommandFailed();
99 String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationRequest);
101 HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path,
102 requestDetailsWrapper, RequestReferencesContainer.class);
105 if (msoResponse.getStatus() >= 200 && msoResponse.getStatus() < 400) {
106 final Job.JobStatus jobStatus = Job.JobStatus.IN_PROGRESS;
107 final String requestId = msoResponse.getBody().getRequestReferences().getRequestId();
108 final String instanceId = msoResponse.getBody().getRequestReferences().getInstanceId();
109 asyncInstantiationBL.auditVidStatus(uuid, jobStatus);
110 setInitialRequestAuditStatusFromMso(requestId);
111 asyncInstantiationBL.updateServiceInfo(uuid, x-> {
112 x.setJobStatus(jobStatus);
113 x.setServiceInstanceId(instanceId);
116 return new NextCommand(jobStatus, new InProgressStatusCommand(uuid, requestId));
118 auditService.setFailedAuditStatusFromMso(uuid,null, msoResponse.getStatus(),
119 msoResponse.getBody().toString());
120 return handleCommandFailed();
125 private void setInitialRequestAuditStatusFromMso(String requestId){
126 final String initialMsoRequestStatus = "REQUESTED";
127 asyncInstantiationBL.auditMsoStatus(uuid,initialMsoRequestStatus,requestId,null);
130 protected NextCommand handleCommandFailed() {
131 asyncInstantiationBL.handleFailedInstantiation(uuid);
132 return new NextCommand(Job.JobStatus.FAILED);
136 public ServiceInstantiationCommand init(UUID jobUuid, Map<String, Object> data) {
137 final Object request = data.get("request");
141 OBJECT_MAPPER.convertValue(request, ServiceInstantiation.class),
142 (String) data.get("userId")
146 private ServiceInstantiationCommand init(UUID jobUuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
148 this.serviceInstantiationRequest = serviceInstantiationRequest;
149 this.userId = userId;
155 public Map<String, Object> getData() {
156 return ImmutableMap.of(
158 "request", serviceInstantiationRequest,