Merge "Fix Nexus IQ issues."
[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       // TODO for Nexus-IQ
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);
121
122 //      save2SDC(json, jsonBpmn);
123 //      FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, jsonBpmn);
124
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);
132     }
133   }
134
135   /**
136    * @param json
137    * @param bpmn
138    * @return
139    */
140   protected String insertJson2Bpmn(String json, String bpmn) {
141     StringBuffer sb = new StringBuffer(bpmn);
142     sb.append("<!-- \n").append(json).append("-->\n");
143
144     return sb.toString();
145   }
146
147   /**
148    * 
149    * @return
150    * @throws DocumentException
151    */
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();
159       }
160     }
161
162     return null;
163   }
164
165
166 //  /**
167 //   * @param json
168 //   * @param bpmn
169 //   * @throws WorkflowDesignerException
170 //   */
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);
175 //
176 //    SDCServiceProxy sdcProxy = new SDCServiceProxy();
177 //    sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
178 //        workflowInfo.getId(), workflowArtifactInfo);
179 //  }
180
181   /**
182    * 
183    * @param srcUri
184    * @param processName
185    * @return
186    * @throws IOException
187    * @throws Exception
188    */
189   protected String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
190     Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
191     Process process = parser.parse(processName, srcUri);
192
193     // transform bpmn template
194     BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
195     return writer.completePlanTemplate();
196   }
197
198 }