Catalog alignment
[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
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.info.Info;
28 import io.swagger.v3.oas.annotations.media.ArraySchema;
29 import io.swagger.v3.oas.annotations.media.Content;
30 import io.swagger.v3.oas.annotations.media.Schema;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.responses.ApiResponses;
33 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
34 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
35 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
36 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
37 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.user.UserBusinessLogic;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.springframework.stereotype.Controller;
45
46 import javax.inject.Inject;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.ws.rs.Consumes;
49 import javax.ws.rs.GET;
50 import javax.ws.rs.HeaderParam;
51 import javax.ws.rs.POST;
52 import javax.ws.rs.Path;
53 import javax.ws.rs.PathParam;
54 import javax.ws.rs.Produces;
55 import javax.ws.rs.core.Context;
56 import javax.ws.rs.core.MediaType;
57 import javax.ws.rs.core.Response;
58 import java.util.List;
59
60 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
61 @Path("/v1/catalog")
62 @OpenAPIDefinition(info = @Info(title = "policy types resource"))
63 @Controller
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
66 public class AutomatedUpgradeEndpoint extends BeGenericServlet {
67     private static final Logger log = Logger.getLogger(PolicyTypesEndpoint.class);
68
69     private final UpgradeBusinessLogic businessLogic;
70
71     @Inject
72     public AutomatedUpgradeEndpoint(UserBusinessLogic userBusinessLogic,
73         ComponentsUtils componentsUtils,
74         UpgradeBusinessLogic businessLogic) {
75         super(userBusinessLogic, componentsUtils);
76         this.businessLogic = businessLogic;
77     }
78
79     @POST
80     @Path("/{componentType}/{componentId}/automatedupgrade")
81     @Consumes(MediaType.APPLICATION_JSON)
82     @Produces(MediaType.APPLICATION_JSON)
83     @Operation(description = "Autometed upgrade", method = "POST", summary = "....", responses = @ApiResponse(
84             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
85     @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Component found"),
86             @ApiResponse(responseCode = "403", description = "Restricted operation"),
87             @ApiResponse(responseCode = "404", description = "Component not found")})
88     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
89     public Response autometedUpgrade(@PathParam("componentType") final String componentType,
90             @Context final HttpServletRequest request, @PathParam("componentId") final String componentId,
91             @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
92             @Parameter(description = "json describes upgrade request", required = true) String data) {
93
94      
95         String url = request.getMethod() + " " + request.getRequestURI();
96         log.debug("(POST) Start handle request of {}", url);
97
98         try {
99             
100             List<UpgradeRequest> inputsToUpdate = JsonParserUtils.toList(data, UpgradeRequest.class);
101             
102             if (log.isDebugEnabled()) {
103                 log.debug("Received upgrade requests size is {}", inputsToUpdate == null ? 0 : inputsToUpdate.size());
104             }
105             UpgradeStatus actionResponse = businessLogic.automatedUpgrade(componentId, inputsToUpdate, userId);
106             
107             return actionResponse.getStatus() == ActionStatus.OK ? buildOkResponse(actionResponse) : buildErrorResponse(actionResponse.getError());
108
109         } catch (Exception e) {
110             log.error("#autometedUpgrade - Exception occurred during autometed Upgrade", e);
111             throw e;
112         }
113     }
114     
115     @GET
116     @Path("/{componentType}/{componentId}/dependencies")
117     @Consumes(MediaType.APPLICATION_JSON)
118     @Produces(MediaType.APPLICATION_JSON)
119     @Operation(description = "Autometed upgrade", method = "POST", summary = "....", responses = @ApiResponse(
120             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
121     @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Component found"),
122             @ApiResponse(responseCode = "403", description = "Restricted operation"),
123             @ApiResponse(responseCode = "404", description = "Component not found")})
124     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
125     public Response getComponentDependencies(@PathParam("componentType") final String componentType,
126             @Context final HttpServletRequest request, @PathParam("componentId") final String componentId,
127             @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
128             @Parameter(description = "Consumer Object to be created", required = true) List<String> data) {
129         String url = request.getMethod() + " " + request.getRequestURI();
130         log.debug("(GET) Start handle request of {}", url);
131
132         try {
133             return  businessLogic.getComponentDependencies(componentId, userId)
134                     .either(this::buildOkResponse, this::buildErrorResponse);  
135         } catch (Exception e) {
136             log.error("#getServicesForComponent - Exception occurred during autometed Upgrade", e);
137             throw e;
138         }
139      
140         
141     }
142 }