4a40597a8be8b85f30eecb3513d27dc61f225295
[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.util.HashMap;
17 import java.util.Map;
18
19 import javax.ws.rs.Consumes;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.onap.sdc.workflowdesigner.common.ActivitySpecProxyException;
29 import org.onap.sdc.workflowdesigner.config.AppConfig;
30 import org.onap.sdc.workflowdesigner.externalservice.sdc.ActivitySpecServiceProxy;
31 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.ActivitySpec;
32 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.Parameter;
33 import org.onap.sdc.workflowdesigner.resources.entity.ExtActivityDisplayInfo;
34 import org.onap.sdc.workflowdesigner.resources.entity.I18nString;
35 import org.onap.sdc.workflowdesigner.resources.entity.InputOutput;
36 import org.onap.sdc.workflowdesigner.resources.entity.NodeCategory;
37 import org.onap.sdc.workflowdesigner.resources.entity.CategoryData;
38 import org.onap.sdc.workflowdesigner.resources.entity.Content;
39 import org.onap.sdc.workflowdesigner.resources.entity.ExtActivity;
40 import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
41 import org.onap.sdc.workflowdesigner.utils.JsonUtils;
42 import org.onap.sdc.workflowdesigner.utils.RestUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.codahale.metrics.annotation.Timed;
47
48 import io.swagger.annotations.Api;
49 import io.swagger.annotations.ApiOperation;
50 import io.swagger.annotations.ApiParam;
51 import io.swagger.annotations.ApiResponse;
52 import io.swagger.annotations.ApiResponses;
53
54 /**
55  * Extend Activity Resource.
56  * 
57  */
58 @Path("/ext-activities")
59 @Api(tags = {"Workflow Modeler"})
60 public class ExtendActivityResource {
61   private static final Logger LOGGER = LoggerFactory.getLogger(ExtendActivityResource.class);
62
63   /** */
64   private static final String EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME =
65       "ext-activities-display-info.json";
66
67   private static final String EXT_ACTIVITIES_FILE_NAME = "ext-activities.json";
68
69   private static final CategoryData EXTENSION_TASK_CATEGORY =
70       new CategoryData(new I18nString("Extension Task", "Extension Task"));
71
72   private static final String EXTENSION_TASK_CATEGORY_CATEGORY_ID = "extension_task_category_id";
73
74   /**
75    * test function.
76    * 
77    * @return Response
78    */
79   @Path("/")
80   @GET
81   @Consumes(MediaType.APPLICATION_JSON)
82   @Produces(MediaType.APPLICATION_JSON)
83   @ApiOperation(value = "Get Extend Activities.", response = ExtActivity.class,
84       responseContainer = "List")
85   @ApiResponses(value = {
86       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
87           response = String.class),
88       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
89           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
90       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
91           response = String.class)})
92   @Timed
93   public Response getExtActivities(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
94     if (AppConfig.isSDCAdapter()) {
95       return getExtActivitiesfromSDC();
96     } else {
97       return getExtActivitiesfromLocal();
98     }
99   }
100
101   /**
102    * @return
103    */
104   private Response getExtActivitiesfromLocal() {
105     try {
106       String json = FileCommonUtils.readString(EXT_ACTIVITIES_FILE_NAME);
107       ExtActivity[] extActivities = JsonUtils.fromJson(json, ExtActivity[].class);
108       return Response.status(Response.Status.OK).entity(extActivities).build();
109     } catch (IOException e) {
110       LOGGER.error("Get ExtActivities from local failed.", e);
111       throw RestUtils.newInternalServerErrorException(e);
112     }
113   }
114
115   /**
116    * @return
117    */
118   private Response getExtActivitiesfromSDC() {
119     try {
120       ActivitySpecServiceProxy proxy = new ActivitySpecServiceProxy();
121       ActivitySpec[] activitySpecs = proxy.getActivitySpecs();
122       ExtActivity[] extActivities = convert2ExtActivities(activitySpecs);
123       return Response.status(Response.Status.OK).entity(extActivities).build();
124     } catch (ActivitySpecProxyException e) {
125       LOGGER.error("Get ExtActivities from sdc failed.", e);
126       throw RestUtils.newInternalServerErrorException(e);
127     }
128   }
129
130   /**
131    * @param activitySpecs
132    * @return
133    */
134   private ExtActivity[] convert2ExtActivities(ActivitySpec[] activitySpecs) {
135     ExtActivity[] extendActivities = new ExtActivity[activitySpecs.length];
136     for (int i = 0; i < activitySpecs.length; i++) {
137       extendActivities[i] = convert2ExtActivity(activitySpecs[i]);
138     }
139     return extendActivities;
140   }
141
142   /**
143    * @param activitySpec
144    * @return
145    */
146   private ExtActivity convert2ExtActivity(ActivitySpec activitySpec) {
147     ExtActivity extActivity = new ExtActivity();
148     extActivity.setId(activitySpec.getId());
149     extActivity.setDisplayName(new I18nString(activitySpec.getName(), activitySpec.getName()));
150     extActivity.setDescription(
151         new I18nString(activitySpec.getDescription(), activitySpec.getDescription()));
152     extActivity.setType(activitySpec.getType());
153     extActivity.setContent(buildContent(activitySpec));
154     return extActivity;
155   }
156
157   /**
158    * @param activitySpec
159    * @return
160    */
161   private Content buildContent(ActivitySpec activitySpec) {
162     Content content = new Content();
163     content.setScript(activitySpec.getContent());
164     content.setInputs(convert2InputOutputs(activitySpec.getInputs()));
165     content.setOutputs(convert2InputOutputs(activitySpec.getOutputs()));
166     return content;
167   }
168
169   /**
170    * @param parameters
171    * @return
172    */
173   private Map<String, InputOutput> convert2InputOutputs(Parameter[] parameters) {
174     Map<String, InputOutput> inputOutputs = new HashMap<>();
175     for (Parameter parameter : parameters) {
176       inputOutputs.put(parameter.getName(), convert2InputOutput(parameter));
177     }
178     return inputOutputs;
179   }
180
181   /**
182    * @param parameter
183    * @return
184    */
185   private InputOutput convert2InputOutput(Parameter parameter) {
186     InputOutput inputOutput = new InputOutput();
187     inputOutput.setDisplayName(new I18nString(parameter.getName(), parameter.getName()));
188     inputOutput.setType(parameter.getType());
189     inputOutput.setDefault(parameter.getDefault());
190     inputOutput.setValue(parameter.getValue());
191     return inputOutput;
192   }
193
194
195   @Path("/displayInfo")
196   @GET
197   @Consumes(MediaType.APPLICATION_JSON)
198   @Produces(MediaType.APPLICATION_JSON)
199   @ApiOperation(value = "Get Extend Activities DisplayInfo",
200       response = ExtActivityDisplayInfo.class)
201   @ApiResponses(value = {
202       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
203           response = String.class),
204       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
205           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
206       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
207           response = String.class)})
208   @Timed
209   public Response getDisplayInfo(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
210     if (AppConfig.isSDCAdapter()) {
211       return getDisplayInfofromSDC();
212     } else {
213       return getDisplayInfofromLocal(sence);
214     }
215   }
216
217   /**
218    * @param sence
219    * @return
220    */
221   private Response getDisplayInfofromLocal(String sence) {
222     try {
223       ExtActivityDisplayInfo displayInfo = retriveDisplayInfo(sence);
224       return Response.status(Response.Status.OK).entity(displayInfo).build();
225     } catch (IOException e) {
226       LOGGER.error("Get Extend Activities DisplayInfo from failed.", e);
227       throw RestUtils.newInternalServerErrorException(e);
228     }
229   }
230
231   /**
232    * @return
233    */
234   private Response getDisplayInfofromSDC() {
235     try {
236       ActivitySpecServiceProxy proxy = new ActivitySpecServiceProxy();
237       ActivitySpec[] activitySpecs = proxy.getActivitySpecs();
238       ExtActivityDisplayInfo displayInfo = convert2ExtActivityDisplayInfo(activitySpecs);
239       return Response.status(Response.Status.OK).entity(displayInfo).build();
240     } catch (ActivitySpecProxyException e) {
241       LOGGER.error("Get Extend Activities DisplayInfo from sdc failed.", e);
242       throw RestUtils.newInternalServerErrorException(e);
243     }
244   }
245
246   /**
247    * @param activitySpecs
248    * @return
249    */
250   private ExtActivityDisplayInfo convert2ExtActivityDisplayInfo(ActivitySpec[] activitySpecs) {
251     ExtActivityDisplayInfo displayInfo = new ExtActivityDisplayInfo();
252
253     for (ActivitySpec activitySpec : activitySpecs) {
254       displayInfo.getNodes().put(activitySpec.getId(), buildNodeCategory(activitySpec));
255     }
256
257     displayInfo.getCategoryData().put(EXTENSION_TASK_CATEGORY_CATEGORY_ID, EXTENSION_TASK_CATEGORY);
258
259     return displayInfo;
260   }
261
262   /**
263    * @param activitySpec
264    * @return
265    */
266   private NodeCategory buildNodeCategory(ActivitySpec activitySpec) {
267     NodeCategory nodeCategory = new NodeCategory();
268     nodeCategory.setCategory(EXTENSION_TASK_CATEGORY_CATEGORY_ID);
269
270     return nodeCategory;
271   }
272
273   /**
274    * @param sence
275    * @return
276    * @throws IOException
277    */
278   private ExtActivityDisplayInfo retriveDisplayInfo(String sence) throws IOException {
279     String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
280     return JsonUtils.fromJson(json, ExtActivityDisplayInfo.class);
281   }
282
283 }