1 package org.openecomp.sdc.be.servlets;
4 import com.google.common.collect.Sets;
5 import com.jcabi.aspects.Loggable;
7 import io.swagger.annotations.*;
8 import org.apache.commons.collections.MapUtils;
9 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
10 import org.openecomp.sdc.be.config.BeEcompErrorManager;
11 import org.openecomp.sdc.be.dao.api.ActionStatus;
12 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
13 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
14 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
15 import org.openecomp.sdc.be.model.Service;
16 import org.openecomp.sdc.be.model.User;
17 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
18 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
19 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
20 import org.openecomp.sdc.common.api.Constants;
21 import org.openecomp.sdc.common.log.wrappers.Logger;
22 import org.openecomp.sdc.exception.ResponseFormat;
24 import javax.inject.Singleton;
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.Context;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import java.util.Collections;
34 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
35 @Path("/v1/catalog/services/{serviceId}/paths")
36 @Consumes(MediaType.APPLICATION_JSON)
37 @Produces(MediaType.APPLICATION_JSON)
38 @Api(value = "Service Forwarding Path", description = "Service Forwarding Path Servlet")
40 public class ServiceForwardingPathServlet extends AbstractValidationsServlet {
42 private static final Logger log = Logger.getLogger(ServiceForwardingPathServlet.class);
45 @Consumes(MediaType.APPLICATION_JSON)
46 @Produces(MediaType.APPLICATION_JSON)
48 @ApiOperation(value = "Create Forwarding Path", httpMethod = "POST", notes = "Create Forwarding Path", response = Service.class)
50 {@ApiResponse(code = 201, message = "Create Forwarding Path"),
51 @ApiResponse(code = 403, message = "Restricted operation"),
52 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
53 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
54 public Response createForwardingPath(
55 @ApiParam(value = "Forwarding Path to create", required = true) String data,
56 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
57 @Context final HttpServletRequest request,
58 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
59 return createOrUpdate(data, serviceId, request, userId, false);
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
68 @ApiOperation(value = "Update Forwarding Path", httpMethod = "PUT", notes = "Update Forwarding Path", response = Service.class)
70 {@ApiResponse(code = 201, message = "Update Forwarding Path"),
71 @ApiResponse(code = 403, message = "Restricted operation"),
72 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
73 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
74 public Response updateForwardingPath(
75 @ApiParam(value = "Update Path to create", required = true) String data,
76 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
77 @Context final HttpServletRequest request,
78 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
79 return createOrUpdate(data, serviceId, request, userId, true);
82 private Response createOrUpdate( String data, String serviceId, HttpServletRequest request, String userId, boolean isUpdate) {
83 ServletContext context = request.getSession().getServletContext();
84 String url = request.getMethod() + " " + request.getRequestURI();
85 log.debug("Start handle request of {}", url);
87 User modifier = new User();
88 modifier.setUserId(userId);
89 log.debug("modifier id is {}", userId);
94 String serviceIdLower = serviceId.toLowerCase();
95 ServiceBusinessLogic businessLogic = getServiceBL(context);
97 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
98 if (convertResponse.isRight()) {
99 log.debug("failed to parse service");
100 response = buildErrorResponse(convertResponse.right().value());
103 Service updatedService = convertResponse.left().value();
104 Either<Service, ResponseFormat> actionResponse ;
106 actionResponse = businessLogic.updateForwardingPath(serviceIdLower, updatedService, modifier, true);
108 actionResponse = businessLogic.createForwardingPath(serviceIdLower, updatedService, modifier, true);
111 if (actionResponse.isRight()) {
112 log.debug("failed to update or create paths");
113 response = buildErrorResponse(actionResponse.right().value());
117 Service service = actionResponse.left().value();
118 Object result = RepresentationUtils.toRepresentation(service);
119 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
121 } catch (Exception e) {
122 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Forward Path Creation or update");
123 log.debug("create or update forwarding path with an error", e);
124 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
131 @Consumes(MediaType.APPLICATION_JSON)
132 @Produces(MediaType.APPLICATION_JSON)
133 @Path("/{forwardingPathId}")
134 @ApiOperation(value = "Get Forwarding Path", httpMethod = "GET", notes = "GET Forwarding Path", response = ForwardingPathDataDefinition.class)
135 @ApiResponses(value =
136 {@ApiResponse(code = 201, message = "Get Forwarding Path"),
137 @ApiResponse(code = 403, message = "Restricted operation"),
138 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
139 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
140 public Response getForwardingPath(
141 @ApiParam(value = "Forwarding Path to create", required = true) String datax,
142 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
143 @ApiParam(value = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
144 @Context final HttpServletRequest request,
145 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
146 ServletContext context = request.getSession().getServletContext();
147 String url = request.getMethod() + " " + request.getRequestURI();
148 log.debug("Start handle request of {}", url);
150 User modifier = new User();
151 modifier.setUserId(userId);
152 log.debug("modifier id is {}", userId);
156 ServiceBusinessLogic businessLogic = getServiceBL(context);
157 Either<UiComponentDataTransfer, ResponseFormat> serviceResponse = businessLogic.getComponentDataFilteredByParams(serviceId, modifier, Collections.singletonList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
158 if (serviceResponse.isRight()) {
159 return buildErrorResponse(serviceResponse.right().value());
162 UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) serviceResponse.left().value();
164 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition();
165 if (!MapUtils.isEmpty(uiServiceDataTransfer.getForwardingPaths())) {
166 forwardingPathDataDefinition = uiServiceDataTransfer.getForwardingPaths().get(forwardingPathId);
168 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), RepresentationUtils.toRepresentation(forwardingPathDataDefinition));
171 } catch (Exception e) {
172 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Metadata");
173 log.debug("update service metadata failed with exception", e);
174 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
179 @Consumes(MediaType.APPLICATION_JSON)
180 @Produces(MediaType.APPLICATION_JSON)
181 @Path("/{forwardingPathId}")
182 @ApiOperation(value = "Delete Forwarding Path", httpMethod = "DELETE", notes = "Delete Forwarding Path", response = Service.class)
183 @ApiResponses(value =
184 {@ApiResponse(code = 201, message = "Delete Forwarding Path"),
185 @ApiResponse(code = 403, message = "Restricted operation"),
186 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
187 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
188 public Response deleteForwardingPath(
189 @ApiParam(value = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
190 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
191 @Context final HttpServletRequest request,
192 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
193 ServletContext context = request.getSession().getServletContext();
194 String url = request.getMethod() + " " + request.getRequestURI();
195 log.debug("Start handle request of {}", url);
197 User modifier = new User();
198 modifier.setUserId(userId);
199 log.debug("modifier id is {}", userId);
204 String serviceIdLower = serviceId.toLowerCase();
205 ServiceBusinessLogic businessLogic = getServiceBL(context);
207 Either<Set<String>, ResponseFormat> actionResponse = businessLogic.deleteForwardingPaths(serviceIdLower, Sets.newHashSet(forwardingPathId), modifier, true);
209 if (actionResponse.isRight()) {
210 log.debug("failed to delete paths");
211 response = buildErrorResponse(actionResponse.right().value());
215 Set<String> deletedPaths = actionResponse.left().value();
216 Object result = RepresentationUtils.toRepresentation(deletedPaths);
217 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
219 } catch (Exception e) {
220 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete forward paths");
221 log.debug("Delete service paths with an error", e);
222 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
229 private Either<Service, ResponseFormat> parseToService(String serviceJson, User user) {
230 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, Service.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE);//TODO: change sSERVICE constant