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.annotations.Api;
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
32 import javax.inject.Inject;
33 import javax.inject.Singleton;
34 import javax.servlet.ServletContext;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.HeaderParam;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.PUT;
42 import javax.ws.rs.Path;
43 import javax.ws.rs.PathParam;
44 import javax.ws.rs.Produces;
45 import javax.ws.rs.core.Context;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.Response;
48 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
52 import org.openecomp.sdc.be.config.BeEcompErrorManager;
53 import org.openecomp.sdc.be.dao.api.ActionStatus;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
56 import org.openecomp.sdc.be.impl.ComponentsUtils;
57 import org.openecomp.sdc.be.impl.ServletUtils;
58 import org.openecomp.sdc.be.model.InterfaceDefinition;
59 import org.openecomp.sdc.be.model.User;
60 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
61 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
62 import org.openecomp.sdc.be.user.UserBusinessLogic;
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.beans.factory.annotation.Autowired;
69 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
73 @Api(value = "Interface Operation Servlet", description = "Interface Operation Servlet")
75 public class InterfaceOperationServlet extends AbstractValidationsServlet {
77 private static final Logger log = LoggerFactory.getLogger(InterfaceOperationServlet.class);
78 private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic;
81 public InterfaceOperationServlet(UserBusinessLogic userBusinessLogic,
82 ComponentInstanceBusinessLogic componentInstanceBL,
83 ComponentsUtils componentsUtils, ServletUtils servletUtils,
84 ResourceImportManager resourceImportManager,
85 InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) {
86 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
87 this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic;
91 @Consumes(MediaType.APPLICATION_JSON)
92 @Produces(MediaType.APPLICATION_JSON)
93 @Path("/resources/{resourceId}/interfaceOperations")
94 @ApiOperation(value = "Create Interface Operations on Resource", httpMethod = "POST",
95 notes = "Create Interface Operations on Resource", response = InterfaceDefinition.class, responseContainer = "List")
96 @ApiResponses(value = {@ApiResponse(code = 201, message = "Create Interface Operations on Resource"),
97 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
98 @ApiResponse(code = 403, message = "Restricted operation"),
99 @ApiResponse(code = 404, message = "Resource not found"),
100 @ApiResponse(code = 409, message = "Interface Operation already exist")})
101 public Response createInterfaceOperationsOnResource(
102 @ApiParam(value = "Interface Operations to create", required = true) String data,
103 @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
104 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
105 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, false);
108 private Response createOrUpdate(String data, ComponentTypeEnum componentType, String componentId,
109 HttpServletRequest request, String userId, boolean isUpdate) {
110 String url = request.getMethod() + " " + request.getRequestURI();
112 User modifier = new User();
113 modifier.setUserId(userId);
114 log.debug("Start create or update request of {} with modifier id {}", url, userId);
117 String componentIdLower = componentId.toLowerCase();
119 List<InterfaceDefinition> mappedInterfaceData = getMappedInterfaceData(data, modifier, componentType);
120 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse;
123 interfaceOperationBusinessLogic
124 .updateInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
127 interfaceOperationBusinessLogic
128 .createInterfaceOperation(componentIdLower, mappedInterfaceData, modifier, true);
131 if (actionResponse.isRight()) {
132 log.error("failed to create or update interface operation");
133 return buildErrorResponse(actionResponse.right().value());
136 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
137 getFormattedResponse(actionResponse.left().value()));
138 } catch (Exception e) {
139 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update");
140 log.error("create or update interface Operation with an error", e);
141 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
145 private List<InterfaceDefinition> getMappedInterfaceData(String inputJson, User user,
146 ComponentTypeEnum componentTypeEnum) {
147 Either<UiComponentDataTransfer, ResponseFormat> uiComponentEither =
148 getComponentsUtils().convertJsonToObjectUsingObjectMapper(inputJson, user,
149 UiComponentDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, componentTypeEnum);
150 return new ArrayList<>(uiComponentEither.left().value().getInterfaces().values());
153 private Object getFormattedResponse(List<InterfaceDefinition> interfaceDefinitions) throws IOException {
154 Map<String, List<InterfaceDefinition>> allInterfaces =
155 ImmutableMap.of(JsonPresentationFields.INTERFACES.getPresentation(), interfaceDefinitions);
156 return RepresentationUtils.toFilteredRepresentation(allInterfaces);
160 @Consumes(MediaType.APPLICATION_JSON)
161 @Produces(MediaType.APPLICATION_JSON)
162 @Path("/resources/{resourceId}/interfaceOperations")
163 @ApiOperation(value = "Update Interface Operations on Resource", httpMethod = "PUT",
164 notes = "Update Interface Operations on Resource", response = InterfaceDefinition.class, responseContainer = "List")
165 @ApiResponses(value = {@ApiResponse(code = 201, message = "Update Interface Operations on Resource"),
166 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
167 @ApiResponse(code = 403, message = "Restricted operation"),
168 @ApiResponse(code = 404, message = "Resource not found")})
169 public Response updateInterfaceOperationsOnResource(
170 @ApiParam(value = "Interface Operations to update", required = true) String data,
171 @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
172 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
173 return createOrUpdate(data, ComponentTypeEnum.RESOURCE, resourceId, request, userId, true);
177 @Consumes(MediaType.APPLICATION_JSON)
178 @Produces(MediaType.APPLICATION_JSON)
179 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
180 @ApiOperation(value = "Delete Interface Operation from Resource", httpMethod = "DELETE",
181 notes = "Delete Interface Operation from Resource", response = InterfaceDefinition.class, responseContainer = "List")
182 @ApiResponses(value = {@ApiResponse(code = 201, message = "Delete Interface Operation from Resource"),
183 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
184 @ApiResponse(code = 403, message = "Restricted operation"),
185 @ApiResponse(code = 404, message = "Resource not found")})
186 public Response deleteInterfaceOperationsFromResource(
187 @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
188 @ApiParam(value = "Interface Id") @PathParam("interfaceId") String interfaceId,
189 @ApiParam(value = "Operation Id") @PathParam("operationId") String operationId,
190 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
191 return delete(interfaceId, operationId, resourceId, request, userId);
194 private Response delete(String interfaceId, String operationId, String componentId, HttpServletRequest request,
197 String url = request.getMethod() + " " + request.getRequestURI();
199 User modifier = new User();
200 modifier.setUserId(userId);
201 log.debug("Start delete request of {} with modifier id {}", url, userId);
204 String componentIdLower = componentId.toLowerCase();
205 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse =
206 interfaceOperationBusinessLogic.deleteInterfaceOperation(
207 componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
208 if (actionResponse.isRight()) {
209 log.error("failed to delete interface operation");
210 return buildErrorResponse(actionResponse.right().value());
213 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
214 getFormattedResponse(actionResponse.left().value()));
215 } catch (Exception e) {
216 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation");
217 log.error("Delete interface operation with an error", e);
218 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
223 @Consumes(MediaType.APPLICATION_JSON)
224 @Produces(MediaType.APPLICATION_JSON)
225 @Path("/resources/{resourceId}/interfaces/{interfaceId}/operations/{operationId}")
226 @ApiOperation(value = "Get Interface Operation from Resource", httpMethod = "GET",
227 notes = "GET Interface Operation from Resource", response = InterfaceDefinition.class, responseContainer = "List")
228 @ApiResponses(value = {@ApiResponse(code = 201, message = "Delete Interface Operation from Resource"),
229 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
230 @ApiResponse(code = 403, message = "Restricted operation"),
231 @ApiResponse(code = 404, message = "Resource not found")})
232 public Response getInterfaceOperationsFromResource(
233 @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
234 @ApiParam(value = "Interface Id") @PathParam("interfaceId") String interfaceId,
235 @ApiParam(value = "Operation Id") @PathParam("operationId") String operationId,
236 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
237 return get(interfaceId, operationId, resourceId, request, userId);
240 private Response get(String interfaceId, String operationId, String componentId, HttpServletRequest request,
242 String url = request.getMethod() + " " + request.getRequestURI();
244 User modifier = new User();
245 modifier.setUserId(userId);
246 log.debug("Start get request of {} with modifier id {}", url, userId);
249 String componentIdLower = componentId.toLowerCase();
250 Either<List<InterfaceDefinition>, ResponseFormat> actionResponse =
251 interfaceOperationBusinessLogic.getInterfaceOperation(
252 componentIdLower, interfaceId, Collections.singletonList(operationId), modifier, true);
253 if (actionResponse.isRight()) {
254 log.error("failed to get interface operation");
255 return buildErrorResponse(actionResponse.right().value());
258 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
259 getFormattedResponse(actionResponse.left().value()));
260 } catch (Exception e) {
261 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component interface operations");
262 log.error("get component interface operations failed with exception", e);
263 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
268 @Consumes(MediaType.APPLICATION_JSON)
269 @Produces(MediaType.APPLICATION_JSON)
270 @Path("/services/{serviceId}/interfaceOperations")
271 @ApiOperation(value = "Create Interface Operations on Service", httpMethod = "POST",
272 notes = "Create Interface Operations on Service", response = InterfaceDefinition.class, responseContainer = "List")
273 @ApiResponses(value = {@ApiResponse(code = 201, message = "Create Interface Operations on Service"),
274 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
275 @ApiResponse(code = 403, message = "Restricted operation"),
276 @ApiResponse(code = 404, message = "Service not found"),
277 @ApiResponse(code = 409, message = "Interface Operation already exist")})
278 public Response createInterfaceOperationsOnService(
279 @ApiParam(value = "Interface Operations to create", required = true) String data,
280 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
281 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
282 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, false);
286 @Consumes(MediaType.APPLICATION_JSON)
287 @Produces(MediaType.APPLICATION_JSON)
288 @Path("/services/{serviceId}/interfaceOperations")
289 @ApiOperation(value = "Update Interface Operations on Service", httpMethod = "PUT",
290 notes = "Update Interface Operations on Service", response = InterfaceDefinition.class, responseContainer = "List")
291 @ApiResponses(value = {@ApiResponse(code = 201, message = "Update Interface Operations on Service"),
292 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
293 @ApiResponse(code = 403, message = "Restricted operation"),
294 @ApiResponse(code = 404, message = "Service not found")})
295 public Response updateInterfaceOperationsOnService(
296 @ApiParam(value = "Interface Operations to update", required = true) String data,
297 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
298 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
299 return createOrUpdate(data, ComponentTypeEnum.SERVICE, serviceId, request, userId, true);
303 @Consumes(MediaType.APPLICATION_JSON)
304 @Produces(MediaType.APPLICATION_JSON)
305 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
306 @ApiOperation(value = "Delete Interface Operation from Service", httpMethod = "DELETE",
307 notes = "Delete Interface Operation from Service", response = InterfaceDefinition.class, responseContainer = "List")
308 @ApiResponses(value = {@ApiResponse(code = 201, message = "Delete Interface Operation from Service"),
309 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
310 @ApiResponse(code = 403, message = "Restricted operation"),
311 @ApiResponse(code = 404, message = "Service not found")})
312 public Response deleteInterfaceOperationsFromService(
313 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
314 @ApiParam(value = "Interface Id") @PathParam("interfaceId") String interfaceId,
315 @ApiParam(value = "Operation Id") @PathParam("operationId") String operationId,
316 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
317 return delete(interfaceId, operationId, serviceId, request, userId);
321 @Consumes(MediaType.APPLICATION_JSON)
322 @Produces(MediaType.APPLICATION_JSON)
323 @Path("/services/{serviceId}/interfaces/{interfaceId}/operations/{operationId}")
324 @ApiOperation(value = "Get Interface Operation from Service", httpMethod = "GET",
325 notes = "GET Interface Operation from Service", response = InterfaceDefinition.class, responseContainer = "List")
326 @ApiResponses(value = {@ApiResponse(code = 201, message = "Get Interface Operation from Service"),
327 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
328 @ApiResponse(code = 403, message = "Restricted operation"),
329 @ApiResponse(code = 404, message = "Service not found")})
330 public Response getInterfaceOperationsFromService(
331 @ApiParam(value = "Service Id") @PathParam("serviceId") String serviceId,
332 @ApiParam(value = "Interface Id") @PathParam("interfaceId") String interfaceId,
333 @ApiParam(value = "Operation Id") @PathParam("operationId") String operationId,
334 @HeaderParam(value = Constants.USER_ID_HEADER) String userId, @Context final HttpServletRequest request) {
335 return get(interfaceId, operationId, serviceId, request, userId);