Get Icon Data of Extend Activity.
[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.List;
19 import java.util.UUID;
20
21 import javax.ws.rs.Consumes;
22 import javax.ws.rs.GET;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29
30 import org.dom4j.Comment;
31 import org.dom4j.Document;
32 import org.dom4j.DocumentException;
33 import org.dom4j.DocumentHelper;
34 import org.eclipse.jetty.http.HttpStatus;
35 import org.onap.sdc.workflowdesigner.common.WorkflowDesignerException;
36 import org.onap.sdc.workflowdesigner.externalservice.sdc.SDCServiceProxy;
37 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.WorkflowArtifactInfo;
38 import org.onap.sdc.workflowdesigner.model.Process;
39 import org.onap.sdc.workflowdesigner.parser.Bpmn4ToscaJsonParser;
40 import org.onap.sdc.workflowdesigner.resources.entity.WorkflowInfo;
41 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
42 import org.onap.sdc.workflowdesigner.utils.JsonUtils;
43 import org.onap.sdc.workflowdesigner.utils.RestUtils;
44 import org.onap.sdc.workflowdesigner.writer.BpmnPlanArtefactWriter;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.codahale.metrics.annotation.Timed;
49
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
53 import io.swagger.annotations.ApiResponse;
54 import io.swagger.annotations.ApiResponses;
55
56 /**
57  * Workflow Modeler Resource.
58  * 
59  */
60 @Path("/models")
61 @Api(tags = {"Workflow Modeler"})
62 public class WorkflowModelerResource {
63   private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
64
65   private static final String WORKFLOW_JSON_TEMP_FILE_NAME = "temp_workflow.json";
66   private static final String WORKFLOW_XML_TEMP_FILE_NAME = "temp_workflow.xml";
67
68
69   /**
70    * 
71    * @return Response
72    */
73   @Path("/{id}")
74   @GET
75   @Consumes(MediaType.APPLICATION_JSON)
76   @Produces(MediaType.APPLICATION_JSON)
77   @ApiOperation(value = "Get Model", response = String.class)
78   @ApiResponses(value = {
79       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
80           response = String.class),
81       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
82           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
83       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
84           response = String.class)})
85   @Timed
86   public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
87     try {
88       String json = FileCommonUtils.readString(WORKFLOW_JSON_TEMP_FILE_NAME);
89       return Response.status(Response.Status.OK).entity(json).build();
90     } catch (IOException e) {
91       logger.error("get workflow failed.", e);
92       throw RestUtils.newInternalServerErrorException(e);
93     }
94   }
95
96
97
98   @Path("/{id}")
99   @PUT
100   @Consumes(MediaType.APPLICATION_JSON)
101   @Produces(MediaType.APPLICATION_JSON)
102   @ApiOperation(value = "Save Model", response = String.class)
103   @ApiResponses(value = {
104       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
105           response = String.class),
106       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
107           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
108       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
109           response = String.class)})
110   @Timed
111   public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
112       @ApiParam(value = "Model Content", required = true) String json) {
113     try {
114       FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
115
116       URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
117       String processName = "plan_" + UUID.randomUUID().toString();
118       String bpmn = buildBPMN(srcUri, processName);
119       String jsonBpmn = insertJson2Bpmn(json, bpmn);
120
121 //      save2SDC(json, jsonBpmn);
122       FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, jsonBpmn);
123
124       return Response.status(Response.Status.OK).entity(json).build();
125     } catch (IOException e) {
126       logger.error("save workflow failed.", e);
127       throw RestUtils.newInternalServerErrorException(e);
128     } catch (Exception e) {
129       logger.error("convert workflow from json to bpmn failed.", e);
130       throw RestUtils.newInternalServerErrorException(e);
131     }
132   }
133
134   /**
135    * @param json
136    * @param bpmn
137    * @return
138    */
139   protected String insertJson2Bpmn(String json, String bpmn) {
140     StringBuffer sb = new StringBuffer(bpmn);
141     sb.append("<!-- \n").append(json).append("-->\n");
142
143     return sb.toString();
144   }
145
146   /**
147    * 
148    * @return
149    * @throws DocumentException
150    */
151   protected String readJsonfromBPMNFile(String bpmn) throws DocumentException {
152     Document doc = DocumentHelper.parseText(bpmn);
153     List<?> elementList = doc.content();
154     for (Object object : elementList) {
155       if (object instanceof Comment) {
156         Comment comment = (Comment) object;
157         return comment.getText().trim();
158       }
159     }
160
161     return null;
162   }
163
164
165   /**
166    * @param json
167    * @param bpmn
168    * @throws WorkflowDesignerException
169    */
170   private void save2SDC(String json, String bpmn) throws WorkflowDesignerException {
171     WorkflowInfo workflowInfo = JsonUtils.fromJson(json, WorkflowInfo.class);
172     WorkflowArtifactInfo workflowArtifactInfo =
173         new WorkflowArtifactInfo(workflowInfo.getName(), workflowInfo.getDescription(), bpmn);
174
175     SDCServiceProxy sdcProxy = new SDCServiceProxy();
176     sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
177         workflowInfo.getId(), workflowArtifactInfo);
178   }
179
180   /**
181    * 
182    * @param srcUri
183    * @param processName
184    * @return
185    * @throws IOException
186    * @throws Exception
187    */
188   protected String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
189     Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
190     Process process = parser.parse(processName, srcUri);
191
192     // transform bpmn template
193     BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
194     return writer.completePlanTemplate();
195   }
196
197 }