aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/resources/ExtendActivityResource.java
blob: bd8a541f69d6ba781fec4b3094d01f5577cfc44e (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
/**
 * 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 javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.jetty.http.HttpStatus;
import org.onap.sdc.workflowdesigner.resources.entity.ExtActivityDisplayInfo;
import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;
import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
import org.onap.sdc.workflowdesigner.utils.JsonUtils;
import org.onap.sdc.workflowdesigner.utils.RestUtils;
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;

/**
 * Extend Activity Resource.
 * 
 */
@Path("/ext-activities")
@Api(tags = {"Workflow Modeler"})
public class ExtendActivityResource {
  private static final Logger LOGGER = LoggerFactory.getLogger(ExtendActivityResource.class);

  /** */
  private static final String EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME =
      "ext-activities-display-info.json";

  private static final String EXT_ACTIVITIES_FILE_NAME = "ext-activities.json";

  /**
   * test function.
   * 
   * @return Response
   */
  @Path("/")
  @GET
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Get Extend Activities.", response = ExtendActivity.class,
      responseContainer = "List")
  @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 getExtActivities(@ApiParam(value = "sence") @QueryParam("sence") String sence) {

    try {
      ExtendActivity[] extActivities = retriveExtActivites(sence);
      return Response.status(Response.Status.OK).entity(extActivities).build();
    } catch (IOException e) {
      LOGGER.error("Get ExtActivities failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    }

  }

  /**
   * @param sence
   * @return
   * @throws IOException
   */
  protected ExtendActivity[] retriveExtActivites(String sence) throws IOException {
    String json = FileCommonUtils.readString(EXT_ACTIVITIES_FILE_NAME);
    return JsonUtils.fromJson(json, ExtendActivity[].class);
  }


  @Path("/displayInfo")
  @GET
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ApiOperation(value = "Get Extend Activities DisplayInfo",
      response = ExtActivityDisplayInfo.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 getDisplayInfo(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
    try {
      ExtActivityDisplayInfo displayInfo = retriveDisplayInfo(sence);
      return Response.status(Response.Status.OK).entity(displayInfo).build();
    } catch (IOException e) {
      LOGGER.error("Get Extend Activities DisplayInfo failed.", e);
      throw RestUtils.newInternalServerErrorException(e);
    }
  }

  /**
   * @param sence
   * @return
   * @throws IOException
   */
  private ExtActivityDisplayInfo retriveDisplayInfo(String sence) throws IOException {
    String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
    return JsonUtils.fromJson(json, ExtActivityDisplayInfo.class);
  }

}