2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.apihandlerinfra;
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;
65 @Path("/onap/so/infra/instanceManagement")
66 @Api(value = "/onap/so/infra/instanceManagement", description = "Infrastructure API Requests for Instance Management")
67 public class InstanceManagement {
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";
74 private RequestsDbClient infraActiveRequestsClient;
77 private CatalogDbClient catalogDbClient;
80 private MsoRequest msoRequest;
83 private RequestHandlerUtils requestHandlerUtils;
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)
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)
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,
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");
111 Boolean aLaCarte = true;
112 long startTime = System.currentTimeMillis();
113 ServiceInstancesRequest sir = null;
114 String apiVersion = version.substring(1);
116 String requestUri = requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
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);
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;
138 requestHandlerUtils.parseRequest(sir, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
140 requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
142 int requestVersion = Integer.parseInt(version.substring(1));
143 String vnfType = msoRequest.getVnfType(sir, requestScope, action, requestVersion);
145 if (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && vnfType != null) {
146 currentActiveReq.setVnfType(vnfType);
149 InfraActiveRequests dup = null;
150 boolean inProgress = false;
152 dup = requestHandlerUtils.duplicateCheck(action, instanceIdMap, startTime, msoRequest, null, requestScope,
156 inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq);
159 if (dup != null && inProgress) {
160 requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, startTime,
161 msoRequest, null, requestScope, dup);
163 ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse();
165 RequestReferences referencesResponse = new RequestReferences();
167 referencesResponse.setRequestId(requestId);
169 serviceResponse.setRequestReferences(referencesResponse);
170 Boolean isBaseVfModule = false;
172 String workflowUuid = null;
173 if (instanceIdMap != null) {
174 workflowUuid = instanceIdMap.get("workflowUuid");
177 RecipeLookupResult recipeLookupResult = getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
179 String serviceInstanceType = requestHandlerUtils.getServiceType(requestScope, sir, true);
181 serviceInstanceId = requestHandlerUtils.setServiceInstanceId(requestScope, sir);
184 if (sir.getVnfInstanceId() != null) {
185 vnfId = sir.getVnfInstanceId();
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();
198 RequestClientParameter requestClientParameter = null;
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)
214 return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
215 recipeLookupResult.getOrchestrationURI(), requestScope);
218 private RecipeLookupResult getInstanceManagementWorkflowRecipe(InfraActiveRequests currentActiveReq,
219 String workflowUuid) throws ApiException {
220 RecipeLookupResult recipeLookupResult = null;
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;
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)
243 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED,
244 recipeNotFoundExceptionException.getMessage());
245 throw recipeNotFoundExceptionException;
248 return recipeLookupResult;
251 private RecipeLookupResult getCustomWorkflowUri(String workflowUuid) throws IOException {
253 String recipeUri = null;
254 Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
255 if (workflow == null) {
258 String workflowName = workflow.getName();
259 recipeUri = "/mso/async/services/" + workflowName;
261 return new RecipeLookupResult(recipeUri, 180);