2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.servlets;
19 import com.google.common.collect.ImmutableMap;
20 import com.jcabi.aspects.Loggable;
21 import fj.data.Either;
22 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.info.Info;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.responses.ApiResponses;
31 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
34 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
35 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.impl.ServletUtils;
42 import org.openecomp.sdc.be.model.InterfaceDefinition;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
45 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
46 import org.openecomp.sdc.be.user.UserBusinessLogic;
47 import org.openecomp.sdc.common.api.Constants;
48 import org.openecomp.sdc.exception.ResponseFormat;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.stereotype.Controller;
53 import javax.inject.Inject;
54 import javax.servlet.http.HttpServletRequest;
55 import javax.ws.rs.Consumes;
56 import javax.ws.rs.DELETE;
57 import javax.ws.rs.GET;
58 import javax.ws.rs.HeaderParam;
59 import javax.ws.rs.POST;
60 import javax.ws.rs.PUT;
61 import javax.ws.rs.Path;
62 import javax.ws.rs.PathParam;
63 import javax.ws.rs.Produces;
64 import javax.ws.rs.core.Context;
65 import javax.ws.rs.core.MediaType;
66 import javax.ws.rs.core.Response;
67 import java.io.IOException;
68 import java.util.ArrayList;
69 import java.util.Collections;
70 import java.util.List;
73 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
75 @Consumes(MediaType.APPLICATION_JSON)
76 @Produces(MediaType.APPLICATION_JSON)
77 @OpenAPIDefinition(info = @Info(title = "Interface Operation Servlet", description = "Interface Operation Servlet"))
79 public class InterfaceOperationServlet extends AbstractValidationsServlet {
81 private static final Logger log = LoggerFactory.getLogger(InterfaceOperationServlet.class);
82 private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
85 public InterfaceOperationServlet(UserBusinessLogic userBusinessLogic,
86 ComponentInstanceBusinessLogic componentInstanceBL,
87 ComponentsUtils componentsUtils, ServletUtils servletUtils,
88 ResourceImportManager resourceImportManager,
89 InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) {
90 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
91 this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic;
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_JSON)
97 @Path("/resources/{resourceId}/interfaceOperations")
98 @Operation(description = "Create Interface Operations on Resource", method = "POST",
99 summary = "Create Interface Operations on Resource",responses = @ApiResponse(content = @Content(
100 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
101 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Create Interface Operations on Resource"),
102 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
103 @ApiResponse(responseCode = "403", description = "Restricted operation"),
104 @ApiResponse(responseCode = "404", description = "Resource not found"),
105 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
106 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
107 public Response createInterfaceOperationsOnResource(
108 @Parameter(description = "Interface Operations to create", required = true) String data,
109 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
110 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
111 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, false);
114 private Response createOrUpdate(String data, ComponentTypeEnum componentType, String componentId,
115 HttpServletRequest request, String userId, boolean isUpdate) {
116 String url = request.getMethod() + " " + request.getRequestURI();
118 User modifier = new User();
119 modifier.setUserId(userId);
120 log.debug("Start create or update request of {} with modifier id {}", url, userId);
123 String componentIdLower = componentId.toLowerCase();
125 List<InterfaceDefinition> mappedInterfaceData = getMappedInterfaceData(data, modifier, componentType);
126 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse;
129 interfaceOperationBusinessLogic.updateInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
132 interfaceOperationBusinessLogic.createInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
135 if (actionResponse.isRight()) {
136 log.error("failed to create or update interface operation");
137 return buildErrorResponse(actionResponse.right().value());
140 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
141 getFormattedResponse(actionResponse.left().value()));
142 } catch (Exception e) {
143 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update");
144 log.error("create or update interface Operation with an error", e);
145 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
149 private List<InterfaceDefinition> getMappedInterfaceData(String inputJson, User user,
150 ComponentTypeEnum componentTypeEnum) {
151 Either<UiComponentDataTransfer, ResponseFormat> uiComponentEither =
152 getComponentsUtils().convertJsonToObjectUsingObjectMapper(inputJson, user,
153 UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, componentTypeEnum);
154 return new ArrayList<>(uiComponentEither.left().value().getInterfaces().values());
157 private Object getFormattedResponse(List<InterfaceDefinition> interfaceDefinitions) throws IOException {
158 Map<String, List<InterfaceDefinition>> allInterfaces =
159 ImmutableMap.of(JsonPresentationFields.INTERFACES.getPresentation(), interfaceDefinitions);
160 return RepresentationUtils.toFilteredRepresentation(allInterfaces);
164 @Consumes(MediaType.APPLICATION_JSON)
165 @Produces(MediaType.APPLICATION_JSON)
166 @Path("/resources/{resourceId}/interfaceOperations")
167 @Operation(description = "Update Interface Operations on Resource", method = "PUT",
168 summary = "Update Interface Operations on Resource",responses = @ApiResponse(content = @Content(
169 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
170 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Update Interface Operations on Resource"),
171 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
172 @ApiResponse(responseCode = "403", description = "Restricted operation"),
173 @ApiResponse(responseCode = "404", description = "Resource not found")})
174 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
175 public Response updateInterfaceOperationsOnResource(
176 @Parameter(description = "Interface Operations to update", required = true) String data,
177 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
178 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
179 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, true);
183 @Consumes(MediaType.APPLICATION_JSON)
184 @Produces(MediaType.APPLICATION_JSON)
185 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
186 @Operation(description = "Delete Interface Operation from Resource", method = "DELETE",
187 summary = "Delete Interface Operation from Resource", responses = @ApiResponse(content = @Content(
188 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
189 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Delete Interface Operation from Resource"),
190 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
191 @ApiResponse(responseCode = "403", description = "Restricted operation"),
192 @ApiResponse(responseCode = "404", description = "Resource not found")})
193 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
194 public Response deleteInterfaceOperationsFromResource(
195 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
196 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
197 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
198 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
199 return delete(interfaceId, operationId, resourceId, request, userId);
202 private Response delete(String interfaceId, String operationId, String componentId, HttpServletRequest request,
205 String url = request.getMethod() + " " + request.getRequestURI();
207 User modifier = new User();
208 modifier.setUserId(userId);
209 log.debug("Start delete request of {} with modifier id {}", url, userId);
212 String componentIdLower = componentId.toLowerCase();
213 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic.deleteInterfaceOperation(
214 componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
215 if (actionResponse.isRight()) {
216 log.error("failed to delete interface operation");
217 return buildErrorResponse(actionResponse.right().value());
220 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
221 getFormattedResponse(actionResponse.left().value()));
222 } catch (Exception e) {
223 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation");
224 log.error("Delete interface operation with an error", e);
225 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
230 @Consumes(MediaType.APPLICATION_JSON)
231 @Produces(MediaType.APPLICATION_JSON)
232 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
233 @Operation(description = "Get Interface Operation from Resource", method = "GET",
234 summary = "GET Interface Operation from Resource",responses = @ApiResponse(content = @Content(
235 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
236 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Delete Interface Operation from Resource"),
237 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
238 @ApiResponse(responseCode = "403", description = "Restricted operation"),
239 @ApiResponse(responseCode = "404", description = "Resource not found")})
240 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
241 public Response getInterfaceOperationsFromResource(
242 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
243 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
244 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
245 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
246 return get(interfaceId, operationId, resourceId, request, userId);
249 private Response get(String interfaceId, String operationId, String componentId, HttpServletRequest request,
251 String url = request.getMethod() + " " + request.getRequestURI();
253 User modifier = new User();
254 modifier.setUserId(userId);
255 log.debug("Start get request of {} with modifier id {}", url, userId);
258 String componentIdLower = componentId.toLowerCase();
259 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic.getInterfaceOperation(
260 componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
261 if (actionResponse.isRight()) {
262 log.error("failed to get interface operation");
263 return buildErrorResponse(actionResponse.right().value());
266 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
267 getFormattedResponse(actionResponse.left().value()));
268 } catch (Exception e) {
269 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component interface operations");
270 log.error("get component interface operations failed with exception", e);
271 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
276 @Consumes(MediaType.APPLICATION_JSON)
277 @Produces(MediaType.APPLICATION_JSON)
278 @Path("/services/{serviceId}/interfaceOperations")
279 @Operation(description = "Create Interface Operations on Service", method = "POST",
280 summary = "Create Interface Operations on Service", responses = @ApiResponse(content = @Content(
281 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
282 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Create Interface Operations on Service"),
283 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
284 @ApiResponse(responseCode = "403", description = "Restricted operation"),
285 @ApiResponse(responseCode = "404", description = "Service not found"),
286 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
287 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
288 public Response createInterfaceOperationsOnService(
289 @Parameter(description = "Interface Operations to create", required = true) String data,
290 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
291 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
292 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, false);
296 @Consumes(MediaType.APPLICATION_JSON)
297 @Produces(MediaType.APPLICATION_JSON)
298 @Path("/services/{serviceId}/interfaceOperations")
299 @Operation(description = "Update Interface Operations on Service", method = "PUT",
300 summary = "Update Interface Operations on Service",responses = @ApiResponse(content = @Content(
301 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
302 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Update Interface Operations on Service"),
303 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
304 @ApiResponse(responseCode = "403", description = "Restricted operation"),
305 @ApiResponse(responseCode = "404", description = "Service not found")})
306 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
307 public Response updateInterfaceOperationsOnService(
308 @Parameter(description = "Interface Operations to update", required = true) String data,
309 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
310 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
311 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, true);
315 @Consumes(MediaType.APPLICATION_JSON)
316 @Produces(MediaType.APPLICATION_JSON)
317 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
318 @Operation(description = "Delete Interface Operation from Service", method = "DELETE",
319 summary = "Delete Interface Operation from Service",responses = @ApiResponse(content = @Content(
320 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
321 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Delete Interface Operation from Service"),
322 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
323 @ApiResponse(responseCode = "403", description = "Restricted operation"),
324 @ApiResponse(responseCode = "404", description = "Service not found")})
325 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
326 public Response deleteInterfaceOperationsFromService(
327 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
328 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
329 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
330 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
331 return delete(interfaceId, operationId, serviceId, request, userId);
335 @Consumes(MediaType.APPLICATION_JSON)
336 @Produces(MediaType.APPLICATION_JSON)
337 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
338 @Operation(description = "Get Interface Operation from Service", method = "GET",
339 summary = "GET Interface Operation from Service",responses = @ApiResponse(content = @Content(
340 array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))))
341 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Get Interface Operation from Service"),
342 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
343 @ApiResponse(responseCode = "403", description = "Restricted operation"),
344 @ApiResponse(responseCode = "404", description = "Service not found")})
345 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
346 public Response getInterfaceOperationsFromService(
347 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
348 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
349 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
350 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
351 return get(interfaceId, operationId, serviceId, request, userId);