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.config.BeEcompErrorManager;
54 import org.openecomp.sdc.be.dao.api.ActionStatus;
55 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
56 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
57 import org.openecomp.sdc.be.impl.ComponentsUtils;
58 import org.openecomp.sdc.be.impl.ServletUtils;
59 import org.openecomp.sdc.be.model.InterfaceDefinition;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
62 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
63 import org.openecomp.sdc.common.api.Constants;
64 import org.openecomp.sdc.exception.ResponseFormat;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import org.springframework.stereotype.Controller;
69 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
73 @Tags({@Tag(name = "SDCE-2 APIs")})
74 @Servers({@Server(url = "/sdc2/rest")})
76 public class InterfaceOperationServlet extends AbstractValidationsServlet {
78 private static final Logger log = LoggerFactory.getLogger(InterfaceOperationServlet.class);
79 private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
82 public InterfaceOperationServlet(ComponentInstanceBusinessLogic componentInstanceBL,
83 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
84 InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) {
85 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
86 this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic;
90 @Consumes(MediaType.APPLICATION_JSON)
91 @Produces(MediaType.APPLICATION_JSON)
92 @Path("/resources/{resourceId}/interfaceOperations")
93 @Operation(description = "Create Interface Operations on Resource", method = "POST", summary = "Create Interface Operations on Resource", responses = {
94 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
95 @ApiResponse(responseCode = "201", description = "Create Interface Operations on Resource"),
96 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
97 @ApiResponse(responseCode = "403", description = "Restricted operation"),
98 @ApiResponse(responseCode = "404", description = "Resource not found"),
99 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
100 public Response createInterfaceOperationsOnResource(@Parameter(description = "Interface Operations to create", required = true) String data,
101 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
102 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
103 @Context final HttpServletRequest request) {
104 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, false);
107 private Response createOrUpdate(String data, ComponentTypeEnum componentType, String componentId, HttpServletRequest request, String userId,
109 String url = request.getMethod() + " " + request.getRequestURI();
110 User modifier = new User();
111 modifier.setUserId(userId);
112 log.debug("Start create or update request of {} with modifier id {}", url, userId);
114 String componentIdLower = componentId.toLowerCase();
115 List<InterfaceDefinition> mappedInterfaceData = getMappedInterfaceData(data, modifier, componentType);
116 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse;
118 actionResponse = interfaceOperationBusinessLogic.updateInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
120 actionResponse = interfaceOperationBusinessLogic.createInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
122 if (actionResponse.isRight()) {
123 log.error("failed to create or update interface operation");
124 return buildErrorResponse(actionResponse.right().value());
126 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
127 } catch (Exception e) {
128 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update");
129 log.error("create or update interface Operation with an error", e);
130 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
134 private List<InterfaceDefinition> getMappedInterfaceData(String inputJson, User user, ComponentTypeEnum componentTypeEnum) {
135 Either<UiComponentDataTransfer, ResponseFormat> uiComponentEither = getComponentsUtils()
136 .convertJsonToObjectUsingObjectMapper(inputJson, user, UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE,
138 return new ArrayList<>(uiComponentEither.left().value().getInterfaces().values());
141 private Object getFormattedResponse(List<InterfaceDefinition> interfaceDefinitions) throws IOException {
142 Map<String, List<InterfaceDefinition>> allInterfaces = ImmutableMap
143 .of(JsonPresentationFields.INTERFACES.getPresentation(), interfaceDefinitions);
144 return RepresentationUtils.toFilteredRepresentation(allInterfaces);
148 @Consumes(MediaType.APPLICATION_JSON)
149 @Produces(MediaType.APPLICATION_JSON)
150 @Path("/resources/{resourceId}/interfaceOperations")
151 @Operation(description = "Update Interface Operations on Resource", method = "PUT", summary = "Update Interface Operations on Resource", responses = {
152 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
153 @ApiResponse(responseCode = "201", description = "Update Interface Operations on Resource"),
154 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
155 @ApiResponse(responseCode = "403", description = "Restricted operation"),
156 @ApiResponse(responseCode = "404", description = "Resource not found")})
157 public Response updateInterfaceOperationsOnResource(@Parameter(description = "Interface Operations to update", required = true) String data,
158 @Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
159 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
160 @Context final HttpServletRequest request) {
161 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, true);
165 @Consumes(MediaType.APPLICATION_JSON)
166 @Produces(MediaType.APPLICATION_JSON)
167 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
168 @Operation(description = "Delete Interface Operation from Resource", method = "DELETE", summary = "Delete Interface Operation from Resource", responses = {
169 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
170 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from 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 public Response deleteInterfaceOperationsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
175 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
176 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
177 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
178 @Context final HttpServletRequest request) {
179 return delete(interfaceId, operationId, resourceId, request, userId);
182 private Response delete(String interfaceId, String operationId, String componentId, HttpServletRequest request, String userId) {
183 String url = request.getMethod() + " " + request.getRequestURI();
184 User modifier = new User();
185 modifier.setUserId(userId);
186 log.debug("Start delete request of {} with modifier id {}", url, userId);
188 String componentIdLower = componentId.toLowerCase();
189 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic
190 .deleteInterfaceOperation(componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
191 if (actionResponse.isRight()) {
192 log.error("failed to delete interface operation");
193 return buildErrorResponse(actionResponse.right().value());
195 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
196 } catch (Exception e) {
197 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation");
198 log.error("Delete interface operation with an error", e);
199 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
204 @Consumes(MediaType.APPLICATION_JSON)
205 @Produces(MediaType.APPLICATION_JSON)
206 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
207 @Operation(description = "Get Interface Operation from Resource", method = "GET", summary = "GET Interface Operation from Resource", responses = {
208 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
209 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from Resource"),
210 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
211 @ApiResponse(responseCode = "403", description = "Restricted operation"),
212 @ApiResponse(responseCode = "404", description = "Resource not found")})
213 public Response getInterfaceOperationsFromResource(@Parameter(description = "Resource Id") @PathParam("resourceId") String resourceId,
214 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
215 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
216 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
217 @Context final HttpServletRequest request) {
218 return get(interfaceId, operationId, resourceId, request, userId);
221 private Response get(String interfaceId, String operationId, String componentId, HttpServletRequest request, String userId) {
222 String url = request.getMethod() + " " + request.getRequestURI();
223 User modifier = new User();
224 modifier.setUserId(userId);
225 log.debug("Start get request of {} with modifier id {}", url, userId);
227 String componentIdLower = componentId.toLowerCase();
228 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse = interfaceOperationBusinessLogic
229 .getInterfaceOperation(componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
230 if (actionResponse.isRight()) {
231 log.error("failed to get interface operation");
232 return buildErrorResponse(actionResponse.right().value());
234 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), getFormattedResponse(actionResponse.left().value()));
235 } catch (Exception e) {
236 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component interface operations");
237 log.error("get component interface operations failed with exception", e);
238 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
243 @Consumes(MediaType.APPLICATION_JSON)
244 @Produces(MediaType.APPLICATION_JSON)
245 @Path("/services/{serviceId}/interfaceOperations")
246 @Operation(description = "Create Interface Operations on Service", method = "POST", summary = "Create Interface Operations on Service", responses = {
247 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
248 @ApiResponse(responseCode = "201", description = "Create Interface Operations on Service"),
249 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
250 @ApiResponse(responseCode = "403", description = "Restricted operation"),
251 @ApiResponse(responseCode = "404", description = "Service not found"),
252 @ApiResponse(responseCode = "409", description = "Interface Operation already exist")})
253 public Response createInterfaceOperationsOnService(@Parameter(description = "Interface Operations to create", required = true) String data,
254 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
255 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
256 @Context final HttpServletRequest request) {
257 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, false);
261 @Consumes(MediaType.APPLICATION_JSON)
262 @Produces(MediaType.APPLICATION_JSON)
263 @Path("/services/{serviceId}/interfaceOperations")
264 @Operation(description = "Update Interface Operations on Service", method = "PUT", summary = "Update Interface Operations on Service", responses = {
265 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
266 @ApiResponse(responseCode = "201", description = "Update Interface Operations on Service"),
267 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
268 @ApiResponse(responseCode = "403", description = "Restricted operation"),
269 @ApiResponse(responseCode = "404", description = "Service not found")})
270 public Response updateInterfaceOperationsOnService(@Parameter(description = "Interface Operations to update", required = true) String data,
271 @Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
272 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
273 @Context final HttpServletRequest request) {
274 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, true);
278 @Consumes(MediaType.APPLICATION_JSON)
279 @Produces(MediaType.APPLICATION_JSON)
280 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
281 @Operation(description = "Delete Interface Operation from Service", method = "DELETE", summary = "Delete Interface Operation from Service", responses = {
282 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
283 @ApiResponse(responseCode = "201", description = "Delete Interface Operation from Service"),
284 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
285 @ApiResponse(responseCode = "403", description = "Restricted operation"),
286 @ApiResponse(responseCode = "404", description = "Service not found")})
287 public Response deleteInterfaceOperationsFromService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
288 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
289 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
290 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
291 @Context final HttpServletRequest request) {
292 return delete(interfaceId, operationId, serviceId, request, userId);
296 @Consumes(MediaType.APPLICATION_JSON)
297 @Produces(MediaType.APPLICATION_JSON)
298 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
299 @Operation(description = "Get Interface Operation from Service", method = "GET", summary = "GET Interface Operation from Service", responses = {
300 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = InterfaceDefinition.class)))),
301 @ApiResponse(responseCode = "201", description = "Get Interface Operation from Service"),
302 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
303 @ApiResponse(responseCode = "403", description = "Restricted operation"),
304 @ApiResponse(responseCode = "404", description = "Service not found")})
305 public Response getInterfaceOperationsFromService(@Parameter(description = "Service Id") @PathParam("serviceId") String serviceId,
306 @Parameter(description = "Interface Id") @PathParam("interfaceId") String interfaceId,
307 @Parameter(description = "Operation Id") @PathParam("operationId") String operationId,
308 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
309 @Context final HttpServletRequest request) {
310 return get(interfaceId, operationId, serviceId, request, userId);