Toggle
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / TogglingServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.media.ArraySchema;
26 import io.swagger.v3.oas.annotations.media.Content;
27 import io.swagger.v3.oas.annotations.media.Schema;
28 import io.swagger.v3.oas.annotations.responses.ApiResponse;
29 import io.swagger.v3.oas.annotations.servers.Server;
30 import io.swagger.v3.oas.annotations.servers.Servers;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import io.swagger.v3.oas.annotations.tags.Tags;
33 import java.util.Map;
34 import javax.inject.Inject;
35 import javax.inject.Singleton;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.PUT;
40 import javax.ws.rs.Path;
41 import javax.ws.rs.PathParam;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
48 import org.openecomp.sdc.be.components.impl.TogglingBusinessLogic;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.impl.ServletUtils;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54
55 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
56 @Path("/v1/catalog/toggle")
57 @Tags({@Tag(name = "SDC Internal APIs")})
58 @Servers({@Server(url = "/sdc2/rest")})
59 @Singleton
60 public class TogglingServlet extends AbstractValidationsServlet {
61
62     private final TogglingBusinessLogic togglingBusinessLogic;
63
64     @Inject
65     public TogglingServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL,
66             ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
67             TogglingBusinessLogic togglingBusinessLogic) {
68         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
69         this.togglingBusinessLogic = togglingBusinessLogic;
70     }
71
72     private static final Logger log = Logger.getLogger(TogglingServlet.class);
73     private static final String ALL_FEATURES_STATES_WERE_SET_SUCCESSFULLY = "All features states were set successfully";
74     private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
75     private static final String FEATURE_STATE_WAS_UPDATED_SUCCESSFULLY = "Feature state was updated successfully";
76
77
78     @GET
79     @Path("/")
80     @Consumes(MediaType.APPLICATION_JSON)
81     @Produces(MediaType.APPLICATION_JSON)
82     @Operation(description = "Get all Toggleable features", method = "GET", summary = "Returns list of toggleable features", responses = {
83             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = javax.ws.rs.core.Response.class)))),
84             @ApiResponse(responseCode = "200", description = "Success"),
85             @ApiResponse(responseCode = "403", description = "Restricted operation"),
86             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
87             @ApiResponse(responseCode = "404", description = "Toggleable features not found")})
88     public Response getAllFeatures(@Context final HttpServletRequest request) {
89         String url = request.getMethod() + " " + request.getRequestURI();
90         log.debug(START_HANDLE_REQUEST_OF, url);
91
92         try {
93             Map<String, Boolean> features = togglingBusinessLogic.getAllFeatureStates();
94             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), features);
95         } catch (Exception e) {
96             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
97         }
98     }
99
100     @GET
101     @Path("/{featureName}/state")
102     @Consumes(MediaType.APPLICATION_JSON)
103     @Produces(MediaType.APPLICATION_JSON)
104     @Operation(description = "Get Toggleable feature state", method = "GET", summary = "Returns one toggleable feature state", responses = {
105             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = javax.ws.rs.core.Response.class)))),
106             @ApiResponse(responseCode = "200", description = "Success"),
107             @ApiResponse(responseCode = "403", description = "Restricted operation"),
108             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
109             @ApiResponse(responseCode = "404", description = "Toggleable feature not found")})
110     public Response getToggleableFeature(@PathParam("featureName") String featureName, @Context final HttpServletRequest request) {
111         String url = request.getMethod() + " " + request.getRequestURI();
112         log.debug(START_HANDLE_REQUEST_OF, url);
113
114         try {
115             boolean featureState = togglingBusinessLogic.getFeatureState(featureName);
116             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), featureState);
117         } catch (Exception e) {
118             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
119         }
120     }
121
122     @PUT
123     @Path("/state/{state}")
124     @Consumes(MediaType.APPLICATION_JSON)
125     @Produces(MediaType.APPLICATION_JSON)
126     @Operation(description = "Update all feature toggle state", method = "PUT", summary = "Update all feature status", responses = {
127             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = javax.ws.rs.core.Response.class)))),
128             @ApiResponse(responseCode = "200", description = "Success"),
129             @ApiResponse(responseCode = "403", description = "Restricted operation"),
130             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
131             @ApiResponse(responseCode = "404", description = "Toggleable features not found")})
132     public Response setAllFeatures(@PathParam("state") boolean state, @Context final HttpServletRequest request) {
133         String url = request.getMethod() + " " + request.getRequestURI();
134         log.debug(START_HANDLE_REQUEST_OF, url);
135
136         try {
137             togglingBusinessLogic.setAllFeatures(state);
138             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), ALL_FEATURES_STATES_WERE_SET_SUCCESSFULLY);
139         } catch (Exception e) {
140             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
141         }
142     }
143
144     @PUT
145     @Path("/{featureName}/state/{state}")
146     @Consumes(MediaType.APPLICATION_JSON)
147     @Produces(MediaType.APPLICATION_JSON)
148     @Operation(description = "Update feature toggle state", method = "PUT", summary = "Update feature status", responses = {
149             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = javax.ws.rs.core.Response.class)))),
150             @ApiResponse(responseCode = "200", description = "Success"),
151             @ApiResponse(responseCode = "403", description = "Restricted operation"),
152             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
153             @ApiResponse(responseCode = "404", description = "Toggleable features not found")})
154     public Response updateFeatureState(@PathParam("featureName") String featureName,
155             @PathParam("state") boolean state, @Context final HttpServletRequest request) {
156         String url = request.getMethod() + " " + request.getRequestURI();
157         log.debug("(get) Start handle request of {}", url);
158
159         try {
160             togglingBusinessLogic.updateFeatureState(featureName, state);
161             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), FEATURE_STATE_WAS_UPDATED_SUCCESSFULLY);
162         } catch (Exception e) {
163             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
164         }
165     }
166
167
168 }