Disable locking during deployment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / LockServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.servlets;
20
21 import java.util.Arrays;
22
23 import javax.inject.Inject;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.HeaderParam;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33
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.components.validation.UserValidations;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
39 import org.openecomp.sdc.be.user.Role;
40 import org.openecomp.sdc.be.user.UserBusinessLogic;
41 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.stereotype.Controller;
44
45 import com.jcabi.aspects.Loggable;
46
47 import io.swagger.v3.oas.annotations.Operation;
48 import io.swagger.v3.oas.annotations.Parameter;
49 import io.swagger.v3.oas.annotations.media.Content;
50 import io.swagger.v3.oas.annotations.media.Schema;
51 import io.swagger.v3.oas.annotations.responses.ApiResponse;
52 import io.swagger.v3.oas.annotations.servers.Server;
53 import io.swagger.v3.oas.annotations.tags.Tag;
54
55
56 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
57 @Path("/v1/catalog")
58 @Tag(name = "SDCE-2 APIs")
59 @Server(url = "/sdc2/rest")
60 @Controller
61 public class LockServlet extends BeGenericServlet {
62
63     private static final Logger log = Logger.getLogger(LockServlet.class);
64     private final UserValidations userValidations;
65     private final IGraphLockOperation graphLockOperation;
66
67     @Inject
68     public LockServlet(final UserBusinessLogic userBusinessLogic, final ComponentsUtils componentsUtils,
69             final UserValidations userValidations, IGraphLockOperation graphLockOperation) {
70         super(userBusinessLogic, componentsUtils);
71         this.userValidations = userValidations;
72         this.graphLockOperation = graphLockOperation;
73     }
74
75     @POST
76     @Path("/lock")
77     @Consumes(MediaType.APPLICATION_JSON)
78     @Produces(MediaType.APPLICATION_JSON)
79     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
80     @Operation(description = "Toggle disable locking", method = "POST", responses = {
81         @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))),
82         @ApiResponse(responseCode = "200", description = "Disable locking successfully updated"),
83         @ApiResponse(responseCode = "500", description = "Update disable locking failed")
84     })
85     public Response toggleDisableLocking(@Context final HttpServletRequest request, @HeaderParam("USER_ID") String userId,
86             @Parameter(description = "Disable Locking") boolean disable) {
87         log.info("User {} attempting to set disable locking with value {}", userId, disable);
88         userValidations.validateUserRole(userValidations.validateUserExists(userId), Arrays.asList(Role.DESIGNER, Role.ADMIN));
89         try {
90             return Response.ok().entity(graphLockOperation.disableLocking(disable)).build();
91         } catch (final Exception e) {
92             log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, LockServlet.class.getName(), "Failed to set disable locking", e);
93             return Response.serverError().build();
94         }
95     }
96 }