f39a95e92c8ad6ab2eb4b29f79775e5817467aea
[so.git] /
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  * Modifications Copyright (c) 2020 Nokia
11  * ================================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.so.apihandlerinfra;
27
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.constants.Status;
37 import org.onap.so.db.catalog.beans.Workflow;
38 import org.onap.so.db.catalog.client.CatalogDbClient;
39 import org.onap.so.db.request.beans.InfraActiveRequests;
40 import org.onap.so.db.request.client.RequestsDbClient;
41 import org.onap.so.exceptions.ValidationException;
42 import org.onap.logging.filter.base.ErrorCode;
43 import org.onap.so.logger.MessageEnum;
44 import org.onap.so.serviceinstancebeans.ModelType;
45 import org.onap.so.serviceinstancebeans.RequestReferences;
46 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
47 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Component;
52 import javax.transaction.Transactional;
53 import javax.ws.rs.Consumes;
54 import javax.ws.rs.POST;
55 import javax.ws.rs.Path;
56 import javax.ws.rs.PathParam;
57 import javax.ws.rs.Produces;
58 import javax.ws.rs.container.ContainerRequestContext;
59 import javax.ws.rs.core.Context;
60 import javax.ws.rs.core.MediaType;
61 import javax.ws.rs.core.Response;
62 import java.io.IOException;
63 import java.util.HashMap;
64 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
65 import io.swagger.v3.oas.annotations.Operation;
66 import io.swagger.v3.oas.annotations.info.Info;
67 import io.swagger.v3.oas.annotations.media.ArraySchema;
68 import io.swagger.v3.oas.annotations.media.Content;
69 import io.swagger.v3.oas.annotations.media.Schema;
70 import io.swagger.v3.oas.annotations.responses.ApiResponse;
71
72 @Component
73 @Path("/onap/so/infra/instanceManagement")
74 @OpenAPIDefinition(info = @Info(title = "/onap/so/infra/instanceManagement",
75         description = "Infrastructure API Requests for Instance Management"))
76 public class InstanceManagement {
77
78     private static Logger logger = LoggerFactory.getLogger(InstanceManagement.class);
79     private static String uriPrefix = "/instanceManagement/";
80     private static final String SAVE_TO_DB = "save instance to db";
81
82     @Autowired
83     private RequestsDbClient infraActiveRequestsClient;
84
85     @Autowired
86     private CatalogDbClient catalogDbClient;
87
88     @Autowired
89     private MsoRequest msoRequest;
90
91     @Autowired
92     private RequestHandlerUtils requestHandlerUtils;
93
94     @POST
95     @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/workflows/{workflowUuid}")
96     @Consumes(MediaType.APPLICATION_JSON)
97     @Produces(MediaType.APPLICATION_JSON)
98     @Operation(description = "Execute custom VNF workflow", responses = @ApiResponse(
99             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
100     @Transactional
101     public Response executeVNFCustomWorkflow(String request, @PathParam("version") String version,
102             @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("vnfInstanceId") String vnfInstanceId,
103             @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
104             throws ApiException {
105         String requestId = requestHandlerUtils.getRequestId(requestContext);
106         HashMap<String, String> instanceIdMap = new HashMap<>();
107         instanceIdMap.put("serviceInstanceId", serviceInstanceId);
108         instanceIdMap.put("vnfInstanceId", vnfInstanceId);
109         instanceIdMap.put("workflowUuid", workflowUuid);
110         return processCustomWorkflowRequest(request, Action.inPlaceSoftwareUpdate, instanceIdMap, version, requestId,
111                 requestContext);
112     }
113
114     @POST
115     @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/pnfs/{pnfName}/workflows/{workflowUuid}")
116     @Consumes(MediaType.APPLICATION_JSON)
117     @Produces(MediaType.APPLICATION_JSON)
118     @Operation(description = "Execute custom PNF workflow", responses = @ApiResponse(
119             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
120     @Transactional
121     public Response executePNFCustomWorkflow(String request, @PathParam("version") String version,
122             @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("pnfName") String pnfName,
123             @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
124             throws ApiException {
125         String requestId = requestHandlerUtils.getRequestId(requestContext);
126         HashMap<String, String> instanceIdMap = new HashMap<>();
127         instanceIdMap.put("serviceInstanceId", serviceInstanceId);
128         instanceIdMap.put("pnfName", pnfName);
129         instanceIdMap.put("workflowUuid", workflowUuid);
130         return processPNFCustomWorkflowRequest(request, Action.forCustomWorkflow, instanceIdMap, version, requestId,
131                 requestContext);
132     }
133
134     private Response processCustomWorkflowRequest(String requestJSON, Actions action,
135             HashMap<String, String> instanceIdMap, String version, String requestId,
136             ContainerRequestContext requestContext) throws ApiException {
137         String serviceInstanceId;
138         boolean aLaCarte = true;
139         ServiceInstancesRequest sir;
140         String apiVersion = version.substring(1);
141
142         String requestUri = requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
143
144         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
145         String requestScope = requestHandlerUtils.deriveRequestScope(action, sir, requestUri);
146         InfraActiveRequests currentActiveReq =
147                 msoRequest.createRequestObject(sir, action, requestId, Status.IN_PROGRESS, requestJSON, requestScope);
148
149         try {
150             requestHandlerUtils.validateHeaders(requestContext);
151         } catch (ValidationException e) {
152             logger.error("Exception occurred", e);
153             ErrorLoggerInfo errorLoggerInfo =
154                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
155                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
156             ValidateException validateException =
157                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
158                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
159             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
160             throw validateException;
161         }
162
163         requestHandlerUtils.parseRequest(sir, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
164                 currentActiveReq);
165         requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
166
167         String vnfType = msoRequest.getVnfType(sir, requestScope);
168
169         if (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && vnfType != null) {
170             currentActiveReq.setVnfType(vnfType);
171         }
172
173         checkDuplicateAndBuildError(action, instanceIdMap, requestScope, currentActiveReq);
174
175         ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse();
176
177         RequestReferences referencesResponse = new RequestReferences();
178
179         referencesResponse.setRequestId(requestId);
180
181         serviceResponse.setRequestReferences(referencesResponse);
182         boolean isBaseVfModule = false;
183
184         String workflowUuid = null;
185         if (instanceIdMap != null) {
186             workflowUuid = instanceIdMap.get("workflowUuid");
187         }
188
189         RecipeLookupResult recipeLookupResult = getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
190
191         String serviceInstanceType = requestHandlerUtils.getServiceType(requestScope, sir, true);
192
193         serviceInstanceId = requestHandlerUtils.setServiceInstanceId(requestScope, sir);
194         String vnfId = "";
195
196         if (sir.getVnfInstanceId() != null) {
197             vnfId = sir.getVnfInstanceId();
198         }
199
200         currentActiveReq = setWorkflowNameAndOperationName(currentActiveReq, workflowUuid);
201         saveCurrentActiveRequest(currentActiveReq);
202
203         RequestClientParameter requestClientParameter;
204         try {
205             requestClientParameter = new RequestClientParameter.Builder().setRequestId(requestId)
206                     .setBaseVfModule(isBaseVfModule).setRecipeTimeout(recipeLookupResult.getRecipeTimeout())
207                     .setRequestAction(action.toString()).setServiceInstanceId(serviceInstanceId).setVnfId(vnfId)
208                     .setServiceType(serviceInstanceType).setVnfType(vnfType)
209                     .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(requestJSON, null, aLaCarte, action))
210                     .setApiVersion(apiVersion).setALaCarte(aLaCarte).setRequestUri(requestUri).build();
211         } catch (IOException e) {
212             ErrorLoggerInfo errorLoggerInfo =
213                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
214                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
215             throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
216                     HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
217                             .build();
218         }
219         return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
220                 recipeLookupResult.getOrchestrationURI(), requestScope);
221     }
222
223     private void saveCurrentActiveRequest(InfraActiveRequests currentActiveReq) throws RequestDbFailureException {
224         try {
225             infraActiveRequestsClient.save(currentActiveReq);
226         } catch (Exception e) {
227             ErrorLoggerInfo errorLoggerInfo =
228                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
229                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
230             throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
231                     ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
232         }
233     }
234
235     private void checkDuplicateAndBuildError(Actions action, HashMap<String, String> instanceIdMap, String requestScope,
236             InfraActiveRequests currentActiveReq) throws ApiException {
237
238         InfraActiveRequests dup =
239                 requestHandlerUtils.duplicateCheck(action, instanceIdMap, null, requestScope, currentActiveReq);
240         if (dup == null) {
241             return;
242         }
243
244         boolean inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq);
245         if (inProgress) {
246             requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, null, requestScope,
247                     dup);
248         }
249     }
250
251     private Response processPNFCustomWorkflowRequest(String requestJSON, Actions action,
252             HashMap<String, String> instanceIdMap, String version, String requestId,
253             ContainerRequestContext requestContext) throws ApiException {
254         boolean aLaCarte = false;
255         ServiceInstancesRequest sir;
256         String apiVersion = version.substring(1);
257
258         String serviceInstanceId = "";
259         String pnfName = "";
260         String workflowUuid = "";
261         if (instanceIdMap != null) {
262             serviceInstanceId = instanceIdMap.get("serviceInstanceId");
263             pnfName = instanceIdMap.get("pnfName");
264             workflowUuid = instanceIdMap.get("workflowUuid");
265         }
266
267         String requestUri = requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
268         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
269         sir.setServiceInstanceId(serviceInstanceId);
270         sir.setPnfName(pnfName);
271         String requestScope = ModelType.pnf.name();
272         InfraActiveRequests currentActiveReq =
273                 msoRequest.createRequestObject(sir, action, requestId, Status.IN_PROGRESS, requestJSON, requestScope);
274
275         try {
276             requestHandlerUtils.validateHeaders(requestContext);
277         } catch (ValidationException e) {
278             logger.error("Exception occurred", e);
279             ErrorLoggerInfo errorLoggerInfo =
280                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
281                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
282             ValidateException validateException =
283                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
284                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
285             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
286             throw validateException;
287         }
288
289         requestHandlerUtils.parseRequest(sir, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
290                 currentActiveReq);
291         requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
292
293         checkDuplicateAndBuildError(action, instanceIdMap, requestScope, currentActiveReq);
294
295         RecipeLookupResult recipeLookupResult = getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
296
297         currentActiveReq = setWorkflowNameAndOperationName(currentActiveReq, workflowUuid);
298         saveCurrentActiveRequest(currentActiveReq);
299
300         RequestClientParameter requestClientParameter;
301         try {
302             requestClientParameter = new RequestClientParameter.Builder().setRequestId(requestId)
303                     .setRecipeTimeout(recipeLookupResult.getRecipeTimeout()).setRequestAction(action.toString())
304                     .setServiceInstanceId(serviceInstanceId).setPnfCorrelationId(pnfName)
305                     .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(requestJSON, null, aLaCarte, action))
306                     .setApiVersion(apiVersion).setRequestUri(requestUri).build();
307         } catch (IOException e) {
308             ErrorLoggerInfo errorLoggerInfo =
309                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
310                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
311             throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
312                     HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
313                             .build();
314         }
315         return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
316                 recipeLookupResult.getOrchestrationURI(), requestScope);
317     }
318
319     private RecipeLookupResult getInstanceManagementWorkflowRecipe(InfraActiveRequests currentActiveReq,
320             String workflowUuid) throws ApiException {
321         RecipeLookupResult recipeLookupResult;
322
323         try {
324             recipeLookupResult = getCustomWorkflowUri(workflowUuid);
325         } catch (Exception e) {
326             ErrorLoggerInfo errorLoggerInfo =
327                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
328                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
329             ValidateException validateException =
330                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
331                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
332             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
333             throw validateException;
334         }
335
336         if (recipeLookupResult == null) {
337             ErrorLoggerInfo errorLoggerInfo =
338                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
339                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
340             RecipeNotFoundException recipeNotFoundExceptionException =
341                     new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
342                             HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
343                                     .build();
344             requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED,
345                     recipeNotFoundExceptionException.getMessage());
346             throw recipeNotFoundExceptionException;
347         }
348
349         return recipeLookupResult;
350     }
351
352     private RecipeLookupResult getCustomWorkflowUri(String workflowUuid) {
353
354         Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
355         if (workflow != null) {
356             String workflowName = workflow.getName();
357             String recipeUri = "/mso/async/services/" + workflowName;
358             return new RecipeLookupResult(recipeUri, 180);
359         }
360         return null;
361     }
362
363     protected InfraActiveRequests setWorkflowNameAndOperationName(InfraActiveRequests currentActiveReq,
364             String workflowUuid) {
365         Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
366         if (workflow != null) {
367             currentActiveReq.setWorkflowName(workflow.getName());
368             currentActiveReq.setOperationName(workflow.getOperationName());
369         }
370         return currentActiveReq;
371     }
372 }