catalog-be servlets refactoring
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ElementServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.*;
26 import javax.inject.Inject;
27 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
28 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
30 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
36 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.info.ArtifactTypesInfo;
39 import org.openecomp.sdc.be.model.*;
40 import org.openecomp.sdc.be.model.Tag;
41 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
42 import org.openecomp.sdc.be.model.category.CategoryDefinition;
43 import org.openecomp.sdc.be.model.category.GroupingDefinition;
44 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
45 import org.openecomp.sdc.be.ui.model.UiCategories;
46 import org.openecomp.sdc.be.user.UserBusinessLogic;
47 import org.openecomp.sdc.common.api.Constants;
48 import org.openecomp.sdc.common.log.wrappers.Logger;
49 import org.openecomp.sdc.exception.ResponseFormat;
50
51 import javax.inject.Singleton;
52 import javax.servlet.ServletContext;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.ws.rs.*;
55 import javax.ws.rs.core.Context;
56 import javax.ws.rs.core.MediaType;
57 import javax.ws.rs.core.Response;
58 import java.util.ArrayList;
59 import java.util.HashMap;
60 import java.util.List;
61 import java.util.Map;
62
63 @Path("/v1/")
64
65 /****
66  * 
67  * UI oriented servlet - to return elements in specific format UI needs
68  * 
69  *
70  */
71 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
72 @Api(value = "Element Servlet", description = "Element Servlet")
73 @Singleton
74 public class ElementServlet extends BeGenericServlet {
75
76     private static final Logger log = Logger.getLogger(ElementServlet.class);
77     private final ComponentsCleanBusinessLogic componentsCleanBusinessLogic;
78     private final ElementBusinessLogic elementBusinessLogic;
79     private final UserBusinessLogic userBusinessLogic;
80
81     @Inject
82     public ElementServlet(UserBusinessLogic userBusinessLogic,
83         ComponentsUtils componentsUtils,
84         ComponentsCleanBusinessLogic componentsCleanBusinessLogic,
85         ElementBusinessLogic elementBusinessLogic) {
86         super(userBusinessLogic, componentsUtils);
87         this.componentsCleanBusinessLogic = componentsCleanBusinessLogic;
88         this.elementBusinessLogic = elementBusinessLogic;
89         this.userBusinessLogic = userBusinessLogic;
90     }
91
92     /*
93      ******************************************************************************
94      * NEW CATEGORIES category / \ subcategory subcategory / grouping
95      ******************************************************************************/
96
97     /*
98      *
99      *
100      * CATEGORIES
101      */
102     /////////////////////////////////////////////////////////////////////////////////////////////////////
103     // retrieve all component categories
104     @GET
105     @Path("/categories/{componentType}")
106     @Consumes(MediaType.APPLICATION_JSON)
107     @Produces(MediaType.APPLICATION_JSON)
108     @ApiOperation(value = "Retrieve the list of all resource/service/product categories/sub-categories/groupings", httpMethod = "GET", notes = "Retrieve the list of all resource/service/product categories/sub-categories/groupings.", response = Response.class)
109     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns categories Ok"), @ApiResponse(code = 403, message = "Missing information"), @ApiResponse(code = 400, message = "Invalid component type"),
110             @ApiResponse(code = 409, message = "Restricted operation"), @ApiResponse(code = 500, message = "Internal Server Error") })
111     public Response getComponentCategories(@ApiParam(value = "allowed values are resources / services/ products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
112             + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam(value = "componentType") final String componentType, @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
113
114         try {
115             Either<List<CategoryDefinition>, ResponseFormat> either = elementBusinessLogic.getAllCategories(componentType, userId);
116             if (either.isRight()) {
117                 log.debug("No categories were found for type {}", componentType);
118                 return buildErrorResponse(either.right().value());
119             } else {
120                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), either.left().value());
121             }
122         } catch (Exception e) {
123             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component Categories");
124             log.debug("getComponentCategories failed with exception", e);
125             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
126         }
127     }
128
129     @GET
130     @Path("/categories")
131     @Consumes(MediaType.APPLICATION_JSON)
132     @Produces(MediaType.APPLICATION_JSON)
133     @ApiOperation(value = "Retrieve the all resource, service and product categories", httpMethod = "GET", notes = "Retrieve the all resource, service and product categories", response = Response.class)
134     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns categories Ok"), @ApiResponse(code = 403, message = "Missing information"),
135             @ApiResponse(code = 409, message = "Restricted operation"), @ApiResponse(code = 500, message = "Internal Server Error") })
136     public Response getAllCategories(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
137
138         try {
139             Either<UiCategories, ResponseFormat> either = elementBusinessLogic
140                 .getAllCategories(userId);
141             if (either.isRight()) {
142                 log.debug("No categories were found");
143                 return buildErrorResponse(either.right().value());
144             } else {
145                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), either.left().value());
146             }
147         } catch (Exception e) {
148             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Categories");
149             log.debug("getAllCategories failed with exception", e);
150             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
151         }
152     }
153
154
155     @POST
156     @Path("/category/{componentType}")
157     @Consumes(MediaType.APPLICATION_JSON)
158     @Produces(MediaType.APPLICATION_JSON)
159     @ApiOperation(value = "Create new component category", httpMethod = "POST", notes = "Create new component category")
160     @ApiResponses(value = { @ApiResponse(code = 201, message = "Category created"), @ApiResponse(code = 400, message = "Invalid category data"), @ApiResponse(code = 403, message = "USER_ID header is missing"),
161             @ApiResponse(code = 409, message = "Category already exists / User not permitted to perform the action"), @ApiResponse(code = 500, message = "General Error") })
162     public Response createComponentCategory(
163             @ApiParam(value = "allowed values are resources /services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
164                     + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam(value = "componentType") final String componentType,
165             @ApiParam(value = "Category to be created", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
166         try {
167             CategoryDefinition category = RepresentationUtils.fromRepresentation(data, CategoryDefinition.class);
168
169             Either<CategoryDefinition, ResponseFormat> createResourceCategory =
170                 elementBusinessLogic.createCategory(category, componentType, userId);
171             if (createResourceCategory.isRight()) {
172                 return buildErrorResponse(createResourceCategory.right().value());
173             }
174
175             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
176             return buildOkResponse(responseFormat, createResourceCategory.left().value());
177
178         } catch (Exception e) {
179             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create resource category");
180             log.debug("createResourceCategory failed with exception", e);
181             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
182
183         }
184     }
185
186     @DELETE
187     @Path("/category/{componentType}/{categoryUniqueId}")
188     @Consumes(MediaType.APPLICATION_JSON)
189     @Produces(MediaType.APPLICATION_JSON)
190     @ApiOperation(value = "Delete component category", httpMethod = "DELETE", notes = "Delete component category", response = Category.class)
191     @ApiResponses(value = { @ApiResponse(code = 204, message = "Category deleted"), @ApiResponse(code = 403, message = "USER_ID header is missing"), @ApiResponse(code = 409, message = "User not permitted to perform the action"),
192             @ApiResponse(code = 404, message = "Category not found"), @ApiResponse(code = 500, message = "General Error") })
193     public Response deleteComponentCategory(@PathParam(value = "categoryUniqueId") final String categoryUniqueId, @PathParam(value = "componentType") final String componentType, @Context final HttpServletRequest request,
194             @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
195
196         try {
197             Either<CategoryDefinition, ResponseFormat> createResourceCategory =
198                 elementBusinessLogic.deleteCategory(categoryUniqueId, componentType, userId);
199
200             if (createResourceCategory.isRight()) {
201                 return buildErrorResponse(createResourceCategory.right().value());
202             }
203             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
204             return buildOkResponse(responseFormat, null);
205
206         } catch (Exception e) {
207             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create resource category");
208             log.debug("createResourceCategory failed with exception", e);
209             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
210
211         }
212     }
213
214     /*
215      *
216      *
217      * SUBCATEGORIES
218      *
219      */
220
221     @POST
222     @Path("/category/{componentType}/{categoryId}/subCategory")
223     @Consumes(MediaType.APPLICATION_JSON)
224     @Produces(MediaType.APPLICATION_JSON)
225     @ApiOperation(value = "Create new component sub-category", httpMethod = "POST", notes = "Create new component sub-category for existing category")
226     @ApiResponses(value = { @ApiResponse(code = 201, message = "Subcategory created"), @ApiResponse(code = 400, message = "Invalid subcategory data"), @ApiResponse(code = 403, message = "USER_ID header is missing"),
227             @ApiResponse(code = 404, message = "Parent category wasn't found"), @ApiResponse(code = 409, message = "Subcategory already exists / User not permitted to perform the action"), @ApiResponse(code = 500, message = "General Error") })
228     public Response createComponentSubCategory(
229             @ApiParam(value = "allowed values are resources / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + ","
230                     + ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam(value = "componentType") final String componentType,
231             @ApiParam(value = "Parent category unique ID", required = true) @PathParam(value = "categoryId") final String categoryId, @ApiParam(value = "Subcategory to be created. \ne.g. {\"name\":\"Resource-subcat\"}", required = true) String data,
232             @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
233
234         try {
235             SubCategoryDefinition subCategory = RepresentationUtils.fromRepresentation(data, SubCategoryDefinition.class);
236
237             Either<SubCategoryDefinition, ResponseFormat> createSubcategory = elementBusinessLogic
238                 .createSubCategory(subCategory, componentType, categoryId, userId);
239             if (createSubcategory.isRight()) {
240                 return buildErrorResponse(createSubcategory.right().value());
241             }
242             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
243             return buildOkResponse(responseFormat, createSubcategory.left().value());
244
245         } catch (Exception e) {
246             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create sub-category");
247             log.debug("createComponentSubCategory failed with exception", e);
248             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
249
250         }
251     }
252
253     @DELETE
254     @Path("/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}")
255     @Consumes(MediaType.APPLICATION_JSON)
256     @Produces(MediaType.APPLICATION_JSON)
257     @ApiOperation(value = "Delete component category", httpMethod = "DELETE", notes = "Delete component category", response = Category.class)
258     @ApiResponses(value = { @ApiResponse(code = 204, message = "Category deleted"), @ApiResponse(code = 403, message = "USER_ID header is missing"), @ApiResponse(code = 409, message = "User not permitted to perform the action"),
259             @ApiResponse(code = 404, message = "Category not found"), @ApiResponse(code = 500, message = "General Error") })
260     public Response deleteComponentSubCategory(@PathParam(value = "categoryUniqueId") final String categoryUniqueId, @PathParam(value = "subCategoryUniqueId") final String subCategoryUniqueId,
261             @PathParam(value = "componentType") final String componentType, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
262
263         try {
264             Either<SubCategoryDefinition, ResponseFormat> deleteSubResourceCategory =
265                 elementBusinessLogic.deleteSubCategory(subCategoryUniqueId, componentType, userId);
266             if (deleteSubResourceCategory.isRight()) {
267                 return buildErrorResponse(deleteSubResourceCategory.right().value());
268             }
269             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
270             return buildOkResponse(responseFormat, null);
271
272         } catch (Exception e) {
273             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete component category");
274             log.debug("deleteComponentSubCategory failed with exception", e);
275             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
276
277         }
278     }
279
280     /*
281      * GROUPINGS
282      */
283     @POST
284     @Path("/category/{componentType}/{categoryId}/subCategory/{subCategoryId}/grouping")
285     @Consumes(MediaType.APPLICATION_JSON)
286     @Produces(MediaType.APPLICATION_JSON)
287     @ApiOperation(value = "Create new component grouping", httpMethod = "POST", notes = "Create new component grouping for existing sub-category")
288     @ApiResponses(value = { @ApiResponse(code = 201, message = "Grouping created"), @ApiResponse(code = 400, message = "Invalid grouping data"), @ApiResponse(code = 403, message = "USER_ID header is missing"),
289             @ApiResponse(code = 404, message = "Parent category or subcategory were not found"), @ApiResponse(code = 409, message = "Grouping already exists / User not permitted to perform the action"),
290             @ApiResponse(code = 500, message = "General Error") })
291     public Response createComponentGrouping(@ApiParam(value = "allowed values are products", allowableValues = ComponentTypeEnum.PRODUCT_PARAM_NAME, required = true) @PathParam(value = "componentType") final String componentType,
292             @ApiParam(value = "Parent category unique ID", required = true) @PathParam(value = "categoryId") final String grandParentCategoryId,
293             @ApiParam(value = "Parent sub-category unique ID", required = true) @PathParam(value = "subCategoryId") final String parentSubCategoryId, @ApiParam(value = "Subcategory to be created", required = true) String data,
294             @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
295         try {
296             GroupingDefinition grouping = RepresentationUtils.fromRepresentation(data, GroupingDefinition.class);
297
298             Either<GroupingDefinition, ResponseFormat> createGrouping = elementBusinessLogic
299                 .createGrouping(grouping, componentType, grandParentCategoryId, parentSubCategoryId, userId);
300             if (createGrouping.isRight()) {
301                 return buildErrorResponse(createGrouping.right().value());
302             }
303             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
304             return buildOkResponse(responseFormat, createGrouping.left().value());
305
306         } catch (Exception e) {
307             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create grouping");
308             log.debug("createComponentGrouping failed with exception", e);
309             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
310
311         }
312     }
313
314     @DELETE
315     @Path("/category/{componentType}/{categoryUniqueId}/subCategory/{subCategoryUniqueId}/grouping/{groupingUniqueId}")
316     @Consumes(MediaType.APPLICATION_JSON)
317     @Produces(MediaType.APPLICATION_JSON)
318     @ApiOperation(value = "Delete component category", httpMethod = "DELETE", notes = "Delete component category", response = Category.class)
319     @ApiResponses(value = { @ApiResponse(code = 204, message = "Category deleted"), @ApiResponse(code = 403, message = "USER_ID header is missing"), @ApiResponse(code = 409, message = "User not permitted to perform the action"),
320             @ApiResponse(code = 404, message = "Category not found"), @ApiResponse(code = 500, message = "General Error") })
321     public Response deleteComponentGrouping(@PathParam(value = "categoryUniqueId") final String grandParentCategoryUniqueId, @PathParam(value = "subCategoryUniqueId") final String parentSubCategoryUniqueId,
322             @PathParam(value = "groupingUniqueId") final String groupingUniqueId, @PathParam(value = "componentType") final String componentType, @Context final HttpServletRequest request,
323             @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
324
325         try {
326             Either<GroupingDefinition, ResponseFormat> deleteGrouping = elementBusinessLogic
327                 .deleteGrouping(groupingUniqueId, componentType, userId);
328             if (deleteGrouping.isRight()) {
329                 return buildErrorResponse(deleteGrouping.right().value());
330             }
331             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
332             return buildOkResponse(responseFormat, null);
333
334         } catch (Exception e) {
335             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete component grouping");
336             log.debug("deleteGrouping failed with exception", e);
337             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
338
339         }
340     }
341
342     /////////////////////////////////////////////////////////////////////////////////////////////////////
343     // retrieve all tags
344     @GET
345     @Path("/tags")
346     @Consumes(MediaType.APPLICATION_JSON)
347     @Produces(MediaType.APPLICATION_JSON)
348     @ApiOperation(value = "Retrieve all tags", httpMethod = "GET", notes = "Retrieve all tags", response = User.class)
349     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns tags Ok"), @ApiResponse(code = 404, message = "No tags were found"), @ApiResponse(code = 500, message = "Internal Server Error") })
350     public Response getTags(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
351         String url = request.getMethod() + " " + request.getRequestURI();
352         log.debug("(getTags) Start handle request of {}", url);
353
354         try {
355             Either<List<Tag>, ActionStatus> either = elementBusinessLogic.getAllTags(userId);
356             if (either.isRight() || either.left().value() == null) {
357                 log.debug("No tags were found");
358                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
359             } else {
360                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), either.left().value());
361             }
362         } catch (Exception e) {
363             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get All Tags");
364             log.debug("getAllTags failed with exception", e);
365             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
366         }
367     }
368
369     /////////////////////////////////////////////////////////////////////////////////////////////////////
370     // retrieve all property scopes
371     @GET
372     @Path("/propertyScopes")
373     @Consumes(MediaType.APPLICATION_JSON)
374     @Produces(MediaType.APPLICATION_JSON)
375     @ApiOperation(value = "Retrieve all propertyScopes", httpMethod = "GET", notes = "Retrieve all propertyScopes", response = User.class)
376     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns propertyScopes Ok"), @ApiResponse(code = 404, message = "No propertyScopes were found"), @ApiResponse(code = 500, message = "Internal Server Error") })
377     public Response getPropertyScopes(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
378         String url = request.getMethod() + " " + request.getRequestURI();
379         log.debug("(getPropertyScopes) Start handle request of {}", url);
380
381         try {
382             Either<List<PropertyScope>, ActionStatus> either = elementBusinessLogic.getAllPropertyScopes(userId);
383             if (either.isRight() || either.left().value() == null) {
384                 log.debug("No property scopes were found");
385                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
386             } else {
387                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), either.left().value());
388             }
389         } catch (Exception e) {
390             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Property Scopes Categories");
391             log.debug("getPropertyScopes failed with exception", e);
392             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
393         }
394     }
395
396     /////////////////////////////////////////////////////////////////////////////////////////////////////
397     // retrieve all artifact types
398     @GET
399     @Path("/artifactTypes")
400     @Consumes(MediaType.APPLICATION_JSON)
401     @Produces(MediaType.APPLICATION_JSON)
402     @ApiOperation(value = "Retrieve all artifactTypes", httpMethod = "GET", notes = "Retrieve all artifactTypes", response = User.class)
403     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns artifactTypes Ok"), @ApiResponse(code = 404, message = "No artifactTypes were found"), @ApiResponse(code = 500, message = "Internal Server Error") })
404     public Response getArtifactTypes(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
405         String url = request.getMethod() + " " + request.getRequestURI();
406         log.debug("(GET - getArtifactTypes) Start handle request of {}", url);
407
408         try {
409             Either<List<ArtifactType>, ActionStatus> either = elementBusinessLogic.getAllArtifactTypes(userId);
410             if (either.isRight() || either.left().value() == null) {
411                 log.debug("No artifact types were found");
412                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
413             } else {
414
415                 Integer defaultHeatTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultHeatArtifactTimeoutMinutes();
416                 ArtifactTypesInfo typesResponse = new ArtifactTypesInfo();
417                 typesResponse.setArtifactTypes(either.left().value());
418                 typesResponse.setHeatDefaultTimeout(defaultHeatTimeout);
419
420                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), typesResponse);
421             }
422         } catch (Exception e) {
423             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Artifact Types");
424             log.debug("getArtifactTypes failed with exception", e);
425             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
426         }
427     }
428
429     /////////////////////////////////////////////////////////////////////////////////////////////////////
430     // retrieve all artifact types
431     @GET
432     @Path("/configuration/ui")
433     @Consumes(MediaType.APPLICATION_JSON)
434     @Produces(MediaType.APPLICATION_JSON)
435     @ApiOperation(value = "Retrieve all artifactTypes", httpMethod = "GET", notes = "Retrieve all artifactTypes", response = User.class)
436     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns artifactTypes Ok"), @ApiResponse(code = 404, message = "No artifactTypes were found"), @ApiResponse(code = 500, message = "Internal Server Error") })
437     public Response getConfiguration(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
438         String url = request.getMethod() + " " + request.getRequestURI();
439         log.debug("(getConfiguration) Start handle request of {}", url);
440
441         try {
442             Either<List<ArtifactType>, ActionStatus> otherEither = elementBusinessLogic.getAllArtifactTypes(userId);
443             Either<Integer, ActionStatus> defaultHeatTimeout = elementBusinessLogic.getDefaultHeatTimeout();
444             Either<Map<String, Object>, ActionStatus> deploymentEither = elementBusinessLogic.getAllDeploymentArtifactTypes();
445             Either<Map<String, String>, ActionStatus> resourceTypesMap = elementBusinessLogic.getResourceTypesMap();
446
447             if (otherEither.isRight() || otherEither.left().value() == null) {
448                 log.debug("No other artifact types were found");
449                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
450             } else if (deploymentEither.isRight() || deploymentEither.left().value() == null) {
451                 log.debug("No deployment artifact types were found");
452                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
453             } else if (defaultHeatTimeout.isRight() || defaultHeatTimeout.left().value() == null) {
454                 log.debug("heat default timeout was not found");
455                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
456             } else if (resourceTypesMap.isRight() || resourceTypesMap.left().value() == null) {
457                 log.debug("No resource types were found");
458                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT));
459             } else {
460                 Map<String, Object> artifacts = new HashMap<>();
461                 Map<String, Object> configuration = new HashMap<>();
462
463                 artifacts.put("other", otherEither.left().value());
464                 artifacts.put("deployment", deploymentEither.left().value());
465                 configuration.put("artifacts", artifacts);
466                 configuration.put("defaultHeatTimeout", defaultHeatTimeout.left().value());
467                 configuration.put("componentTypes", elementBusinessLogic.getAllComponentTypesParamNames());
468                 configuration.put("roles", elementBusinessLogic.getAllSupportedRoles());
469                 configuration.put("resourceTypes", resourceTypesMap.left().value());
470                 configuration.put("environmentContext", ConfigurationManager.getConfigurationManager().getConfiguration().getEnvironmentContext());
471                 configuration.put("gab", ConfigurationManager.getConfigurationManager().getConfiguration().getGabConfig());
472
473                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), configuration);
474             }
475
476         } catch (Exception e) {
477             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Artifact Types");
478             log.debug("getArtifactTypes failed with exception", e);
479             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
480         }
481     }
482
483     /////////////////////////////////////////////////////////////////////////////////////////////////////
484     // retrieve all followed resources and services
485     @GET
486     @Path("/followed")
487     @Consumes(MediaType.APPLICATION_JSON)
488     @Produces(MediaType.APPLICATION_JSON)
489     @ApiOperation(value = "Retrieve all followed", httpMethod = "GET", notes = "Retrieve all followed", response = User.class)
490     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns followed Ok"), @ApiResponse(code = 404, message = "No followed were found"), @ApiResponse(code = 404, message = "User not found"),
491             @ApiResponse(code = 500, message = "Internal Server Error") })
492     public Response getFollowedResourcesServices(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
493
494         Response res = null;
495         User userData = null;
496         try {
497             String url = request.getMethod() + " " + request.getRequestURI();
498             log.debug("Start handle request of {}", url);
499
500             // Getting the user
501             Either<User, ActionStatus> either = userBusinessLogic.getUser(userId, false);
502             if (either.isRight()) {
503                 // Couldn't find or otherwise fetch the user
504                 return buildErrorResponse(getComponentsUtils().getResponseFormatByUserId(either.right().value(), userId));
505             }
506
507             if (either.left().value() != null) {
508                 userData = either.left().value();
509                 Either<Map<String, List<? extends Component>>, ResponseFormat> followedResourcesServices =
510                     elementBusinessLogic.getFollowed(userData);
511                 if (followedResourcesServices.isRight()) {
512                     log.debug("failed to get followed resources services ");
513                     return buildErrorResponse(followedResourcesServices.right().value());
514                 }
515                 Object data = RepresentationUtils.toRepresentation(followedResourcesServices.left().value());
516                 res = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), data);
517             } else {
518                 res = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
519             }
520         } catch (Exception e) {
521             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Followed Resources / Services Categories");
522             log.debug("Getting followed resources/services failed with exception", e);
523             res = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
524         }
525         return res;
526     }
527
528     /////////////////////////////////////////////////////////////////////////////////////////////////////
529     // retrieve all certified resources and services and their last version
530     @GET
531     @Path("/screen")
532     @Consumes(MediaType.APPLICATION_JSON)
533     @Produces(MediaType.APPLICATION_JSON)
534     @ApiOperation(value = "Retrieve catalog resources and services", httpMethod = "GET", notes = "Retrieve catalog resources and services", response = User.class)
535     @ApiResponses(value = { @ApiResponse(code = 200, message = "Returns resources and services Ok"), @ApiResponse(code = 404, message = "No resources and services were found"), @ApiResponse(code = 404, message = "User not found"),
536             @ApiResponse(code = 500, message = "Internal Server Error") })
537     public Response getCatalogComponents(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @QueryParam("excludeTypes") List<OriginTypeEnum> excludeTypes) {
538
539         Response res = null;
540         try {
541             String url = request.getMethod() + " " + request.getRequestURI();
542             log.debug("Start handle request of {}", url);
543
544                         Either<Map<String, List<CatalogComponent>>, ResponseFormat> catalogData = elementBusinessLogic
545           .getCatalogComponents(userId, excludeTypes);
546
547             if (catalogData.isRight()) {
548                 log.debug("failed to get catalog data");
549                 return buildErrorResponse(catalogData.right().value());
550             }
551             Object data = RepresentationUtils.toRepresentation(catalogData.left().value());
552             res = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), data);
553
554         } catch (Exception e) {
555             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Catalog Components");
556             log.debug("Getting catalog components failed with exception", e);
557             res = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
558         }
559         return res;
560     }
561
562     @DELETE
563     @Path("/inactiveComponents/{componentType}")
564     public Response deleteMarkedResources(@PathParam("componentType") final String componentType, @Context final HttpServletRequest request) {
565         String url = request.getMethod() + " " + request.getRequestURI();
566         log.debug("Start handle request of {}", url);
567
568         // get modifier id
569         String userId = request.getHeader(Constants.USER_ID_HEADER);
570         User modifier = new User();
571         modifier.setUserId(userId);
572         log.debug("modifier id is {}", userId);
573
574         Response response = null;
575
576         NodeTypeEnum nodeType = NodeTypeEnum.getByNameIgnoreCase(componentType);
577         if (nodeType == null) {
578             log.info("componentType is not valid: {}", componentType);
579             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
580         }
581
582         List<NodeTypeEnum> componentsList = new ArrayList<>();
583         componentsList.add(nodeType);
584         try {
585             Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponentsResult = componentsCleanBusinessLogic.cleanComponents(componentsList);
586             Either<List<String>, ResponseFormat> cleanResult = cleanComponentsResult.get(nodeType);
587
588             if (cleanResult.isRight()) {
589                 log.debug("failed to delete marked components of type {}", nodeType);
590                 response = buildErrorResponse(cleanResult.right().value());
591                 return response;
592             }
593             response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), cleanResult.left().value());
594             return response;
595
596         } catch (Exception e) {
597             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Marked Components");
598             log.debug("delete marked components failed with exception", e);
599             response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
600             return response;
601
602         }
603     }
604
605     @GET
606     @Path("/ecompPortalMenu")
607     @Consumes(MediaType.APPLICATION_JSON)
608     @Produces(MediaType.APPLICATION_JSON)
609     @ApiOperation(value = "Retrieve ecomp portal menu - MOC", httpMethod = "GET", notes = "Retrieve ecomp portal menu", response = User.class)
610     @ApiResponses(value = { @ApiResponse(code = 200, message = "Retrieve ecomp portal menu") })
611     public Response getListOfCsars(@Context final HttpServletRequest request) {
612         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
613                 "[{\"menuId\":1,\"column\":2,\"text\":\"Design\",\"parentMenuId\":null,\"url\":\"\",\"appid\":null,\"roles\":null,\"children\":[{\"menuId\":11,\"column\":1,\"text\":\"ProductDesign\",\"parentMenuId\":1,\"url\":\"\",\"appid\":null,\"roles\":null},{\"menuId\":12,\"column\":2,\"text\":\"Service\",\"parentMenuId\":1,\"url\":\"\",\"appid\":null,\"roles\":null,\"children\":[{\"menuId\":21,\"column\":1,\"text\":\"ViewPolicies\",\"parentMenuId\":12,\"url\":\"\",\"appid\":null,\"roles\":null,\"children\":[{\"menuId\":90,\"column\":1,\"text\":\"4thLevelApp1aR16\",\"parentMenuId\":21,\"url\":\"http://google.com\",\"appid\":null,\"roles\":null}]},{\"menuId\":22,\"column\":2,\"text\":\"UpdatePolicies\",\"parentMenuId\":12,\"url\":\"\",\"appid\":null,\"roles\":null,\"children\":[{\"menuId\":91,\"column\":1,\"text\":\"4thLevelApp1bR16\",\"parentMenuId\":22,\"url\":\"http://jsonlint.com/\",\"appid\":null,\"roles\":null}]},{\"menuId\":23,\"column\":3,\"text\":\"UpdateRules\",\"parentMenuId\":12,\"url\":\"\",\"appid\":null,\"roles\":null},{\"menuId\":24,\"column\":4,\"text\":\"CreateSignatures?\",\"parentMenuId\":12,\"url\":\"\",\"appid\":null,\"roles\":null},{\"menuId\":25,\"column\":5,\"text\":\"Definedata\",\"parentMenuId\":12,\"url\":\"\",\"appid\":null,\"roles\":null}]}]}]");
614     }
615
616 }