f6f1699a0dc8e6e7effd7062bf85246bb3f2bd1d
[sdc/sdc-workflow-designer.git] /
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.common.WorkflowDesignerException;
31 import org.onap.sdc.workflowdesigner.externalservice.sdc.SDCServiceProxy;
32 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.WorkflowArtifactInfo;
33 import org.onap.sdc.workflowdesigner.model.Process;
34 import org.onap.sdc.workflowdesigner.parser.Bpmn4ToscaJsonParser;
35 import org.onap.sdc.workflowdesigner.resources.entity.WorkflowInfo;
36 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
37 import org.onap.sdc.workflowdesigner.utils.JsonUtils;
38 import org.onap.sdc.workflowdesigner.utils.RestUtils;
39 import org.onap.sdc.workflowdesigner.writer.BpmnPlanArtefactWriter;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.codahale.metrics.annotation.Timed;
44
45 import io.swagger.annotations.Api;
46 import io.swagger.annotations.ApiOperation;
47 import io.swagger.annotations.ApiParam;
48 import io.swagger.annotations.ApiResponse;
49 import io.swagger.annotations.ApiResponses;
50
51 /**
52  * Workflow Modeler Resource.
53  * 
54  */
55 @Path("/models")
56 @Api(tags = {"Workflow Modeler"})
57 public class WorkflowModelerResource {
58   private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
59
60   private static final String WORKFLOW_JSON_TEMP_FILE_NAME = "temp_workflow.json";
61   private static final String WORKFLOW_XML_TEMP_FILE_NAME = "temp_workflow.xml";
62
63
64   /**
65    * 
66    * @return Response
67    */
68   @Path("/{id}")
69   @GET
70   @Consumes(MediaType.APPLICATION_JSON)
71   @Produces(MediaType.APPLICATION_JSON)
72   @ApiOperation(value = "Get Model", response = String.class)
73   @ApiResponses(value = {
74       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
75           response = String.class),
76       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
77           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
78       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
79           response = String.class)})
80   @Timed
81   public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
82     try {
83       String json = FileCommonUtils.readString(WORKFLOW_JSON_TEMP_FILE_NAME);
84       return Response.status(Response.Status.OK).entity(json).build();
85     } catch (IOException e) {
86       logger.error("get workflow failed.", e);
87       throw RestUtils.newInternalServerErrorException(e);
88     }
89   }
90
91
92
93   @Path("/{id}")
94   @PUT
95   @Consumes(MediaType.APPLICATION_JSON)
96   @Produces(MediaType.APPLICATION_JSON)
97   @ApiOperation(value = "Save Model", response = String.class)
98   @ApiResponses(value = {
99       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
100           response = String.class),
101       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
102           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
103       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
104           response = String.class)})
105   @Timed
106   public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
107       @ApiParam(value = "Model Content", required = true) String json) {
108     try {
109       FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);
110
111       URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
112       String processName = "plan_" + UUID.randomUUID().toString();
113       String bpmn = buildBPMN(srcUri, processName);
114
115       save2SDC(json, bpmn);
116       FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, bpmn);
117
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);
125     }
126   }
127
128
129
130   /**
131    * @param json
132    * @param bpmn
133    * @throws WorkflowDesignerException
134    */
135   private void save2SDC(String json, String bpmn) throws WorkflowDesignerException {
136     WorkflowInfo workflowInfo = JsonUtils.fromJson(json, WorkflowInfo.class);
137     WorkflowArtifactInfo workflowArtifactInfo =
138         new WorkflowArtifactInfo(workflowInfo.getName(), workflowInfo.getDescription(), bpmn);
139
140     SDCServiceProxy sdcProxy = new SDCServiceProxy();
141     sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
142         workflowInfo.getId(), workflowArtifactInfo);
143   }
144
145   /**
146    * 
147    * @param srcUri
148    * @param processName
149    * @return
150    * @throws IOException
151    * @throws Exception
152    */
153   private String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
154     Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
155     Process process = parser.parse(processName, srcUri);
156
157     // transform bpmn template
158     BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
159     return writer.completePlanTemplate();
160   }
161
162 }