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.
16 package org.openecomp.sdc.be.servlets;
18 import com.google.common.collect.ImmutableMap;
19 import com.jcabi.aspects.Loggable;
20 import fj.data.Either;
21 import io.swagger.v3.oas.annotations.Operation;
22 import io.swagger.v3.oas.annotations.Parameter;
23 import io.swagger.v3.oas.annotations.media.ArraySchema;
24 import io.swagger.v3.oas.annotations.media.Content;
25 import io.swagger.v3.oas.annotations.media.Schema;
26 import io.swagger.v3.oas.annotations.responses.ApiResponse;
27 import io.swagger.v3.oas.annotations.servers.Server;
28 import io.swagger.v3.oas.annotations.servers.Servers;
29 import io.swagger.v3.oas.annotations.tags.Tag;
30 import io.swagger.v3.oas.annotations.tags.Tags;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 import javax.inject.Inject;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.Consumes;
39 import javax.ws.rs.DELETE;
40 import javax.ws.rs.GET;
41 import javax.ws.rs.HeaderParam;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.PUT;
44 import javax.ws.rs.Path;
45 import javax.ws.rs.PathParam;
46 import javax.ws.rs.Produces;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
50 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
53 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
54 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
55 import org.openecomp.sdc.be.config.BeEcompErrorManager;
56 import org.openecomp.sdc.be.dao.api.ActionStatus;
57 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
58 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
59 import org.openecomp.sdc.be.impl.ComponentsUtils;
60 import org.openecomp.sdc.be.impl.ServletUtils;
61 import org.openecomp.sdc.be.model.InterfaceDefinition;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
64 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
65 import org.openecomp.sdc.common.api.Constants;
66 import org.openecomp.sdc.exception.ResponseFormat;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69 import org.springframework.stereotype.Controller;
71 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
73 @Consumes(MediaType.APPLICATION_JSON)
74 @Produces(MediaType.APPLICATION_JSON)
75 @Tags({@Tag(name = "SDCE-2 APIs")})
76 @Servers({@Server(url = "/sdc2/rest")})
78 public class InterfaceOperationServlet extends AbstractValidationsServlet {
80 private static final Logger log = LoggerFactory.getLogger(InterfaceOperationServlet.class);
81 private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
84 public InterfaceOperationServlet(ComponentInstanceBusinessLogic componentInstanceBL,
85 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
86 InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) {
87 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
88 this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic;
92 @Consumes(MediaType.APPLICATION_JSON)
93 @Produces(MediaType.APPLICATION_JSON)
94 @Path("/resources/{resourceId}/interfaceOperations")
95 @Operation(description = "Create Interface Operations on Resource", method = "POST", summary = "Create Interface Operations on Resource", responses = {
96 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
97 @ApiResponse(responseCode = "201", description = "Create Interface Operations on Resource"),
98 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
99 @ApiResponse(responseCode = "403", description = "Restricted operation"),
100 @ApiResponse(responseCode = "404", description = "Resource not found"),
101 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
102 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
103 public Response createInterfaceOperationsOnResource(@Parameter(description = "Interface Operations to create", required = true) String data,
104 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
105 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
106 @Context final HttpServletRequest request) {
107 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, false);
110 private Response createOrUpdate(String data, ComponentTypeEnum componentType, String componentId, HttpServletRequest request, String userId,
112 String url = request.getMethod() + " " + request.getRequestURI();
113 User modifier = new User();
114 modifier.setUserId(userId);
115 log.debug("Start create or update request of {} with modifier id {}", url, userId);
117 String componentIdLower = componentId.toLowerCase();
118 List<InterfaceDefinition> mappedInterfaceData = getMappedInterfaceData(data, modifier, componentType);
119 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse;
121 actionResponse = interfaceOperationBusinessLogic.updateInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
123 actionResponse = interfaceOperationBusinessLogic.createInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
125 if (actionResponse.isRight()) {
126 log.error("failed to create or update interface operation");
127 return buildErrorResponse(actionResponse.right().value());
129 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
130 } catch (Exception e) {
131 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update");
132 log.error("create or update interface Operation with an error", e);
133 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
137 private List<InterfaceDefinition> getMappedInterfaceData(String inputJson, User user, ComponentTypeEnum componentTypeEnum) {
138 Either<UiComponentDataTransfer, ResponseFormat> uiComponentEither = getComponentsUtils()
139 .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
141 return new ArrayList<>(uiComponentEither.left().value().getInterfaces().values());
144 private Object getFormattedResponse(List<InterfaceDefinition> interfaceDefinitions) throws IOException {
145 Map<String, List<InterfaceDefinition>> allInterfaces = ImmutableMap
146 .of(JsonPresentationFields.INTERFACES.getPresentation(), interfaceDefinitions);
147 return RepresentationUtils.toFilteredRepresentation(allInterfaces);
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 @Path("/resources/{resourceId}/interfaceOperations")
154 @Operation(description = "Update Interface Operations on Resource", method = "PUT", summary = "Update Interface Operations on Resource", responses = {
155 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
156 @ApiResponse(responseCode = "201", description = "Update Interface Operations on Resource"),
157 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
158 @ApiResponse(responseCode = "403", description = "Restricted operation"),
159 @ApiResponse(responseCode = "404", description = "Resource not found")})
160 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
161 public Response updateInterfaceOperationsOnResource(@Parameter(description = "Interface Operations to update", required = true) String data,
162 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
163 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
164 @Context final HttpServletRequest request) {
165 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, true);
169 @Consumes(MediaType.APPLICATION_JSON)
170 @Produces(MediaType.APPLICATION_JSON)
171 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
172 @Operation(description = "Delete Interface Operation from Resource", method = "DELETE", summary = "Delete Interface Operation from Resource", responses = {
173 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
174 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from Resource"),
175 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
176 @ApiResponse(responseCode = "403", description = "Restricted operation"),
177 @ApiResponse(responseCode = "404", description = "Resource not found")})
178 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
179 public Response deleteInterfaceOperationsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
180 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
181 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
182 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
183 @Context final HttpServletRequest request) {
184 return delete(interfaceId, operationId, resourceId, request, userId);
187 private Response delete(String interfaceId, String operationId, String componentId, HttpServletRequest request, String userId) {
188 String url = request.getMethod() + " " + request.getRequestURI();
189 User modifier = new User();
190 modifier.setUserId(userId);
191 log.debug("Start delete request of {} with modifier id {}", url, userId);
193 String componentIdLower = componentId.toLowerCase();
194 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic
195 .deleteInterfaceOperation(componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
196 if (actionResponse.isRight()) {
197 log.error("failed to delete interface operation");
198 return buildErrorResponse(actionResponse.right().value());
200 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
201 } catch (Exception e) {
202 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation");
203 log.error("Delete interface operation with an error", e);
204 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
209 @Consumes(MediaType.APPLICATION_JSON)
210 @Produces(MediaType.APPLICATION_JSON)
211 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
212 @Operation(description = "Get Interface Operation from Resource", method = "GET", summary = "GET Interface Operation from Resource", responses = {
213 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
214 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from Resource"),
215 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
216 @ApiResponse(responseCode = "403", description = "Restricted operation"),
217 @ApiResponse(responseCode = "404", description = "Resource not found")})
218 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
219 public Response getInterfaceOperationsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
220 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
221 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
222 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
223 @Context final HttpServletRequest request) {
224 return get(interfaceId, operationId, resourceId, request, userId);
227 private Response get(String interfaceId, String operationId, String componentId, HttpServletRequest request, String userId) {
228 String url = request.getMethod() + " " + request.getRequestURI();
229 User modifier = new User();
230 modifier.setUserId(userId);
231 log.debug("Start get request of {} with modifier id {}", url, userId);
233 String componentIdLower = componentId.toLowerCase();
234 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic
235 .getInterfaceOperation(componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
236 if (actionResponse.isRight()) {
237 log.error("failed to get interface operation");
238 return buildErrorResponse(actionResponse.right().value());
240 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
241 } catch (Exception e) {
242 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component interface operations");
243 log.error("get component interface operations failed with exception", e);
244 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
249 @Consumes(MediaType.APPLICATION_JSON)
250 @Produces(MediaType.APPLICATION_JSON)
251 @Path("/services/{serviceId}/interfaceOperations")
252 @Operation(description = "Create Interface Operations on Service", method = "POST", summary = "Create Interface Operations on Service", responses = {
253 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
254 @ApiResponse(responseCode = "201", description = "Create Interface Operations on Service"),
255 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
256 @ApiResponse(responseCode = "403", description = "Restricted operation"),
257 @ApiResponse(responseCode = "404", description = "Service not found"),
258 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
259 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
260 public Response createInterfaceOperationsOnService(@Parameter(description = "Interface Operations to create", required = true) String data,
261 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
262 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
263 @Context final HttpServletRequest request) {
264 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, false);
268 @Consumes(MediaType.APPLICATION_JSON)
269 @Produces(MediaType.APPLICATION_JSON)
270 @Path("/services/{serviceId}/interfaceOperations")
271 @Operation(description = "Update Interface Operations on Service", method = "PUT", summary = "Update Interface Operations on Service", responses = {
272 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
273 @ApiResponse(responseCode = "201", description = "Update Interface Operations on Service"),
274 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
275 @ApiResponse(responseCode = "403", description = "Restricted operation"),
276 @ApiResponse(responseCode = "404", description = "Service not found")})
277 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
278 public Response updateInterfaceOperationsOnService(@Parameter(description = "Interface Operations to update", required = true) String data,
279 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
280 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
281 @Context final HttpServletRequest request) {
282 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, true);
286 @Consumes(MediaType.APPLICATION_JSON)
287 @Produces(MediaType.APPLICATION_JSON)
288 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
289 @Operation(description = "Delete Interface Operation from Service", method = "DELETE", summary = "Delete Interface Operation from Service", responses = {
290 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
291 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from Service"),
292 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
293 @ApiResponse(responseCode = "403", description = "Restricted operation"),
294 @ApiResponse(responseCode = "404", description = "Service not found")})
295 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
296 public Response deleteInterfaceOperationsFromService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
297 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
298 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
299 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
300 @Context final HttpServletRequest request) {
301 return delete(interfaceId, operationId, serviceId, request, userId);
305 @Consumes(MediaType.APPLICATION_JSON)
306 @Produces(MediaType.APPLICATION_JSON)
307 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
308 @Operation(description = "Get Interface Operation from Service", method = "GET", summary = "GET Interface Operation from Service", responses = {
309 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
310 @ApiResponse(responseCode = "201", description = "Get Interface Operation from Service"),
311 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
312 @ApiResponse(responseCode = "403", description = "Restricted operation"),
313 @ApiResponse(responseCode = "404", description = "Service not found")})
314 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
315 public Response getInterfaceOperationsFromService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
316 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
317 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
318 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
319 @Context final HttpServletRequest request) {
320 return get(interfaceId, operationId, serviceId, request, userId);