7257ea82df739145f1aa8413f5d302f13e1f9a0d
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / AutomatedUpgradeEndpoint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 package org.openecomp.sdc.be.servlets;
21
22 import com.jcabi.aspects.Loggable;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
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.List;
34 import javax.inject.Inject;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.HeaderParam;
39 import javax.ws.rs.POST;
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.aaf.AafPermission;
47 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
48 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
49 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
50 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
51 import org.openecomp.sdc.be.dao.api.ActionStatus;
52 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.log.wrappers.Logger;
56 import org.springframework.stereotype.Controller;
57
58 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
59 @Path("/v1/catalog")
60 @Tags({@Tag(name = "SDCE-2 APIs")})
61 @Servers({@Server(url = "/sdc2/rest")})
62 @Controller
63 @Consumes(MediaType.APPLICATION_JSON)
64 @Produces(MediaType.APPLICATION_JSON)
65 public class AutomatedUpgradeEndpoint extends BeGenericServlet {
66
67     private static final Logger log = Logger.getLogger(PolicyTypesEndpoint.class);
68     private final UpgradeBusinessLogic businessLogic;
69
70     @Inject
71     public AutomatedUpgradeEndpoint(ComponentsUtils componentsUtils, UpgradeBusinessLogic businessLogic) {
72         super(componentsUtils);
73         this.businessLogic = businessLogic;
74     }
75
76     @POST
77     @Path("/{componentType}/{componentId}/automatedupgrade")
78     @Consumes(MediaType.APPLICATION_JSON)
79     @Produces(MediaType.APPLICATION_JSON)
80     @Operation(description = "Autometed upgrade", method = "POST", summary = "....", responses = {
81         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
82         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
83         @ApiResponse(responseCode = "404", description = "Component not found")})
84     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
85     public Response autometedUpgrade(@PathParam("componentType") final String componentType, @Context final HttpServletRequest request,
86                                      @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
87                                      @Parameter(description = "json describes upgrade request", required = true) String data) {
88         String url = request.getMethod() + " " + request.getRequestURI();
89         log.debug("(POST) Start handle request of {}", url);
90         try {
91             List<UpgradeRequest> inputsToUpdate = JsonParserUtils.toList(data, UpgradeRequest.class);
92             if (log.isDebugEnabled()) {
93                 log.debug("Received upgrade requests size is {}", inputsToUpdate == null ? 0 : inputsToUpdate.size());
94             }
95             UpgradeStatus actionResponse = businessLogic.automatedUpgrade(componentId, inputsToUpdate, userId);
96             return actionResponse.getStatus() == ActionStatus.OK ? buildOkResponse(actionResponse) : buildErrorResponse(actionResponse.getError());
97         } catch (Exception e) {
98             log.error("#autometedUpgrade - Exception occurred during autometed Upgrade", e);
99             throw e;
100         }
101     }
102
103     @GET
104     @Path("/{componentType}/{componentId}/dependencies")
105     @Consumes(MediaType.APPLICATION_JSON)
106     @Produces(MediaType.APPLICATION_JSON)
107     @Operation(description = "Autometed upgrade", method = "POST", summary = "....", responses = {
108         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
109         @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
110         @ApiResponse(responseCode = "404", description = "Component not found")})
111     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
112     public Response getComponentDependencies(@PathParam("componentType") final String componentType, @Context final HttpServletRequest request,
113                                              @PathParam("componentId") final String componentId,
114                                              @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
115                                              @Parameter(description = "Consumer Object to be created", required = true) List<String> data) {
116         String url = request.getMethod() + " " + request.getRequestURI();
117         log.debug("(GET) Start handle request of {}", url);
118         try {
119             return businessLogic.getComponentDependencies(componentId, userId).either(this::buildOkResponse, this::buildErrorResponse);
120         } catch (Exception e) {
121             log.error("#getServicesForComponent - Exception occurred during autometed Upgrade", e);
122             throw e;
123         }
124     }
125 }