catalog-be servlets refactoring
[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.annotations.*;
25 import javax.inject.Inject;
26 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
28 import org.openecomp.sdc.be.components.upgrade.UpgradeBusinessLogic;
29 import org.openecomp.sdc.be.components.upgrade.UpgradeRequest;
30 import org.openecomp.sdc.be.components.upgrade.UpgradeStatus;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.user.UserBusinessLogic;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.springframework.stereotype.Controller;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.*;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import java.util.List;
46
47 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
48 @Path("/v1/catalog")
49 @Api(value = "policy types resource")
50 @Controller
51 @Consumes(MediaType.APPLICATION_JSON)
52 @Produces(MediaType.APPLICATION_JSON)
53 public class AutomatedUpgradeEndpoint extends BeGenericServlet {
54     private static final Logger log = Logger.getLogger(PolicyTypesEndpoint.class);
55
56     private final UpgradeBusinessLogic businessLogic;
57
58     @Inject
59     public AutomatedUpgradeEndpoint(UserBusinessLogic userBusinessLogic,
60         ComponentsUtils componentsUtils,
61         UpgradeBusinessLogic businessLogic) {
62         super(userBusinessLogic, componentsUtils);
63         this.businessLogic = businessLogic;
64     }
65
66     @POST
67     @Path("/{componentType}/{componentId}/automatedupgrade")
68     @Consumes(MediaType.APPLICATION_JSON)
69     @Produces(MediaType.APPLICATION_JSON)
70     @ApiOperation(value = "Autometed upgrade", httpMethod = "POST", notes = "....", response = Resource.class)
71     @ApiResponses(value = { @ApiResponse(code = 200, message = "Component found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component not found") })
72     public Response autometedUpgrade(@PathParam("componentType") final String componentType, @Context final HttpServletRequest request, @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
73             @ApiParam(value = "json describes upgrade request", required = true) String data) {
74
75      
76         String url = request.getMethod() + " " + request.getRequestURI();
77         log.debug("(POST) Start handle request of {}", url);
78
79         try {
80             
81             List<UpgradeRequest> inputsToUpdate = JsonParserUtils.toList(data, UpgradeRequest.class);
82             
83             if (log.isDebugEnabled()) {
84                 log.debug("Received upgrade requests size is {}", inputsToUpdate == null ? 0 : inputsToUpdate.size());
85             }
86             UpgradeStatus actionResponse = businessLogic.automatedUpgrade(componentId, inputsToUpdate, userId);
87             
88             return actionResponse.getStatus() == ActionStatus.OK ? buildOkResponse(actionResponse) : buildErrorResponse(actionResponse.getError());
89
90         } catch (Exception e) {
91             log.error("#autometedUpgrade - Exception occurred during autometed Upgrade", e);
92              return buildGeneralErrorResponse();
93         }
94     }
95     
96     @GET
97     @Path("/{componentType}/{componentId}/dependencies")
98     @Consumes(MediaType.APPLICATION_JSON)
99     @Produces(MediaType.APPLICATION_JSON)
100     @ApiOperation(value = "Autometed upgrade", httpMethod = "POST", notes = "....", response = Resource.class)
101     @ApiResponses(value = { @ApiResponse(code = 200, message = "Component found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Component not found") })
102     public Response getComponentDependencies(@PathParam("componentType") final String componentType, @Context final HttpServletRequest request, @PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
103             @ApiParam(value = "Consumer Object to be created", required = true) List<String> data) {
104         String url = request.getMethod() + " " + request.getRequestURI();
105         log.debug("(GET) Start handle request of {}", url);
106
107         try {
108             return  businessLogic.getComponentDependencies(componentId, userId)
109                     .either(this::buildOkResponse, this::buildErrorResponse);  
110         } catch (Exception e) {
111             log.error("#getServicesForComponent - Exception occurred during autometed Upgrade", e);
112             return buildGeneralErrorResponse();
113         }
114      
115         
116     }
117 }