Use workflow's name for invocation
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / InstanceManagement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.apihandlerinfra;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import org.apache.http.HttpStatus;
29 import org.onap.so.apihandler.common.ErrorNumbers;
30 import org.onap.so.apihandler.common.RequestClientParameter;
31 import org.onap.so.apihandlerinfra.exceptions.ApiException;
32 import org.onap.so.apihandlerinfra.exceptions.RecipeNotFoundException;
33 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
34 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
35 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
36 import org.onap.so.db.catalog.beans.Workflow;
37 import org.onap.so.db.catalog.client.CatalogDbClient;
38 import org.onap.so.db.request.beans.InfraActiveRequests;
39 import org.onap.so.db.request.client.RequestsDbClient;
40 import org.onap.so.exceptions.ValidationException;
41 import org.onap.so.logger.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.onap.so.serviceinstancebeans.ModelType;
44 import org.onap.so.serviceinstancebeans.RequestReferences;
45 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
46 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.stereotype.Component;
51 import javax.transaction.Transactional;
52 import javax.ws.rs.Consumes;
53 import javax.ws.rs.POST;
54 import javax.ws.rs.Path;
55 import javax.ws.rs.PathParam;
56 import javax.ws.rs.Produces;
57 import javax.ws.rs.container.ContainerRequestContext;
58 import javax.ws.rs.core.Context;
59 import javax.ws.rs.core.MediaType;
60 import javax.ws.rs.core.Response;
61 import java.io.IOException;
62 import java.util.HashMap;
63
64 @Component
65 @Path("/onap/so/infra/instanceManagement")
66 @Api(value = "/onap/so/infra/instanceManagement", description = "Infrastructure API Requests for Instance Management")
67 public class InstanceManagement {
68
69     private static Logger logger = LoggerFactory.getLogger(InstanceManagement.class);
70     private static String uriPrefix = "/instanceManagement/";
71     private static final String SAVE_TO_DB = "save instance to db";
72
73     @Autowired
74     private RequestsDbClient infraActiveRequestsClient;
75
76     @Autowired
77     private CatalogDbClient catalogDbClient;
78
79     @Autowired
80     private MsoRequest msoRequest;
81
82     @Autowired
83     private RequestHandlerUtils requestHandlerUtils;
84
85     @POST
86     @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/workflows/{workflowUuid}")
87     @Consumes(MediaType.APPLICATION_JSON)
88     @Produces(MediaType.APPLICATION_JSON)
89     @ApiOperation(value = "Execute custom workflow", response = Response.class)
90     @Transactional
91     public Response executeCustomWorkflow(String request, @PathParam("version") String version,
92             @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("vnfInstanceId") String vnfInstanceId,
93             @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
94             throws ApiException {
95         String requestId = requestHandlerUtils.getRequestId(requestContext);
96         HashMap<String, String> instanceIdMap = new HashMap<>();
97         instanceIdMap.put("serviceInstanceId", serviceInstanceId);
98         instanceIdMap.put("vnfInstanceId", vnfInstanceId);
99         instanceIdMap.put("workflowUuid", workflowUuid);
100         return processCustomWorkflowRequest(request, Action.inPlaceSoftwareUpdate, instanceIdMap, version, requestId,
101                 requestContext);
102     }
103
104     private Response processCustomWorkflowRequest(String requestJSON, Actions action,
105             HashMap<String, String> instanceIdMap, String version, String requestId,
106             ContainerRequestContext requestContext) throws ApiException {
107         String serviceInstanceId = null;
108         if (instanceIdMap != null) {
109             serviceInstanceId = instanceIdMap.get("serviceInstanceId");
110         }
111         Boolean aLaCarte = true;
112         long startTime = System.currentTimeMillis();
113         ServiceInstancesRequest sir = null;
114         String apiVersion = version.substring(1);
115
116         String requestUri = requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
117
118         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, startTime, sir, msoRequest,
119                 requestId, requestUri);
120         String requestScope = requestHandlerUtils.deriveRequestScope(action, sir, requestUri);
121         InfraActiveRequests currentActiveReq =
122                 msoRequest.createRequestObject(sir, action, requestId, Status.IN_PROGRESS, requestJSON, requestScope);
123
124         try {
125             requestHandlerUtils.validateHeaders(requestContext);
126         } catch (ValidationException e) {
127             logger.error("Exception occurred", e);
128             ErrorLoggerInfo errorLoggerInfo =
129                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
130                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
131             ValidateException validateException =
132                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
133                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
134             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
135             throw validateException;
136         }
137
138         requestHandlerUtils.parseRequest(sir, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
139                 currentActiveReq);
140         requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
141
142         int requestVersion = Integer.parseInt(version.substring(1));
143         String vnfType = msoRequest.getVnfType(sir, requestScope, action, requestVersion);
144
145         if (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && vnfType != null) {
146             currentActiveReq.setVnfType(vnfType);
147         }
148
149         InfraActiveRequests dup = null;
150         boolean inProgress = false;
151
152         dup = requestHandlerUtils.duplicateCheck(action, instanceIdMap, startTime, msoRequest, null, requestScope,
153                 currentActiveReq);
154
155         if (dup != null) {
156             inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq);
157         }
158
159         if (dup != null && inProgress) {
160             requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, startTime,
161                     msoRequest, null, requestScope, dup);
162         }
163         ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse();
164
165         RequestReferences referencesResponse = new RequestReferences();
166
167         referencesResponse.setRequestId(requestId);
168
169         serviceResponse.setRequestReferences(referencesResponse);
170         Boolean isBaseVfModule = false;
171
172         String workflowUuid = null;
173         if (instanceIdMap != null) {
174             workflowUuid = instanceIdMap.get("workflowUuid");
175         }
176
177         RecipeLookupResult recipeLookupResult = getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
178
179         String serviceInstanceType = requestHandlerUtils.getServiceType(requestScope, sir, true);
180
181         serviceInstanceId = requestHandlerUtils.setServiceInstanceId(requestScope, sir);
182         String vnfId = "";
183
184         if (sir.getVnfInstanceId() != null) {
185             vnfId = sir.getVnfInstanceId();
186         }
187
188         try {
189             infraActiveRequestsClient.save(currentActiveReq);
190         } catch (Exception e) {
191             ErrorLoggerInfo errorLoggerInfo =
192                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
193                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
194             throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
195                     ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
196         }
197
198         RequestClientParameter requestClientParameter = null;
199         try {
200             requestClientParameter = new RequestClientParameter.Builder().setRequestId(requestId)
201                     .setBaseVfModule(isBaseVfModule).setRecipeTimeout(recipeLookupResult.getRecipeTimeout())
202                     .setRequestAction(action.toString()).setServiceInstanceId(serviceInstanceId).setVnfId(vnfId)
203                     .setServiceType(serviceInstanceType).setVnfType(vnfType)
204                     .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(requestJSON, sir, aLaCarte, action))
205                     .setApiVersion(apiVersion).setALaCarte(aLaCarte).setRequestUri(requestUri).build();
206         } catch (IOException e) {
207             ErrorLoggerInfo errorLoggerInfo =
208                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
209                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
210             throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
211                     HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
212                             .build();
213         }
214         return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
215                 recipeLookupResult.getOrchestrationURI(), requestScope);
216     }
217
218     private RecipeLookupResult getInstanceManagementWorkflowRecipe(InfraActiveRequests currentActiveReq,
219             String workflowUuid) throws ApiException {
220         RecipeLookupResult recipeLookupResult = null;
221
222         try {
223             recipeLookupResult = getCustomWorkflowUri(workflowUuid);
224         } catch (IOException e) {
225             ErrorLoggerInfo errorLoggerInfo =
226                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
227                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
228             ValidateException validateException =
229                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
230                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
231             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
232             throw validateException;
233         }
234
235         if (recipeLookupResult == null) {
236             ErrorLoggerInfo errorLoggerInfo =
237                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
238                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
239             RecipeNotFoundException recipeNotFoundExceptionException =
240                     new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
241                             HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
242                                     .build();
243             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED,
244                     recipeNotFoundExceptionException.getMessage());
245             throw recipeNotFoundExceptionException;
246         }
247
248         return recipeLookupResult;
249     }
250
251     private RecipeLookupResult getCustomWorkflowUri(String workflowUuid) throws IOException {
252
253         String recipeUri = null;
254         Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
255         if (workflow == null) {
256             return null;
257         } else {
258             String workflowName = workflow.getName();
259             recipeUri = "/mso/async/services/" + workflowName;
260         }
261         return new RecipeLookupResult(recipeUri, 180);
262     }
263 }