2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.google.gson.Gson;
24 import com.jcabi.aspects.Loggable;
25 import fj.data.Either;
26 import io.swagger.annotations.*;
27 import javax.inject.Inject;
28 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.ConsumerBusinessLogic;
30 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.ConsumerDefinition;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
37 import org.openecomp.sdc.be.user.UserBusinessLogic;
38 import org.openecomp.sdc.common.api.Constants;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import org.springframework.beans.factory.annotation.Autowired;
43 import javax.inject.Singleton;
44 import javax.servlet.ServletContext;
45 import javax.servlet.http.HttpServletRequest;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
51 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
52 @Path("/v1/consumers")
53 @Api(value = "Consumer Servlet", description = "Consumer Servlet")
55 public class ConsumerServlet extends BeGenericServlet {
57 private static final String MODIFIER_ID_IS = "modifier id is {}";
58 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
59 private static final Logger log = Logger.getLogger(ConsumerServlet.class);
60 private final ConsumerBusinessLogic businessLogic;
63 public ConsumerServlet(UserBusinessLogic userBusinessLogic,
64 ComponentsUtils componentsUtils,
65 ConsumerBusinessLogic businessLogic) {
66 super(userBusinessLogic, componentsUtils);
67 this.businessLogic = businessLogic;
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
73 @ApiOperation(value = "Consumer credentials", httpMethod = "POST", notes = "Returns created ECOMP consumer credentials", response = Response.class)
74 @ApiResponses(value = { @ApiResponse(code = 201, message = "Consumer credentials created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
75 public Response createConsumer(@ApiParam(value = "Consumer Object to be created", required = true) String data, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
77 ServletContext context = request.getSession().getServletContext();
79 String url = request.getMethod() + " " + request.getRequestURI();
80 log.debug(START_HANDLE_REQUEST_OF, url);
82 User modifier = new User();
83 modifier.setUserId(userId);
84 log.debug(MODIFIER_ID_IS, userId);
87 Either<ConsumerDefinition, ResponseFormat> convertionResponse = convertJsonToObject(data, modifier, AuditingActionEnum.ADD_ECOMP_USER_CREDENTIALS);
89 if (convertionResponse.isRight()) {
90 log.debug("failed to create Consumer");
91 return buildErrorResponse(convertionResponse.right().value());
94 ConsumerDefinition consumer = convertionResponse.left().value();
96 Either<ConsumerDefinition, ResponseFormat> actionResult = businessLogic.createConsumer(modifier, consumer);
98 if (actionResult.isRight()) {
99 log.debug("failed to create Consumer");
100 return buildErrorResponse(actionResult.right().value());
103 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), actionResult.left().value());
105 } catch (Exception e) {
106 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create consumer");
107 log.debug("create consumer failed with exception", e);
108 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
109 return buildErrorResponse(responseFormat);
115 @Path("/{consumerId}")
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 @ApiOperation(value = "Retrieve Consumer", httpMethod = "GET", notes = "Returns consumer according to ConsumerID", response = ConsumerDefinition.class)
119 @ApiResponses(value = { @ApiResponse(code = 200, message = "Consumer found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Consumer not found") })
120 public Response getConsumer(@PathParam("consumerId") final String consumerId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
122 ServletContext context = request.getSession().getServletContext();
124 String url = request.getMethod() + " " + request.getRequestURI();
125 log.debug(START_HANDLE_REQUEST_OF, url);
127 User modifier = new User();
128 modifier.setUserId(userId);
129 log.debug(MODIFIER_ID_IS, userId);
131 Response response = null;
133 Either<ConsumerDefinition, ResponseFormat> actionResponse = businessLogic.getConsumer(consumerId, modifier);
135 if (actionResponse.isRight()) {
136 log.debug("failed to get consumer");
137 response = buildErrorResponse(actionResponse.right().value());
140 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
142 } catch (Exception e) {
143 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Consumer");
144 log.debug("get consumer failed with exception", e);
145 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
151 @Path("/{consumerId}")
152 @Consumes(MediaType.APPLICATION_JSON)
153 @Produces(MediaType.APPLICATION_JSON)
154 @ApiOperation(value = "Deletes Consumer", httpMethod = "DELETE", notes = "Returns deleted consumer according to ConsumerID", response = ConsumerDefinition.class)
155 @ApiResponses(value = { @ApiResponse(code = 204, message = "Consumer deleted"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Consumer not found") })
156 public Response deleteConsumer(@PathParam("consumerId") final String consumerId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
158 ServletContext context = request.getSession().getServletContext();
160 String url = request.getMethod() + " " + request.getRequestURI();
161 log.debug(START_HANDLE_REQUEST_OF, url);
163 User modifier = new User();
164 modifier.setUserId(userId);
165 log.debug(MODIFIER_ID_IS, userId);
167 Response response = null;
169 Either<ConsumerDefinition, ResponseFormat> actionResponse = businessLogic.deleteConsumer(consumerId, modifier);
171 if (actionResponse.isRight()) {
172 log.debug("failed to delete consumer");
173 response = buildErrorResponse(actionResponse.right().value());
176 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
178 } catch (Exception e) {
179 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Consumer");
180 log.debug("delete consumer failed with exception", e);
181 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
186 public Either<ConsumerDefinition, ResponseFormat> convertJsonToObject(String data, User user, AuditingActionEnum actionEnum) {
187 ConsumerDefinition consumer;
188 Gson gson = new Gson();
190 log.trace("convert json to object. json=\n {}", data);
191 consumer = gson.fromJson(data, ConsumerDefinition.class);
192 if (consumer == null) {
193 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
194 log.debug("object is null after converting from json");
195 ResponseFormat responseFormat = getComponentsUtils().getInvalidContentErrorForConsumerAndAudit(user, null, actionEnum);
196 return Either.right(responseFormat);
198 } catch (Exception e) {
200 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
201 log.debug("failed to convert from json {}", data, e);
202 ResponseFormat responseFormat = getComponentsUtils().getInvalidContentErrorForConsumerAndAudit(user, null, actionEnum);
203 return Either.right(responseFormat);
205 return Either.left(consumer);