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=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import com.google.common.collect.Sets;
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.servers.Servers;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import io.swagger.v3.oas.annotations.tags.Tags;
35 import java.io.IOException;
36 import java.util.Collections;
38 import javax.inject.Inject;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.Consumes;
41 import javax.ws.rs.DELETE;
42 import javax.ws.rs.GET;
43 import javax.ws.rs.HeaderParam;
44 import javax.ws.rs.POST;
45 import javax.ws.rs.PUT;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.MediaType;
51 import javax.ws.rs.core.Response;
52 import org.apache.commons.collections.MapUtils;
53 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
55 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
56 import org.openecomp.sdc.be.config.BeEcompErrorManager;
57 import org.openecomp.sdc.be.dao.api.ActionStatus;
58 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
59 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
60 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
61 import org.openecomp.sdc.be.impl.ComponentsUtils;
62 import org.openecomp.sdc.be.impl.ServletUtils;
63 import org.openecomp.sdc.be.model.Service;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
66 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
67 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
68 import org.openecomp.sdc.common.api.Constants;
69 import org.openecomp.sdc.common.log.wrappers.Logger;
70 import org.openecomp.sdc.exception.ResponseFormat;
71 import org.springframework.stereotype.Controller;
73 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
74 @Path("/v1/catalog/services/{serviceId}/paths")
75 @Consumes(MediaType.APPLICATION_JSON)
76 @Produces(MediaType.APPLICATION_JSON)
77 @Tags({@Tag(name = "SDCE-2 APIs")})
78 @Servers({@Server(url = "/sdc2/rest")})
80 public class ServiceForwardingPathServlet extends AbstractValidationsServlet {
82 private static final Logger log = Logger.getLogger(ServiceForwardingPathServlet.class);
83 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
84 private static final String MODIFIER_ID_IS = "modifier id is {}";
85 private final ServiceBusinessLogic serviceBusinessLogic;
88 public ServiceForwardingPathServlet(ComponentInstanceBusinessLogic componentInstanceBL,
89 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
90 ServiceBusinessLogic serviceBusinessLogic) {
91 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
92 this.serviceBusinessLogic = serviceBusinessLogic;
96 @Consumes(MediaType.APPLICATION_JSON)
97 @Produces(MediaType.APPLICATION_JSON)
99 @Operation(description = "Create Forwarding Path", method = "POST", summary = "Create Forwarding Path", responses = {
100 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
101 @ApiResponse(responseCode = "201", description = "Create Forwarding Path"),
102 @ApiResponse(responseCode = "403", description = "Restricted operation"),
103 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
104 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
105 public Response createForwardingPath(@Parameter(description = "Forwarding Path to create", required = true) String data,
106 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
107 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
109 return createOrUpdate(data, serviceId, request, userId, false);
113 @Consumes(MediaType.APPLICATION_JSON)
114 @Produces(MediaType.APPLICATION_JSON)
116 @Operation(description = "Update Forwarding Path", method = "PUT", summary = "Update Forwarding Path", responses = {
117 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
118 @ApiResponse(responseCode = "201", description = "Update Forwarding Path"),
119 @ApiResponse(responseCode = "403", description = "Restricted operation"),
120 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
121 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
122 public Response updateForwardingPath(@Parameter(description = "Update Path to create", required = true) String data,
123 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
124 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
126 return createOrUpdate(data, serviceId, request, userId, true);
129 private Response createOrUpdate(String data, String serviceId, HttpServletRequest request, String userId, boolean isUpdate) throws IOException {
130 String url = request.getMethod() + " " + request.getRequestURI();
131 log.debug(START_HANDLE_REQUEST_OF, url);
132 User modifier = new User();
133 modifier.setUserId(userId);
134 log.debug(MODIFIER_ID_IS, userId);
137 String serviceIdLower = serviceId.toLowerCase();
138 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
139 if (convertResponse.isRight()) {
140 log.debug("failed to parse service");
141 response = buildErrorResponse(convertResponse.right().value());
144 Service updatedService = convertResponse.left().value();
145 Service actionResponse;
147 actionResponse = serviceBusinessLogic.updateForwardingPath(serviceIdLower, updatedService, modifier, true);
149 actionResponse = serviceBusinessLogic.createForwardingPath(serviceIdLower, updatedService, modifier, true);
151 Service service = actionResponse;
152 Object result = RepresentationUtils.toRepresentation(service);
153 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
154 } catch (IOException e) {
155 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Forward Path Creation or update");
156 log.debug("create or update forwarding path with an error", e);
162 @Consumes(MediaType.APPLICATION_JSON)
163 @Produces(MediaType.APPLICATION_JSON)
164 @Path("/{forwardingPathId}")
165 @Operation(description = "Get Forwarding Path", method = "GET", summary = "GET Forwarding Path", responses = {
166 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = ForwardingPathDataDefinition.class)))),
167 @ApiResponse(responseCode = "201", description = "Get Forwarding Path"),
168 @ApiResponse(responseCode = "403", description = "Restricted operation"),
169 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
170 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
171 public Response getForwardingPath(@Parameter(description = "Forwarding Path to create", required = true) String datax,
172 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
173 @Parameter(description = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
174 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
176 String url = request.getMethod() + " " + request.getRequestURI();
177 log.debug(START_HANDLE_REQUEST_OF, url);
178 User modifier = new User();
179 modifier.setUserId(userId);
180 log.debug(MODIFIER_ID_IS, userId);
182 Either<UiComponentDataTransfer, ResponseFormat> serviceResponse = serviceBusinessLogic
183 .getComponentDataFilteredByParams(serviceId, modifier, Collections.singletonList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
184 if (serviceResponse.isRight()) {
185 return buildErrorResponse(serviceResponse.right().value());
187 UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) serviceResponse.left().value();
188 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition();
189 if (!MapUtils.isEmpty(uiServiceDataTransfer.getForwardingPaths())) {
190 forwardingPathDataDefinition = uiServiceDataTransfer.getForwardingPaths().get(forwardingPathId);
192 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
193 RepresentationUtils.toRepresentation(forwardingPathDataDefinition));
194 } catch (Exception e) {
195 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Metadata");
196 log.debug("update service metadata failed with exception", e);
202 @Consumes(MediaType.APPLICATION_JSON)
203 @Produces(MediaType.APPLICATION_JSON)
204 @Path("/{forwardingPathId}")
205 @Operation(description = "Delete Forwarding Path", method = "DELETE", summary = "Delete Forwarding Path", responses = {
206 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
207 @ApiResponse(responseCode = "201", description = "Delete Forwarding Path"),
208 @ApiResponse(responseCode = "403", description = "Restricted operation"),
209 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
210 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
211 public Response deleteForwardingPath(@Parameter(description = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
212 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
213 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
215 String url = request.getMethod() + " " + request.getRequestURI();
216 log.debug(START_HANDLE_REQUEST_OF, url);
217 User modifier = new User();
218 modifier.setUserId(userId);
219 log.debug(MODIFIER_ID_IS, userId);
222 String serviceIdLower = serviceId.toLowerCase();
223 Set<String> deletedPaths = serviceBusinessLogic.deleteForwardingPaths(serviceIdLower, Sets.newHashSet(forwardingPathId), modifier, true);
224 Object result = RepresentationUtils.toRepresentation(deletedPaths);
225 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
226 } catch (IOException e) {
227 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete forward paths");
228 log.debug("Delete service paths with an error", e);
233 private Either<Service, ResponseFormat> parseToService(String serviceJson, User user) {
234 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, Service.class, AuditingActionEnum.CREATE_RESOURCE,
235 ComponentTypeEnum.SERVICE);//TODO: change sSERVICE constant