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.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.so.logger.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;
66 @Path("/onap/so/infra/instanceManagement")
67 @Api(value = "/onap/so/infra/instanceManagement", description = "Infrastructure API Requests for Instance Management")
68 public class InstanceManagement {
70 private static Logger logger = LoggerFactory.getLogger(InstanceManagement.class);
71 private static String uriPrefix = "/instanceManagement/";
72 private static final String SAVE_TO_DB = "save instance to db";
75 private RequestsDbClient infraActiveRequestsClient;
78 private CatalogDbClient catalogDbClient;
81 private MsoRequest msoRequest;
84 private RequestHandlerUtils requestHandlerUtils;
87 @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/workflows/{workflowUuid}")
88 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces(MediaType.APPLICATION_JSON)
90 @ApiOperation(value = "Execute custom workflow", response = Response.class)
92 public Response executeCustomWorkflow(String request, @PathParam("version") String version,
93 @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("vnfInstanceId") String vnfInstanceId,
94 @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
96 String requestId = requestHandlerUtils.getRequestId(requestContext);
97 HashMap<String, String> instanceIdMap = new HashMap<>();
98 instanceIdMap.put("serviceInstanceId", serviceInstanceId);
99 instanceIdMap.put("vnfInstanceId", vnfInstanceId);
100 instanceIdMap.put("workflowUuid", workflowUuid);
101 return processCustomWorkflowRequest(request, Action.inPlaceSoftwareUpdate, instanceIdMap, version, requestId,
105 private Response processCustomWorkflowRequest(String requestJSON, Actions action,
106 HashMap<String, String> instanceIdMap, String version, String requestId,
107 ContainerRequestContext requestContext) throws ApiException {
108 String serviceInstanceId;
109 Boolean aLaCarte = true;
110 ServiceInstancesRequest sir;
111 String apiVersion = version.substring(1);
113 String requestUri = requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
115 sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
116 String requestScope = requestHandlerUtils.deriveRequestScope(action, sir, requestUri);
117 InfraActiveRequests currentActiveReq =
118 msoRequest.createRequestObject(sir, action, requestId, Status.IN_PROGRESS, requestJSON, requestScope);
121 requestHandlerUtils.validateHeaders(requestContext);
122 } catch (ValidationException e) {
123 logger.error("Exception occurred", e);
124 ErrorLoggerInfo errorLoggerInfo =
125 new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
126 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
127 ValidateException validateException =
128 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
129 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
130 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
131 throw validateException;
134 requestHandlerUtils.parseRequest(sir, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
136 requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
138 int requestVersion = Integer.parseInt(version.substring(1));
139 String vnfType = msoRequest.getVnfType(sir, requestScope, action, requestVersion);
141 if (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && vnfType != null) {
142 currentActiveReq.setVnfType(vnfType);
145 InfraActiveRequests dup = null;
146 boolean inProgress = false;
148 dup = requestHandlerUtils.duplicateCheck(action, instanceIdMap, null, requestScope, currentActiveReq);
151 inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq);
154 if (dup != null && inProgress) {
155 requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, null, requestScope,
158 ServiceInstancesResponse serviceResponse = new ServiceInstancesResponse();
160 RequestReferences referencesResponse = new RequestReferences();
162 referencesResponse.setRequestId(requestId);
164 serviceResponse.setRequestReferences(referencesResponse);
165 Boolean isBaseVfModule = false;
167 String workflowUuid = null;
168 if (instanceIdMap != null) {
169 workflowUuid = instanceIdMap.get("workflowUuid");
172 RecipeLookupResult recipeLookupResult = getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
174 String serviceInstanceType = requestHandlerUtils.getServiceType(requestScope, sir, true);
176 serviceInstanceId = requestHandlerUtils.setServiceInstanceId(requestScope, sir);
179 if (sir.getVnfInstanceId() != null) {
180 vnfId = sir.getVnfInstanceId();
184 infraActiveRequestsClient.save(currentActiveReq);
185 } catch (Exception e) {
186 ErrorLoggerInfo errorLoggerInfo =
187 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
188 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
189 throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
190 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
193 RequestClientParameter requestClientParameter = null;
195 requestClientParameter = new RequestClientParameter.Builder().setRequestId(requestId)
196 .setBaseVfModule(isBaseVfModule).setRecipeTimeout(recipeLookupResult.getRecipeTimeout())
197 .setRequestAction(action.toString()).setServiceInstanceId(serviceInstanceId).setVnfId(vnfId)
198 .setServiceType(serviceInstanceType).setVnfType(vnfType)
199 .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(requestJSON, null, aLaCarte, action))
200 .setApiVersion(apiVersion).setALaCarte(aLaCarte).setRequestUri(requestUri).build();
201 } catch (IOException e) {
202 ErrorLoggerInfo errorLoggerInfo =
203 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
204 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
205 throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
206 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
209 return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
210 recipeLookupResult.getOrchestrationURI(), requestScope);
213 private RecipeLookupResult getInstanceManagementWorkflowRecipe(InfraActiveRequests currentActiveReq,
214 String workflowUuid) throws ApiException {
215 RecipeLookupResult recipeLookupResult = null;
218 recipeLookupResult = getCustomWorkflowUri(workflowUuid);
219 } catch (Exception e) {
220 ErrorLoggerInfo errorLoggerInfo =
221 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
222 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
223 ValidateException validateException =
224 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
225 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
226 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
227 throw validateException;
230 if (recipeLookupResult == null) {
231 ErrorLoggerInfo errorLoggerInfo =
232 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
233 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
234 RecipeNotFoundException recipeNotFoundExceptionException =
235 new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
236 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
238 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED,
239 recipeNotFoundExceptionException.getMessage());
240 throw recipeNotFoundExceptionException;
243 return recipeLookupResult;
246 private RecipeLookupResult getCustomWorkflowUri(String workflowUuid) {
248 String recipeUri = null;
249 Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
250 if (workflow == null) {
253 String workflowName = workflow.getName();
254 recipeUri = "/mso/async/services/" + workflowName;
256 return new RecipeLookupResult(recipeUri, 180);