958fc115e277d7ed0603f6073ff4a7893c9d88dd
[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.Objects;
46 import java.util.UUID;
47
48
49 @Component
50 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
51 public class ServiceInstantiationCommand implements JobCommand {
52
53     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
54
55     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ServiceInstantiationCommand.class);
56
57     @Inject
58     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
59
60     @Inject
61     private AuditService auditService;
62
63     @Inject
64     private MsoInterface restMso;
65
66     private UUID uuid;
67     private ServiceInstantiation serviceInstantiationRequest;
68     private String userId;
69
70     public ServiceInstantiationCommand() {
71     }
72
73     public ServiceInstantiationCommand(UUID uuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
74         init(uuid, serviceInstantiationRequest, userId);
75     }
76
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;
83     }
84
85     @Override
86     public NextCommand call() {
87         RequestDetailsWrapper<ServiceInstantiationRequestDetails> requestDetailsWrapper ;
88         try {
89             requestDetailsWrapper = asyncInstantiationBL.generateServiceInstantiationRequest(
90                     uuid, serviceInstantiationRequest, userId
91             );
92         }
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);
99         }
100
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();
105         }
106
107         String path = asyncInstantiationBL.getServiceInstantiationPath(serviceInstantiationRequest);
108
109         HttpResponse<RequestReferencesContainer> msoResponse = restMso.post(path,
110             requestDetailsWrapper, RequestReferencesContainer.class);
111
112
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);
122             });
123
124             return new NextCommand(jobStatus, new InProgressStatusCommand(uuid, requestId));
125         } else {
126             auditService.setFailedAuditStatusFromMso(uuid,null, msoResponse.getStatus(),
127                     Objects.toString(msoResponse.getBody()));
128             return handleCommandFailed();
129         }
130
131     }
132
133     private void setInitialRequestAuditStatusFromMso(String requestId){
134         final String initialMsoRequestStatus = "REQUESTED";
135         asyncInstantiationBL.auditMsoStatus(uuid,initialMsoRequestStatus,requestId,null);
136     }
137
138     protected NextCommand handleCommandFailed() {
139         asyncInstantiationBL.handleFailedInstantiation(uuid);
140         return new NextCommand(Job.JobStatus.FAILED);
141     }
142
143     @Override
144     public ServiceInstantiationCommand init(UUID jobUuid, Map<String, Object> data) {
145         final Object request = data.get("request");
146
147         return init(
148                 jobUuid,
149                 OBJECT_MAPPER.convertValue(request, ServiceInstantiation.class),
150                 (String) data.get("userId")
151         );
152     }
153
154     private ServiceInstantiationCommand init(UUID jobUuid, ServiceInstantiation serviceInstantiationRequest, String userId) {
155         this.uuid = jobUuid;
156         this.serviceInstantiationRequest = serviceInstantiationRequest;
157         this.userId = userId;
158
159         return this;
160     }
161
162     @Override
163     public Map<String, Object> getData() {
164         return ImmutableMap.of(
165                 "uuid", uuid,
166                 "request", serviceInstantiationRequest,
167                 "userId", userId
168         );
169     }
170 }