2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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;
24 import com.google.common.collect.Sets;
25 import com.jcabi.aspects.Loggable;
26 import fj.data.Either;
27 import io.swagger.annotations.*;
28 import javax.inject.Inject;
29 import org.apache.commons.collections.MapUtils;
30 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
31 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
33 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.impl.ServletUtils;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
44 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
45 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
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;
51 import javax.inject.Singleton;
52 import javax.servlet.ServletContext;
53 import javax.servlet.http.HttpServletRequest;
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.Collections;
61 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
62 @Path("/v1/catalog/services/{serviceId}/paths")
63 @Consumes(MediaType.APPLICATION_JSON)
64 @Produces(MediaType.APPLICATION_JSON)
65 @Api(value = "Service Forwarding Path", description = "Service Forwarding Path Servlet")
67 public class ServiceForwardingPathServlet extends AbstractValidationsServlet {
69 private static final Logger log = Logger.getLogger(ServiceForwardingPathServlet.class);
70 private final ServiceBusinessLogic serviceBusinessLogic;
73 public ServiceForwardingPathServlet(UserBusinessLogic userBusinessLogic,
74 ComponentInstanceBusinessLogic componentInstanceBL,
75 ComponentsUtils componentsUtils, ServletUtils servletUtils,
76 ResourceImportManager resourceImportManager,
77 ServiceBusinessLogic serviceBusinessLogic) {
78 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
79 this.serviceBusinessLogic = serviceBusinessLogic;
83 @Consumes(MediaType.APPLICATION_JSON)
84 @Produces(MediaType.APPLICATION_JSON)
86 @ApiOperation(value = "Create Forwarding Path", httpMethod = "POST", notes = "Create Forwarding Path", response = Service.class)
88 {@ApiResponse(code = 201, message = "Create Forwarding Path"),
89 @ApiResponse(code = 403, message = "Restricted operation"),
90 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
91 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
92 public Response createForwardingPath(
93 @ApiParam(value = "Forwarding Path to create", required = true) String data,
94 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
95 @Context final HttpServletRequest request,
96 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
97 return createOrUpdate(data, serviceId, request, userId, false);
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
106 @ApiOperation(value = "Update Forwarding Path", httpMethod = "PUT", notes = "Update Forwarding Path", response = Service.class)
107 @ApiResponses(value =
108 {@ApiResponse(code = 201, message = "Update Forwarding Path"),
109 @ApiResponse(code = 403, message = "Restricted operation"),
110 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
111 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
112 public Response updateForwardingPath(
113 @ApiParam(value = "Update Path to create", required = true) String data,
114 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
115 @Context final HttpServletRequest request,
116 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
117 return createOrUpdate(data, serviceId, request, userId, true);
120 private Response createOrUpdate( String data, String serviceId, HttpServletRequest request, String userId, boolean isUpdate) {
121 String url = request.getMethod() + " " + request.getRequestURI();
122 log.debug("Start handle request of {}", url);
124 User modifier = new User();
125 modifier.setUserId(userId);
126 log.debug("modifier id is {}", userId);
131 String serviceIdLower = serviceId.toLowerCase();
133 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
134 if (convertResponse.isRight()) {
135 log.debug("failed to parse service");
136 response = buildErrorResponse(convertResponse.right().value());
139 Service updatedService = convertResponse.left().value();
140 Either<Service, ResponseFormat> actionResponse ;
142 actionResponse = serviceBusinessLogic.updateForwardingPath(serviceIdLower, updatedService, modifier, true);
144 actionResponse = serviceBusinessLogic.createForwardingPath(serviceIdLower, updatedService, modifier, true);
147 if (actionResponse.isRight()) {
148 log.debug("failed to update or create paths");
149 response = buildErrorResponse(actionResponse.right().value());
153 Service service = actionResponse.left().value();
154 Object result = RepresentationUtils.toRepresentation(service);
155 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
157 } catch (Exception e) {
158 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Forward Path Creation or update");
159 log.debug("create or update forwarding path with an error", e);
160 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
167 @Consumes(MediaType.APPLICATION_JSON)
168 @Produces(MediaType.APPLICATION_JSON)
169 @Path("/{forwardingPathId}")
170 @ApiOperation(value = "Get Forwarding Path", httpMethod = "GET", notes = "GET Forwarding Path", response = ForwardingPathDataDefinition.class)
171 @ApiResponses(value =
172 {@ApiResponse(code = 201, message = "Get Forwarding Path"),
173 @ApiResponse(code = 403, message = "Restricted operation"),
174 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
175 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
176 public Response getForwardingPath(
177 @ApiParam(value = "Forwarding Path to create", required = true) String datax,
178 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
179 @ApiParam(value = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
180 @Context final HttpServletRequest request,
181 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
182 String url = request.getMethod() + " " + request.getRequestURI();
183 log.debug("Start handle request of {}", url);
185 User modifier = new User();
186 modifier.setUserId(userId);
187 log.debug("modifier id is {}", userId);
191 Either<UiComponentDataTransfer, ResponseFormat> serviceResponse = serviceBusinessLogic.getComponentDataFilteredByParams(serviceId, modifier, Collections.singletonList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
192 if (serviceResponse.isRight()) {
193 return buildErrorResponse(serviceResponse.right().value());
196 UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) serviceResponse.left().value();
198 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition();
199 if (!MapUtils.isEmpty(uiServiceDataTransfer.getForwardingPaths())) {
200 forwardingPathDataDefinition = uiServiceDataTransfer.getForwardingPaths().get(forwardingPathId);
202 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), RepresentationUtils.toRepresentation(forwardingPathDataDefinition));
205 } catch (Exception e) {
206 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Metadata");
207 log.debug("update service metadata failed with exception", e);
208 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
213 @Consumes(MediaType.APPLICATION_JSON)
214 @Produces(MediaType.APPLICATION_JSON)
215 @Path("/{forwardingPathId}")
216 @ApiOperation(value = "Delete Forwarding Path", httpMethod = "DELETE", notes = "Delete Forwarding Path", response = Service.class)
217 @ApiResponses(value =
218 {@ApiResponse(code = 201, message = "Delete Forwarding Path"),
219 @ApiResponse(code = 403, message = "Restricted operation"),
220 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
221 @ApiResponse(code = 409, message = "Forwarding Path already exist")})
222 public Response deleteForwardingPath(
223 @ApiParam(value = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
224 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
225 @Context final HttpServletRequest request,
226 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
227 String url = request.getMethod() + " " + request.getRequestURI();
228 log.debug("Start handle request of {}", url);
230 User modifier = new User();
231 modifier.setUserId(userId);
232 log.debug("modifier id is {}", userId);
237 String serviceIdLower = serviceId.toLowerCase();
239 Either<Set<String>, ResponseFormat> actionResponse = serviceBusinessLogic.deleteForwardingPaths(serviceIdLower, Sets.newHashSet(forwardingPathId), modifier, true);
241 if (actionResponse.isRight()) {
242 log.debug("failed to delete paths");
243 response = buildErrorResponse(actionResponse.right().value());
247 Set<String> deletedPaths = actionResponse.left().value();
248 Object result = RepresentationUtils.toRepresentation(deletedPaths);
249 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
251 } catch (Exception e) {
252 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete forward paths");
253 log.debug("Delete service paths with an error", e);
254 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
261 private Either<Service, ResponseFormat> parseToService(String serviceJson, User user) {
262 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, Service.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.SERVICE);//TODO: change sSERVICE constant