aboutsummaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/WorkflowModelerResource.java
blob: c08fcef5b3fd50b39373cd911b0c7997558ca1d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
 * Copyright (c) 2017-2018 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the Apache License, Version 2.0
 * and the Eclipse Public License v1.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */

package org.onap.sdc.workflowdesigner.resources;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.eclipse.jetty.http.HttpStatus;
import org.onap.sdc.workflowdesigner.common.SDCProxyException;
import org.onap.sdc.workflowdesigner.config.AppConfig;
import org.onap.sdc.workflowdesigner.externalservice.sdc.SDCServiceProxy;
import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.WorkflowArtifactInfo;
import org.onap.sdc.workflowdesigner.model.Process;
import org.onap.sdc.workflowdesigner.parser.Bpmn4ToscaJsonParser;
import org.onap.sdc.workflowdesigner.resources.entity.WorkflowInfo;
import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
import org.onap.sdc.workflowdesigner.utils.JsonUtils;
import org.onap.sdc.workflowdesigner.utils.RestUtils;
import org.onap.sdc.workflowdesigner.utils.ToolUtils;
import org.onap.sdc.workflowdesigner.writer.BpmnPlanArtefactWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.codahale.metrics.annotation.Timed;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * Workflow Modeler Resource.
 * 
 */
@Path("/models")
@Api(tags = {"Workflow Modeler"})
public class WorkflowModelerResource {
  private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);

  private static final String WORKFLOW_JSON_TEMP_FILE_NAME = "temp_workflow.json";
  private static final String WORKFLOW_XML_TEMP_FILE_NAME = "temp_workflow.xml";


  /**
   * 
   * @return Response
   */
  @Path("/{id}")
  @GET
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Get Model", response = WorkflowInfo.class)
  @ApiResponses(value = {
      @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
          response = String.class),
      @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
          message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
      @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
          response = String.class)})
  @Timed
  public Response getModel(@ApiParam(value = "id") @PathParam("id") String id,
      @ApiParam(value = "name") @QueryParam("name") String name,
      @ApiParam(value = "uuid") @QueryParam("uuid") String uuid,
      @ApiParam(value = "operationId") @QueryParam("operationId") String operationId) {
    if (AppConfig.isSDCAdapter()) {
      return getModelfromSDC(uuid, operationId, id, name);
    } else {
      return getModelfromLocal();
    }

  }

  /**
   * @return
   */
  private Response getModelfromLocal() {
    try {
      String json = FileCommonUtils.readString(WORKFLOW_JSON_TEMP_FILE_NAME);
      return Response.status(Response.Status.OK).entity(json).build();
    } catch (IOException e) {
      logger.error("get workflow from local failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    }
  }


  /**
   * @param uuid
   * @param operationId
   * @param id
   * @param name
   * @return
   */
  private Response getModelfromSDC(String uuid, String operationId, String id, String name) {
    try {
      SDCServiceProxy sdcProxy = new SDCServiceProxy();
      WorkflowArtifactInfo wai = sdcProxy.getWorkflowArtifact(uuid, operationId, id);
      String bpmn = wai.getPayloadData();
      String json = readJsonfromBPMN(bpmn);
      if (ToolUtils.isEmpty(json)) {
        WorkflowInfo wfi = newEmptyWorkflowInfo(uuid, operationId, id, name);
        return Response.status(Response.Status.OK).entity(wfi).build();
      }
      return Response.status(Response.Status.OK).entity(json).build();
    } catch (SDCProxyException e) {
      logger.error("get workflow from sdc failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    } catch (DocumentException e) {
      logger.error("get workflow from sdc failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    }
  }

  /**
   * @param id
   * @param operationId
   * @param uuid
   * @param name
   * @return
   */
  private WorkflowInfo newEmptyWorkflowInfo(String uuid, String operationId, String id,
      String name) {
    WorkflowInfo wfi = new WorkflowInfo();
    wfi.setId(id);
    wfi.setName(name);
    wfi.setUuid(uuid);
    wfi.setOperationId(operationId);

    return wfi;
  }



  @Path("/{id}")
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Save Model", response = String.class)
  @ApiResponses(value = {
      @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
          response = String.class),
      @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
          message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
      @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
          response = String.class)})
  @Timed
  public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
      @ApiParam(value = "Model Content", required = true) String json) {
    try {
      FileCommonUtils.write(WORKFLOW_JSON_TEMP_FILE_NAME, json);

      URI srcUri = Paths.get(".", WORKFLOW_JSON_TEMP_FILE_NAME).toUri();
      String processName = "plan_" + UUID.randomUUID().toString();
      String bpmn = buildBPMN(srcUri, processName);
      String jsonBpmn = insertJson2Bpmn(json, bpmn);

      if (AppConfig.isSDCAdapter()) {
        save2SDC(json, jsonBpmn);
      }
      FileCommonUtils.write(WORKFLOW_XML_TEMP_FILE_NAME, jsonBpmn);

      return Response.status(Response.Status.OK).entity(json).build();
    } catch (IOException e) {
      logger.error("save workflow failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    } catch (Exception e) {
      logger.error("convert workflow from json to bpmn failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    }
  }

  /**
   * @param json
   * @param bpmn
   * @return
   */
  protected String insertJson2Bpmn(String json, String bpmn) {
    StringBuffer sb = new StringBuffer(bpmn);
    sb.append("<!-- \n").append(json).append("-->\n");

    return sb.toString();
  }

  /**
   * 
   * @return
   * @throws DocumentException
   */
  protected String readJsonfromBPMN(String bpmn) throws DocumentException {
    if (ToolUtils.isEmpty(bpmn)) {
      return null;
    }

    Document doc = DocumentHelper.parseText(bpmn);
    List<?> elementList = doc.content();
    for (Object object : elementList) {
      if (object instanceof Comment) {
        Comment comment = (Comment) object;
        return comment.getText().trim();
      }
    }

    return null;
  }


  /**
   * @param json
   * @param bpmn
   * @throws SDCProxyException
   */
  private void save2SDC(String json, String bpmn) throws SDCProxyException {
    WorkflowInfo workflowInfo = JsonUtils.fromJson(json, WorkflowInfo.class);
    WorkflowArtifactInfo workflowArtifactInfo =
        new WorkflowArtifactInfo(workflowInfo.getName(), workflowInfo.getDescription(), bpmn);

    SDCServiceProxy sdcProxy = new SDCServiceProxy();
    sdcProxy.saveWorkflowArtifact(workflowInfo.getUuid(), workflowInfo.getOperationId(),
        workflowInfo.getId(), workflowArtifactInfo);
  }

  /**
   * 
   * @param srcUri
   * @param processName
   * @return
   * @throws IOException
   * @throws Exception
   */
  protected String buildBPMN(URI srcUri, String processName) throws IOException, Exception {
    Bpmn4ToscaJsonParser parser = new Bpmn4ToscaJsonParser();
    Process process = parser.parse(processName, srcUri);

    // transform bpmn template
    BpmnPlanArtefactWriter writer = new BpmnPlanArtefactWriter(process);
    return writer.completePlanTemplate();
  }

}