99e2f28a14593173f5975f9b141c5856f30f7bd2
[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.SDCProxyException;
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 (SDCProxyException 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.setClass(activitySpec.getContent().getClazz());
164     content.clazz = activitySpec.getContent().clazz;
165     content.setScript(activitySpec.getContent().getScript());
166     content.setScriptFormat(activitySpec.getContent().getScriptFormat());
167     content.setInputs(convert2InputOutputs(activitySpec.getInputs()));
168     content.setOutputs(convert2InputOutputs(activitySpec.getOutputs()));
169     return content;
170   }
171
172   /**
173    * @param parameters
174    * @return
175    */
176   private Map<String, InputOutput> convert2InputOutputs(Parameter[] parameters) {
177     Map<String, InputOutput> inputOutputs = new HashMap<>();
178     for (Parameter parameter : parameters) {
179       inputOutputs.put(parameter.getName(), convert2InputOutput(parameter));
180     }
181     return inputOutputs;
182   }
183
184   /**
185    * @param parameter
186    * @return
187    */
188   private InputOutput convert2InputOutput(Parameter parameter) {
189     InputOutput inputOutput = new InputOutput();
190     inputOutput.setDisplayName(new I18nString(parameter.getName(), parameter.getName()));
191     inputOutput.setType(parameter.getType());
192     inputOutput.setDefault(parameter.getDefault());
193     inputOutput.setValue(parameter.getValue());
194     return inputOutput;
195   }
196
197
198   @Path("/displayInfo")
199   @GET
200   @Consumes(MediaType.APPLICATION_JSON)
201   @Produces(MediaType.APPLICATION_JSON)
202   @ApiOperation(value = "Get Extend Activities DisplayInfo",
203       response = ExtActivityDisplayInfo.class)
204   @ApiResponses(value = {
205       @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found",
206           response = String.class),
207       @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415,
208           message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
209       @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "server internal error",
210           response = String.class)})
211   @Timed
212   public Response getDisplayInfo(@ApiParam(value = "sence") @QueryParam("sence") String sence) {
213     if (AppConfig.isSDCAdapter()) {
214       return getDisplayInfofromSDC();
215     } else {
216       return getDisplayInfofromLocal(sence);
217     }
218   }
219
220   /**
221    * @param sence
222    * @return
223    */
224   private Response getDisplayInfofromLocal(String sence) {
225     try {
226       ExtActivityDisplayInfo displayInfo = retriveDisplayInfo(sence);
227       return Response.status(Response.Status.OK).entity(displayInfo).build();
228     } catch (IOException e) {
229       LOGGER.error("Get Extend Activities DisplayInfo from failed.", e);
230       throw RestUtils.newInternalServerErrorException(e);
231     }
232   }
233
234   /**
235    * @return
236    */
237   private Response getDisplayInfofromSDC() {
238     try {
239       ActivitySpecServiceProxy proxy = new ActivitySpecServiceProxy();
240       ActivitySpec[] activitySpecs = proxy.getActivitySpecs();
241       ExtActivityDisplayInfo displayInfo = convert2ExtActivityDisplayInfo(activitySpecs);
242       return Response.status(Response.Status.OK).entity(displayInfo).build();
243     } catch (SDCProxyException e) {
244       LOGGER.error("Get Extend Activities DisplayInfo from sdc failed.", e);
245       throw RestUtils.newInternalServerErrorException(e);
246     }
247   }
248
249   /**
250    * @param activitySpecs
251    * @return
252    */
253   private ExtActivityDisplayInfo convert2ExtActivityDisplayInfo(ActivitySpec[] activitySpecs) {
254     ExtActivityDisplayInfo displayInfo = new ExtActivityDisplayInfo();
255
256     for (ActivitySpec activitySpec : activitySpecs) {
257       displayInfo.getNodes().put(activitySpec.getId(), buildNodeCategory(activitySpec));
258     }
259
260     displayInfo.getCategoryData().put(EXTENSION_TASK_CATEGORY_CATEGORY_ID, EXTENSION_TASK_CATEGORY);
261
262     return displayInfo;
263   }
264
265   /**
266    * @param activitySpec
267    * @return
268    */
269   private NodeCategory buildNodeCategory(ActivitySpec activitySpec) {
270     NodeCategory nodeCategory = new NodeCategory();
271     nodeCategory.setCategory(EXTENSION_TASK_CATEGORY_CATEGORY_ID);
272
273     return nodeCategory;
274   }
275
276   /**
277    * @param sence
278    * @return
279    * @throws IOException
280    */
281   private ExtActivityDisplayInfo retriveDisplayInfo(String sence) throws IOException {
282     String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
283     return JsonUtils.fromJson(json, ExtActivityDisplayInfo.class);
284   }
285
286 }