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.model.Process;
31 import org.onap.sdc.workflowdesigner.parser.Bpmn4ToscaJsonParser;
32 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
33 import org.onap.sdc.workflowdesigner.utils.RestUtils;
34 import org.onap.sdc.workflowdesigner.writer.BpmnPlanArtefactWriter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.codahale.metrics.annotation.Timed;
40 import io.swagger.annotations.Api;
41 import io.swagger.annotations.ApiOperation;
42 import io.swagger.annotations.ApiParam;
43 import io.swagger.annotations.ApiResponse;
44 import io.swagger.annotations.ApiResponses;
47 * Workflow Modeler Resource.
51 @Api(tags = {"Workflow Modeler"})
52 public class WorkflowModelerResource {
53 private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
55 private static final String WORKFLOW_JSON_TEMP_FILE_NAME = "temp_workflow.json";
56 private static final String WORKFLOW_XML_TEMP_FILE_NAME = "temp_workflow.xml";
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
67 @ApiOperation(value = "Get Model", response = String.class)
68 @ApiResponses(value = {
69 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
70 response = String.class),
71 @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
72 message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
73 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
74 response = String.class)})
76 public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
78 String json = FileCommonUtils.readString(WORKFLOW_JSON_TEMP_FILE_NAME);
79 return Response.status(Response.Status.OK).entity(json).build();
80 } catch (IOException e) {
81 logger.error("get workflow failed.", e);
82 throw RestUtils.newInternalServerErrorException(e);
90 @Consumes(MediaType.APPLICATION_JSON)
91 @Produces(MediaType.APPLICATION_JSON)
92 @ApiOperation(value = "Save Model", response = String.class)
93 @ApiResponses(value = {
94 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
95 response = String.class),
96 @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
97 message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
98 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
99 response = String.class)})
101 public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
102 @ApiParam(value = "Model Content", required = true) String json) {
104 FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
106 URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
107 String processName = "plan_" + UUID.randomUUID().toString();
108 Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
109 Process process = parser.parse(processName, srcUri);
111 // transform bpmn template
112 BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
113 String bpmn = writer.completePlanTemplate();
115 FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, bpmn);
117 return Response.status(Response.Status.OK).entity(json).build();
118 } catch (IOException e) {
119 logger.error("save workflow failed.", e);
120 throw RestUtils.newInternalServerErrorException(e);
121 } catch (Exception e) {
122 logger.error("convert workflow from json to bpmn failed.", e);
123 throw RestUtils.newInternalServerErrorException(e);