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 * Modifications Copyright (c) 2020 Nokia
11 * ================================================================================
12 * Modifications Copyright (c) 2020 Nordix
13 * ================================================================================
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
18 * http://www.apache.org/licenses/LICENSE-2.0
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.so.apihandlerinfra;
30 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
31 import io.swagger.v3.oas.annotations.Operation;
32 import io.swagger.v3.oas.annotations.info.Info;
33 import io.swagger.v3.oas.annotations.media.ArraySchema;
34 import io.swagger.v3.oas.annotations.media.Content;
35 import io.swagger.v3.oas.annotations.media.Schema;
36 import io.swagger.v3.oas.annotations.responses.ApiResponse;
37 import java.io.IOException;
38 import java.util.HashMap;
40 import javax.transaction.Transactional;
41 import javax.ws.rs.Consumes;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.container.ContainerRequestContext;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
50 import org.apache.http.HttpStatus;
51 import org.onap.logging.filter.base.ErrorCode;
52 import org.onap.so.apihandler.common.ErrorNumbers;
53 import org.onap.so.apihandler.common.RequestClientParameter;
54 import org.onap.so.apihandlerinfra.exceptions.ApiException;
55 import org.onap.so.apihandlerinfra.exceptions.RecipeNotFoundException;
56 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
57 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
58 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
59 import org.onap.so.constants.Status;
60 import org.onap.so.db.catalog.beans.Workflow;
61 import org.onap.so.db.catalog.client.CatalogDbClient;
62 import org.onap.so.db.request.beans.InfraActiveRequests;
63 import org.onap.so.db.request.client.RequestsDbClient;
64 import org.onap.so.exceptions.ValidationException;
65 import org.onap.so.logger.MessageEnum;
66 import org.onap.so.serviceinstancebeans.ModelType;
67 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.stereotype.Component;
74 @Path("/onap/so/infra/instanceManagement")
75 @OpenAPIDefinition(info = @Info(title = "/onap/so/infra/instanceManagement",
76 description = "Infrastructure API Requests for Instance Management"))
77 public class InstanceManagement {
79 private static final Logger LOG = LoggerFactory.getLogger(InstanceManagement.class);
80 private static final String URI_PREFIX = "/instanceManagement/";
81 private static final String SAVE_TO_DB = "save instance to db";
84 private RequestsDbClient infraActiveRequestsClient;
87 private CatalogDbClient catalogDbClient;
90 private MsoRequest msoRequest;
93 private RequestHandlerUtils requestHandlerUtils;
96 @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/workflows/{workflowUuid}")
97 @Consumes(MediaType.APPLICATION_JSON)
98 @Produces(MediaType.APPLICATION_JSON)
99 @Operation(description = "Execute custom VNF workflow", responses = @ApiResponse(
100 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
102 public Response executeVNFCustomWorkflow(String request, @PathParam("version") String version,
103 @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("vnfInstanceId") String vnfInstanceId,
104 @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
105 throws ApiException {
106 final String requestId = requestHandlerUtils.getRequestId(requestContext);
107 final Map<String, String> instanceIdMap = new HashMap<>();
108 instanceIdMap.put("serviceInstanceId", serviceInstanceId);
109 instanceIdMap.put("vnfInstanceId", vnfInstanceId);
110 instanceIdMap.put("workflowUuid", workflowUuid);
111 return processCustomWorkflowRequest(request, Action.inPlaceSoftwareUpdate, instanceIdMap, version, requestId,
112 requestContext, true);
116 @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/pnfs/{pnfName}/workflows/{workflowUuid}")
117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
119 @Operation(description = "Execute custom PNF workflow", responses = @ApiResponse(
120 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
122 public Response executePNFCustomWorkflow(String request, @PathParam("version") String version,
123 @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("pnfName") String pnfName,
124 @PathParam("workflowUuid") String workflowUuid, @Context ContainerRequestContext requestContext)
125 throws ApiException {
126 final String requestId = requestHandlerUtils.getRequestId(requestContext);
127 final Map<String, String> instanceIdMap = new HashMap<>();
128 instanceIdMap.put("serviceInstanceId", serviceInstanceId);
129 instanceIdMap.put("pnfName", pnfName);
130 instanceIdMap.put("workflowUuid", workflowUuid);
131 return processCustomWorkflowRequest(request, Action.forCustomWorkflow, instanceIdMap, version, requestId,
132 requestContext, false);
136 @Path("/{version:[vV][1]}/serviceInstances/{serviceInstanceId}/workflows/{workflowUuid}")
137 @Consumes(MediaType.APPLICATION_JSON)
138 @Produces(MediaType.APPLICATION_JSON)
139 @Operation(description = "Execute custom Service Level workflow", responses = @ApiResponse(
140 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
142 public Response executeServiceLevelCustomWorkflow(String request, @PathParam("version") String version,
143 @PathParam("serviceInstanceId") String serviceInstanceId, @PathParam("workflowUuid") String workflowUuid,
144 @Context ContainerRequestContext requestContext) throws ApiException {
145 final String requestId = requestHandlerUtils.getRequestId(requestContext);
146 final Map<String, String> instanceIdMap = new HashMap<>();
147 instanceIdMap.put("serviceInstanceId", serviceInstanceId);
148 instanceIdMap.put("workflowUuid", workflowUuid);
149 return processCustomWorkflowRequest(request, Action.forCustomWorkflow, instanceIdMap, version, requestId,
150 requestContext, false);
153 private Response processCustomWorkflowRequest(final String requestJSON, final Actions action,
154 final Map<String, String> instanceIdMap, final String version, final String requestId,
155 final ContainerRequestContext requestContext, final boolean aLaCarte) throws ApiException {
156 String pnfName = null;
157 String vnfType = null;
158 String workflowUuid = null;
159 String vnfInstanceId = null;
160 String svcInstanceId = null;
161 final String apiVersion = version.substring(1);
163 if (instanceIdMap != null && !instanceIdMap.isEmpty()) {
164 pnfName = instanceIdMap.get("pnfName");
165 workflowUuid = instanceIdMap.get("workflowUuid");
166 vnfInstanceId = instanceIdMap.get("vnfInstanceId");
167 svcInstanceId = instanceIdMap.get("serviceInstanceId");
170 final String requestUri = requestHandlerUtils.getRequestUri(requestContext, URI_PREFIX);
171 final ServiceInstancesRequest svcInsReq =
172 requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
173 final String requestScope = requestHandlerUtils.deriveRequestScope(action, svcInsReq, requestUri);
174 InfraActiveRequests currentActiveReq = msoRequest.createRequestObject(svcInsReq, action, requestId,
175 Status.IN_PROGRESS, requestJSON, requestScope);
178 requestHandlerUtils.validateHeaders(requestContext);
179 } catch (ValidationException e) {
180 LOG.error("Exception occurred", e);
181 ErrorLoggerInfo errorLoggerInfo =
182 new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
183 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
184 ValidateException validateException =
185 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
186 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
187 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
188 throw validateException;
191 requestHandlerUtils.parseRequest(svcInsReq, instanceIdMap, action, version, requestJSON, aLaCarte, requestId,
193 requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, null, instanceIdMap);
195 if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
196 vnfType = msoRequest.getVnfType(svcInsReq, requestScope);
197 currentActiveReq.setVnfType(vnfType);
200 checkDuplicateAndBuildError(action, instanceIdMap, requestScope, currentActiveReq);
201 final RecipeLookupResult recipeLookupResult =
202 getInstanceManagementWorkflowRecipe(currentActiveReq, workflowUuid);
204 currentActiveReq = setWorkflowNameAndOperationName(currentActiveReq, workflowUuid);
205 saveCurrentActiveRequest(currentActiveReq);
207 RequestClientParameter requestClientParameter;
209 requestClientParameter = new RequestClientParameter.Builder().setRequestId(requestId)
210 .setRecipeTimeout(recipeLookupResult.getRecipeTimeout()).setRequestAction(action.toString())
211 .setServiceInstanceId(svcInstanceId).setVnfId(vnfInstanceId).setVnfType(vnfType)
212 .setPnfCorrelationId(pnfName).setApiVersion(apiVersion)
213 .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(requestJSON, null, aLaCarte, action))
214 .setALaCarte(aLaCarte).setRequestUri(requestUri).build();
215 } catch (IOException e) {
216 ErrorLoggerInfo errorLoggerInfo =
217 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
218 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
219 throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
220 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
223 return requestHandlerUtils.postBPELRequest(currentActiveReq, requestClientParameter,
224 recipeLookupResult.getOrchestrationURI(), requestScope);
227 private void saveCurrentActiveRequest(InfraActiveRequests currentActiveReq) throws RequestDbFailureException {
229 infraActiveRequestsClient.save(currentActiveReq);
230 } catch (Exception e) {
231 ErrorLoggerInfo errorLoggerInfo =
232 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
233 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
234 throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
235 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
239 private void checkDuplicateAndBuildError(Actions action, Map<String, String> instanceIdMap, String requestScope,
240 InfraActiveRequests currentActiveReq) throws ApiException {
242 InfraActiveRequests dup =
243 requestHandlerUtils.duplicateCheck(action, instanceIdMap, null, requestScope, currentActiveReq);
248 boolean inProgress = requestHandlerUtils.camundaHistoryCheck(dup, currentActiveReq);
250 requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, null, requestScope,
255 private RecipeLookupResult getInstanceManagementWorkflowRecipe(InfraActiveRequests currentActiveReq,
256 String workflowUuid) throws ApiException {
257 RecipeLookupResult recipeLookupResult;
260 recipeLookupResult = getCustomWorkflowUri(workflowUuid);
261 } catch (Exception e) {
262 ErrorLoggerInfo errorLoggerInfo =
263 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
264 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
265 ValidateException validateException =
266 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
267 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
268 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
269 throw validateException;
272 if (recipeLookupResult == null) {
273 ErrorLoggerInfo errorLoggerInfo =
274 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
275 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
276 RecipeNotFoundException recipeNotFoundExceptionException =
277 new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
278 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
280 requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED,
281 recipeNotFoundExceptionException.getMessage());
282 throw recipeNotFoundExceptionException;
285 return recipeLookupResult;
288 private RecipeLookupResult getCustomWorkflowUri(String workflowUuid) {
290 Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
291 if (workflow != null) {
292 String workflowName = workflow.getName();
293 String recipeUri = "/mso/async/services/" + workflowName;
294 return new RecipeLookupResult(recipeUri, 180);
299 protected InfraActiveRequests setWorkflowNameAndOperationName(InfraActiveRequests currentActiveReq,
300 String workflowUuid) {
301 Workflow workflow = catalogDbClient.findWorkflowByArtifactUUID(workflowUuid);
302 if (workflow != null) {
303 currentActiveReq.setWorkflowName(workflow.getName());
304 currentActiveReq.setOperationName(workflow.getOperationName());
306 return currentActiveReq;