Final commit to master merge from
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / TypesFetchServlet.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 java.util.Map;
24
25 import javax.inject.Singleton;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.HeaderParam;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36
37 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
38 import org.openecomp.sdc.be.config.BeEcompErrorManager;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.model.DataTypeDefinition;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.common.api.Constants;
44 import org.openecomp.sdc.common.config.EcompErrorName;
45 import org.openecomp.sdc.common.datastructure.Wrapper;
46 import org.openecomp.sdc.exception.ResponseFormat;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.web.context.WebApplicationContext;
50
51 import com.jcabi.aspects.Loggable;
52
53 import fj.data.Either;
54 import io.swagger.annotations.Api;
55 import io.swagger.annotations.ApiOperation;
56 import io.swagger.annotations.ApiResponse;
57 import io.swagger.annotations.ApiResponses;
58
59 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
60 @Path("/v1/catalog")
61 @Api(value = "Types Fetch Servlet", description = "Types Fetch Servlet")
62 @Singleton
63 public class TypesFetchServlet extends AbstractValidationsServlet {
64
65         private static Logger log = LoggerFactory.getLogger(TypesFetchServlet.class.getName());
66
67         @GET
68         @Path("dataTypes")
69         @Consumes(MediaType.APPLICATION_JSON)
70         @Produces(MediaType.APPLICATION_JSON)
71         @ApiOperation(value = "Get data types", httpMethod = "GET", notes = "Returns data types", response = Response.class)
72         @ApiResponses(value = { @ApiResponse(code = 200, message = "datatypes"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
73                         @ApiResponse(code = 404, message = "Data types not found") })
74         public Response getAllDataTypesServlet(@Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
75
76                 Wrapper<Response> responseWrapper = new Wrapper<Response>();
77                 Wrapper<User> userWrapper = new Wrapper<User>();
78                 ServletContext context = request.getSession().getServletContext();
79
80                 try {
81                         init(log);
82                         validateUserExist(responseWrapper, userWrapper, userId);
83
84                         if (responseWrapper.isEmpty()) {
85                                 String url = request.getMethod() + " " + request.getRequestURI();
86                                 log.debug("Start handle request of {} | modifier id is {}", url, userId);
87
88                                 PropertyBusinessLogic businessLogic = getPropertyBL(context);
89                                 Either<Map<String, DataTypeDefinition>, ResponseFormat> allDataTypes = businessLogic.getAllDataTypes();
90
91                                 if (allDataTypes.isRight()) {
92                                         log.info("Failed to get all dara types. Reason - {}", allDataTypes.right().value());
93                                         Response errorResponse = buildErrorResponse(allDataTypes.right().value());
94                                         responseWrapper.setInnerElement(errorResponse);
95
96                                         // return buildErrorResponse(allDataTypes.right().value());
97                                 } else {
98                                         
99                                         Map<String, DataTypeDefinition> dataTypes = allDataTypes.left().value();
100                                         String dataTypeJson = gson.toJson(dataTypes);                                   
101                                         Response okResponse = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataTypeJson);                                   
102                                         responseWrapper.setInnerElement(okResponse);
103                                         
104                                 }
105                         }
106
107                         return responseWrapper.getInnerElement();
108                 } catch (Exception e) {
109                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "Get all data types");
110                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Property");
111                         log.debug("get all data types failed with exception", e);
112                         ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
113                         return buildErrorResponse(responseFormat);
114                 }
115         }
116
117         private PropertyBusinessLogic getPropertyBL(ServletContext context) {
118                 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
119                 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
120                 PropertyBusinessLogic propertytBl = webApplicationContext.getBean(PropertyBusinessLogic.class);
121                 return propertytBl;
122         }
123
124 }