2ad4aeaeb4af4ccb20eabe5248782543ecfc8ab8
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017 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
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
28 import org.onap.sdc.workflowdesigner.utils.RestUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.codahale.metrics.annotation.Timed;
33
34 import io.swagger.annotations.Api;
35 import io.swagger.annotations.ApiOperation;
36 import io.swagger.annotations.ApiParam;
37 import io.swagger.annotations.ApiResponse;
38 import io.swagger.annotations.ApiResponses;
39
40 /**
41  * Workflow Modeler Resource.
42  * 
43  */
44 @Path("/models")
45 @Api(tags = {"Workflow Modeler"})
46 public class WorkflowModelerResource {
47   private static final Logger logger = LoggerFactory.getLogger(WorkflowModelerResource.class);
48
49   /**
50    * test function.
51    * 
52    * @return Response
53    */
54   @Path("/{id}")
55   @GET
56   @Consumes(MediaType.APPLICATION_JSON)
57   @Produces(MediaType.APPLICATION_JSON)
58   @ApiOperation(value = "Get Model", response = String.class)
59   @ApiResponses(value = {
60       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
61           response = String.class),
62       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
63           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
64       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
65           response = String.class)})
66   @Timed
67   public Response getModel(@ApiParam(value = "id") @PathParam("id") String id) {
68     String filePath = "model.json";
69     try {
70       String json = FileCommonUtils.readString(filePath);
71       return Response.status(Response.Status.OK).entity(json).build();
72     } catch (IOException e) {
73       logger.error("getServiceTemplateById failed.", e);
74       throw RestUtils.newInternalServerErrorException(e);
75     }
76   }
77
78   @Path("/{id}")
79   @PUT
80   @Consumes(MediaType.APPLICATION_JSON)
81   @Produces(MediaType.APPLICATION_JSON)
82   @ApiOperation(value = "Save Model", response = String.class)
83   @ApiResponses(value = {
84       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
85           response = String.class),
86       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
87           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
88       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
89           response = String.class)})
90   @Timed
91   public Response saveModel(@ApiParam(value = "id") @PathParam("id") String id,
92       @ApiParam(value = "Model Content", required = true) String json) {
93     String filePath = "model.json";
94     try {
95       FileCommonUtils.write(filePath, json);
96       return Response.status(Response.Status.OK).entity(json).build();
97     } catch (IOException e) {
98       logger.error("getServiceTemplateById failed.", e);
99       throw RestUtils.newInternalServerErrorException(e);
100     }
101   }
102
103 }