2 * Copyright (c) 2017-2018 ZTE Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the Apache License, Version 2.0
5 * and the Eclipse Public License v1.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * ZTE - initial API and implementation and/or initial documentation
13 package org.onap.sdc.workflowdesigner.resources;
15 import java.io.IOException;
17 import java.nio.file.Paths;
18 import java.util.UUID;
20 import javax.ws.rs.Consumes;
21 import javax.ws.rs.GET;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.PathParam;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.sdc.workflowdesigner.common.WorkflowDesignerException;
31 import org.onap.sdc.workflowdesigner.externalservice.sdc.SDCServiceProxy;
32 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.WorkflowArtifactInfo;
33 import org.onap.sdc.workflowdesigner.model.Process;
34 import org.onap.sdc.workflowdesigner.parser.Bpmn4ToscaJsonParser;
35 import org.onap.sdc.workflowdesigner.resources.entity.WorkflowInfo;
36 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
37 import org.onap.sdc.workflowdesigner.utils.JsonUtils;
38 import org.onap.sdc.workflowdesigner.utils.RestUtils;
39 import org.onap.sdc.workflowdesigner.writer.BpmnPlanArtefactWriter;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.codahale.metrics.annotation.Timed;
45 import io.swagger.annotations.Api;
46 import io.swagger.annotations.ApiOperation;
47 import io.swagger.annotations.ApiParam;
48 import io.swagger.annotations.ApiResponse;
49 import io.swagger.annotations.ApiResponses;
52 * Workflow Modeler Resource.
56 @Api(tags = {"Workflow Modeler"})
57 public class WorkflowModelerResource {
58 private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
60 private static final String WORKFLOW_JSON_TEMP_FILE_NAME = "temp_workflow.json";
61 private static final String WORKFLOW_XML_TEMP_FILE_NAME = "temp_workflow.xml";
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 @ApiOperation(value = "Get Model", response = String.class)
73 @ApiResponses(value = {
74 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
75 response = String.class),
76 @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
77 message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
78 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
79 response = String.class)})
81 public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
83 String json = FileCommonUtils.readString(WORKFLOW_JSON_TEMP_FILE_NAME);
84 return Response.status(Response.Status.OK).entity(json).build();
85 } catch (IOException e) {
86 logger.error("get workflow failed.", e);
87 throw RestUtils.newInternalServerErrorException(e);
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_JSON)
97 @ApiOperation(value = "Save Model", response = String.class)
98 @ApiResponses(value = {
99 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
100 response = String.class),
101 @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
102 message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
103 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
104 response = String.class)})
106 public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
107 @ApiParam(value = "Model Content", required = true) String json) {
109 FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
111 URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
112 String processName = "plan_" + UUID.randomUUID().toString();
113 String bpmn = buildBPMN(srcUri, processName);
115 save2SDC(json, bpmn);
116 FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, bpmn);
118 return Response.status(Response.Status.OK).entity(json).build();
119 } catch (IOException e) {
120 logger.error("save workflow failed.", e);
121 throw RestUtils.newInternalServerErrorException(e);
122 } catch (Exception e) {
123 logger.error("convert workflow from json to bpmn failed.", e);
124 throw RestUtils.newInternalServerErrorException(e);
133 * @throws WorkflowDesignerException
135 private void save2SDC(String json, String bpmn) throws WorkflowDesignerException {
136 WorkflowInfo workflowInfo = JsonUtils.fromJson(json, WorkflowInfo.class);
137 WorkflowArtifactInfo workflowArtifactInfo =
138 new WorkflowArtifactInfo(workflowInfo.getName(), workflowInfo.getDescription(), bpmn);
140 SDCServiceProxy sdcProxy = new SDCServiceProxy();
141 sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
142 workflowInfo.getId(), workflowArtifactInfo);
150 * @throws IOException
153 private String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
154 Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
155 Process process = parser.parse(processName, srcUri);
157 // transform bpmn template
158 BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
159 return writer.completePlanTemplate();