Get bpmn artifact from BE.
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-server / src / main / java / org / onap / sdc / workflowdesigner / resources / WorkflowModelerResource.java
1 /**
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
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 package org.onap.sdc.workflowdesigner.resources;
14
15 import java.io.IOException;
16 import java.net.URI;
17 import java.nio.file.Paths;
18 import java.util.UUID;
19
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;
28
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;
37
38 import com.codahale.metrics.annotation.Timed;
39
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;
45
46 /**
47  * Workflow Modeler Resource.
48  * 
49  */
50 @Path("/models")
51 @Api(tags = {"Workflow Modeler"})
52 public class WorkflowModelerResource {
53   private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
54   
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";
57
58
59   /**
60    * 
61    * @return Response
62    */
63   @Path("/{id}")
64   @GET
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)})
75   @Timed
76   public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
77     try {
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);
83     }
84   }
85
86   
87   
88   @Path("/{id}")
89   @PUT
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)})
100   @Timed
101   public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
102       @ApiParam(value = "Model Content", required = true) String json) {
103     try {
104       FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
105
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);
110       
111       // transform bpmn template
112       BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
113       String bpmn = writer.completePlanTemplate();
114       
115       FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, bpmn);
116       
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);
124     }
125   }
126
127 }