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);
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);
121 save2SDC(json, jsonBpmn);
122 FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, jsonBpmn);
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);
139 protected String insertJson2Bpmn(String json, String bpmn) {
140 StringBuffer sb = new StringBuffer(bpmn);
141 sb.append("<!-- \n").append(json).append("-->\n");
143 return sb.toString();
149 * @throws DocumentException
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();
168 * @throws WorkflowDesignerException
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);
175 SDCServiceProxy sdcProxy = new SDCServiceProxy();
176 sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
177 workflowInfo.getId(), workflowArtifactInfo);
185 * @throws IOException
188 protected String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
189 Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
190 Process process = parser.parse(processName, srcUri);
192 // transform bpmn template
193 BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
194 return writer.completePlanTemplate();