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