Merge "Replace SO client"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ServiceInstantiationCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.vid.job.command;
22
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;
42
43 import javax.inject.Inject;
44 import java.util.Map;
45 import java.util.UUID;
46
47
48 @Component
49 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
50 public class ServiceInstantiationCommand implements JobCommand {
51
52     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
53
54     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
55
56     @Inject
57     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
58
59     @Inject
60     private AuditService auditService;
61
62     @Inject
63     private MsoInterface restMso;
64
65     private UUID uuid;
66     private ServiceInstantiation serviceInstantiationRequest;
67     private String userId;
68
69     public ServiceInstantiationCommand() {
70     }
71
72     public ServiceInstantiationCommand(UUID uuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
73         init(uuid, serviceInstantiationRequest, userId);
74     }
75
76     @Override
77     public NextCommand call() {
78         RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
79         try {
80             requestDetailsWrapper = asyncInstantiationBL.generateServiceInstantiationRequest(
81                     uuid, serviceInstantiationRequest, userId
82             );
83         }
84
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);
91         }
92
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();
97         }
98
99         String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationRequest);
100
101         HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path,
102             requestDetailsWrapper, RequestReferencesContainer.class);
103
104
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);
114             });
115
116             return new NextCommand(jobStatus, new InProgressStatusCommand(uuid, requestId));
117         } else {
118             auditService.setFailedAuditStatusFromMso(uuid,null, msoResponse.getStatus(),
119                 msoResponse.getBody().toString());
120             return handleCommandFailed();
121         }
122
123     }
124
125     private void setInitialRequestAuditStatusFromMso(String requestId){
126         final String initialMsoRequestStatus = "REQUESTED";
127         asyncInstantiationBL.auditMsoStatus(uuid,initialMsoRequestStatus,requestId,null);
128     }
129
130     protected NextCommand handleCommandFailed() {
131         asyncInstantiationBL.handleFailedInstantiation(uuid);
132         return new NextCommand(Job.JobStatus.FAILED);
133     }
134
135     @Override
136     public ServiceInstantiationCommand init(UUID jobUuid, Map<String, Object> data) {
137         final Object request = data.get("request");
138
139         return init(
140                 jobUuid,
141                 OBJECT_MAPPER.convertValue(request, ServiceInstantiation.class),
142                 (String) data.get("userId")
143         );
144     }
145
146     private ServiceInstantiationCommand init(UUID jobUuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
147         this.uuid = jobUuid;
148         this.serviceInstantiationRequest = serviceInstantiationRequest;
149         this.userId = userId;
150
151         return this;
152     }
153
154     @Override
155     public Map<String, Object> getData() {
156         return ImmutableMap.of(
157                 "uuid", uuid,
158                 "request", serviceInstantiationRequest,
159                 "userId", userId
160         );
161     }
162 }