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) {
77 String filePath = "model.json";
79 String json = FileCommonUtils.readString(filePath);
80 return Response.status(Response.Status.OK).entity(json).build();
81 } catch (IOException e) {
82 logger.error("get workflow failed.", e);
83 throw RestUtils.newInternalServerErrorException(e);
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 @ApiOperation(value = "Save Model", response = String.class)
94 @ApiResponses(value = {
95 @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
96 response = String.class),
97 @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
98 message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
99 @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
100 response = String.class)})
102 public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
103 @ApiParam(value = "Model Content", required = true) String json) {
105 FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
107 URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
108 String processName = "plan_" + UUID.randomUUID().toString();
109 Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
110 Process process = parser.parse(processName, srcUri);
112 // transform bpmn template
113 BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
114 String bpmn = writer.completePlanTemplate();
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);