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.components.impl.aaf.AafPermission;
57 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
58 import org.openecomp.sdc.be.config.BeEcompErrorManager;
59 import org.openecomp.sdc.be.dao.api.ActionStatus;
60 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
61 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.impl.ComponentsUtils;
64 import org.openecomp.sdc.be.impl.ServletUtils;
65 import org.openecomp.sdc.be.model.Service;
66 import org.openecomp.sdc.be.model.User;
67 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
68 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
69 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
70 import org.openecomp.sdc.be.user.UserBusinessLogic;
71 import org.openecomp.sdc.common.api.Constants;
72 import org.openecomp.sdc.common.log.wrappers.Logger;
73 import org.openecomp.sdc.exception.ResponseFormat;
74 import org.springframework.stereotype.Controller;
76 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
77 @Path("/v1/catalog/services/{serviceId}/paths")
78 @Consumes(MediaType.APPLICATION_JSON)
79 @Produces(MediaType.APPLICATION_JSON)
80 @Tags({@Tag(name = "SDCE-2 APIs")})
81 @Servers({@Server(url = "/sdc2/rest")})
83 public class ServiceForwardingPathServlet extends AbstractValidationsServlet {
85 private static final Logger log = Logger.getLogger(ServiceForwardingPathServlet.class);
86 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
87 private static final String MODIFIER_ID_IS = "modifier id is {}";
88 private final ServiceBusinessLogic serviceBusinessLogic;
91 public ServiceForwardingPathServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL,
92 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
93 ServiceBusinessLogic serviceBusinessLogic) {
94 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
95 this.serviceBusinessLogic = serviceBusinessLogic;
99 @Consumes(MediaType.APPLICATION_JSON)
100 @Produces(MediaType.APPLICATION_JSON)
102 @Operation(description = "Create Forwarding Path", method = "POST", summary = "Create Forwarding Path", responses = {
103 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
104 @ApiResponse(responseCode = "201", description = "Create Forwarding Path"),
105 @ApiResponse(responseCode = "403", description = "Restricted operation"),
106 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
107 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
108 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
109 public Response createForwardingPath(@Parameter(description = "Forwarding Path to create", required = true) String data,
110 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
111 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
113 return createOrUpdate(data, serviceId, request, userId, false);
117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
120 @Operation(description = "Update Forwarding Path", method = "PUT", summary = "Update Forwarding Path", responses = {
121 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
122 @ApiResponse(responseCode = "201", description = "Update Forwarding Path"),
123 @ApiResponse(responseCode = "403", description = "Restricted operation"),
124 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
125 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
126 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
127 public Response updateForwardingPath(@Parameter(description = "Update Path to create", required = true) String data,
128 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
129 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
131 return createOrUpdate(data, serviceId, request, userId, true);
134 private Response createOrUpdate(String data, String serviceId, HttpServletRequest request, String userId, boolean isUpdate) throws IOException {
135 String url = request.getMethod() + " " + request.getRequestURI();
136 log.debug(START_HANDLE_REQUEST_OF, url);
137 User modifier = new User();
138 modifier.setUserId(userId);
139 log.debug(MODIFIER_ID_IS, userId);
142 String serviceIdLower = serviceId.toLowerCase();
143 Either<Service, ResponseFormat> convertResponse = parseToService(data, modifier);
144 if (convertResponse.isRight()) {
145 log.debug("failed to parse service");
146 response = buildErrorResponse(convertResponse.right().value());
149 Service updatedService = convertResponse.left().value();
150 Service actionResponse;
152 actionResponse = serviceBusinessLogic.updateForwardingPath(serviceIdLower, updatedService, modifier, true);
154 actionResponse = serviceBusinessLogic.createForwardingPath(serviceIdLower, updatedService, modifier, true);
156 Service service = actionResponse;
157 Object result = RepresentationUtils.toRepresentation(service);
158 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
159 } catch (IOException e) {
160 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Forward Path Creation or update");
161 log.debug("create or update forwarding path with an error", e);
167 @Consumes(MediaType.APPLICATION_JSON)
168 @Produces(MediaType.APPLICATION_JSON)
169 @Path("/{forwardingPathId}")
170 @Operation(description = "Get Forwarding Path", method = "GET", summary = "GET Forwarding Path", responses = {
171 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = ForwardingPathDataDefinition.class)))),
172 @ApiResponse(responseCode = "201", description = "Get Forwarding Path"),
173 @ApiResponse(responseCode = "403", description = "Restricted operation"),
174 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
175 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
176 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
177 public Response getForwardingPath(@Parameter(description = "Forwarding Path to create", required = true) String datax,
178 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
179 @Parameter(description = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
180 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
182 String url = request.getMethod() + " " + request.getRequestURI();
183 log.debug(START_HANDLE_REQUEST_OF, url);
184 User modifier = new User();
185 modifier.setUserId(userId);
186 log.debug(MODIFIER_ID_IS, userId);
188 Either<UiComponentDataTransfer, ResponseFormat> serviceResponse = serviceBusinessLogic
189 .getComponentDataFilteredByParams(serviceId, modifier, Collections.singletonList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
190 if (serviceResponse.isRight()) {
191 return buildErrorResponse(serviceResponse.right().value());
193 UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) serviceResponse.left().value();
194 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition();
195 if (!MapUtils.isEmpty(uiServiceDataTransfer.getForwardingPaths())) {
196 forwardingPathDataDefinition = uiServiceDataTransfer.getForwardingPaths().get(forwardingPathId);
198 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
199 RepresentationUtils.toRepresentation(forwardingPathDataDefinition));
200 } catch (Exception e) {
201 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Service Metadata");
202 log.debug("update service metadata failed with exception", e);
208 @Consumes(MediaType.APPLICATION_JSON)
209 @Produces(MediaType.APPLICATION_JSON)
210 @Path("/{forwardingPathId}")
211 @Operation(description = "Delete Forwarding Path", method = "DELETE", summary = "Delete Forwarding Path", responses = {
212 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Service.class)))),
213 @ApiResponse(responseCode = "201", description = "Delete Forwarding Path"),
214 @ApiResponse(responseCode = "403", description = "Restricted operation"),
215 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
216 @ApiResponse(responseCode = "409", description = "Forwarding Path already exist")})
217 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
218 public Response deleteForwardingPath(@Parameter(description = "Forwarding Path Id") @PathParam("forwardingPathId") String forwardingPathId,
219 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
220 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
222 String url = request.getMethod() + " " + request.getRequestURI();
223 log.debug(START_HANDLE_REQUEST_OF, url);
224 User modifier = new User();
225 modifier.setUserId(userId);
226 log.debug(MODIFIER_ID_IS, userId);
229 String serviceIdLower = serviceId.toLowerCase();
230 Set<String> deletedPaths = serviceBusinessLogic.deleteForwardingPaths(serviceIdLower, Sets.newHashSet(forwardingPathId), modifier, true);
231 Object result = RepresentationUtils.toRepresentation(deletedPaths);
232 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
233 } catch (IOException e) {
234 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete forward paths");
235 log.debug("Delete service paths with an error", e);
240 private Either<Service, ResponseFormat> parseToService(String serviceJson, User user) {
241 return getComponentsUtils().convertJsonToObjectUsingObjectMapper(serviceJson, user, Service.class, AuditingActionEnum.CREATE_RESOURCE,
242 ComponentTypeEnum.SERVICE);//TODO: change sSERVICE constant