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.List;
19 //import java.util.UUID;
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;
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;
48 import com.codahale.metrics.annotation.Timed;
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;
57 * Workflow Modeler Resource.
61 @Api(tags = {"Workflow Modeler"})
62 public class WorkflowModelerResource {
63 private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
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";
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)})
86 public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
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);
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)})
111 public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
112 @ApiParam(value = "Model Content", required = true) String json) {
114 FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
117 // URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
118 // String processName = "plan_" + UUID.randomUUID().toString();
119 // String bpmn = buildBPMN(srcUri, processName);
120 // String jsonBpmn = insertJson2Bpmn(json, bpmn);
122 // save2SDC(json, jsonBpmn);
123 // FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, jsonBpmn);
125 return Response.status(Response.Status.OK).entity(json).build();
126 } catch (IOException e) {
127 logger.error("save workflow failed.", e);
128 throw RestUtils.newInternalServerErrorException(e);
129 } catch (Exception e) {
130 logger.error("convert workflow from json to bpmn failed.", e);
131 throw RestUtils.newInternalServerErrorException(e);
140 protected String insertJson2Bpmn(String json, String bpmn) {
141 StringBuffer sb = new StringBuffer(bpmn);
142 sb.append("<!-- \n").append(json).append("-->\n");
144 return sb.toString();
150 * @throws DocumentException
152 protected String readJsonfromBPMNFile(String bpmn) throws DocumentException {
153 Document doc = DocumentHelper.parseText(bpmn);
154 List<?> elementList = doc.content();
155 for (Object object : elementList) {
156 if (object instanceof Comment) {
157 Comment comment = (Comment) object;
158 return comment.getText().trim();
169 // * @throws WorkflowDesignerException
171 // private void save2SDC(String json, String bpmn) throws WorkflowDesignerException {
172 // WorkflowInfo workflowInfo = JsonUtils.fromJson(json, WorkflowInfo.class);
173 // WorkflowArtifactInfo workflowArtifactInfo =
174 // new WorkflowArtifactInfo(workflowInfo.getName(), workflowInfo.getDescription(), bpmn);
176 // SDCServiceProxy sdcProxy = new SDCServiceProxy();
177 // sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
178 // workflowInfo.getId(), workflowArtifactInfo);
186 * @throws IOException
189 protected String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
190 Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
191 Process process = parser.parse(processName, srcUri);
193 // transform bpmn template
194 BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
195 return writer.completePlanTemplate();