catalog-be servlets refactoring
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ArchiveEndpoint.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.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import javax.inject.Inject;
29 import org.openecomp.sdc.be.components.impl.ArchiveBusinessLogic;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.model.catalog.CatalogComponent;
32 import org.openecomp.sdc.common.api.Constants;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.RequestBody;
35
36 import javax.ws.rs.*;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43
44
45 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
46 @Path("/v1/catalog")
47 @Api(value = "Archive Endpoint")
48 @Controller
49 @Consumes(MediaType.APPLICATION_JSON)
50 @Produces(MediaType.APPLICATION_JSON)
51 public class ArchiveEndpoint {
52
53     private final ArchiveBusinessLogic archiveBusinessLogic;
54
55     @Inject
56     public ArchiveEndpoint(ArchiveBusinessLogic archiveBusinessLogic) {
57         this.archiveBusinessLogic = archiveBusinessLogic;
58     }
59
60     @POST
61     @Path("/resources/{componentId}/archive")
62     @ApiOperation(value = "Archive Resource", httpMethod = "POST", notes = "Marks a resource as archived. Can be restored with restore action", response = String.class, responseContainer = "")
63     @ApiResponses(value = {
64             @ApiResponse(code = 200, message = "Archive successful"),
65             @ApiResponse(code = 400, message = "Bad request"),
66             @ApiResponse(code = 403, message = "Restricted operation"),
67             @ApiResponse(code = 404, message = "Resource not found"),
68             @ApiResponse(code = 500, message = "Internal Error")
69     })
70     public Response archiveResources(@PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
71         archiveBusinessLogic.archiveComponent(ComponentTypeEnum.RESOURCE_PARAM_NAME, userId, componentId);
72         return Response.ok().build();
73     }
74
75     @POST
76     @Path("/resources/{componentId}/restore")
77     @ApiOperation(value = "Restore Resource", httpMethod = "POST", notes = "Restores a resource from archive.", response = String.class, responseContainer = "")
78     @ApiResponses(value = {
79             @ApiResponse(code = 200, message = "Restore successful"),
80             @ApiResponse(code = 400, message = "Bad request"),
81             @ApiResponse(code = 403, message = "Restricted operation"),
82             @ApiResponse(code = 404, message = "Resource not found"),
83             @ApiResponse(code = 500, message = "Internal Error")
84     })
85     public Response restoreResource(@PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
86         archiveBusinessLogic.restoreComponent(ComponentTypeEnum.RESOURCE_PARAM_NAME, userId, componentId);
87         return Response.ok().build();
88     }
89
90     @POST
91     @Path("/services/{componentId}/archive")
92     @ApiOperation(value = "Archive Service", httpMethod = "POST", notes = "Marks a service as archived. Can be restored with restore action", response = String.class, responseContainer = "")
93     @ApiResponses(value = {
94             @ApiResponse(code = 200, message = "Archive successful"),
95             @ApiResponse(code = 400, message = "Bad request"),
96             @ApiResponse(code = 403, message = "Restricted operation"),
97             @ApiResponse(code = 404, message = "Service not found"),
98             @ApiResponse(code = 500, message = "Internal Error")
99     })
100     public Response archiveService(@PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
101         archiveBusinessLogic.archiveComponent(ComponentTypeEnum.SERVICE_PARAM_NAME, userId, componentId);
102         return Response.ok().build();
103     }
104
105
106     @POST
107     @Path("/services/{componentId}/restore")
108     @ApiOperation(value = "Restore Service", httpMethod = "POST", notes = "Restores a service from archive.", response = String.class, responseContainer = "")
109     @ApiResponses(value = {
110             @ApiResponse(code = 200, message = "Restore successful"),
111             @ApiResponse(code = 400, message = "Bad request"),
112             @ApiResponse(code = 403, message = "Restricted operation"),
113             @ApiResponse(code = 404, message = "Service not found"),
114             @ApiResponse(code = 500, message = "Internal Error")
115     })
116     public Response restoreService(@PathParam("componentId") final String componentId, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
117         archiveBusinessLogic.restoreComponent(ComponentTypeEnum.SERVICE_PARAM_NAME, userId, componentId);
118         return Response.ok().build();
119     }
120
121     @GET
122     @Path("/archive")
123     @ApiOperation(value = "Get all Archived Components", httpMethod = "GET", notes = "Get all Archived Components", response = String.class, responseContainer = "")
124     @ApiResponses(value = {
125             @ApiResponse(code = 200, message = "Success"),
126             @ApiResponse(code = 400, message = "Bad request"),
127             @ApiResponse(code = 403, message = "Restricted operation"),
128             @ApiResponse(code = 500, message = "Internal Error")
129     })
130     public Map<String, List<CatalogComponent>> getArchivedComponents(@HeaderParam(value = Constants.USER_ID_HEADER) String userId){
131         return this.archiveBusinessLogic.getArchiveComponents(userId, new LinkedList<>());
132     }
133
134     @POST
135     @Path("/notif/vsp/archived")
136     @ApiOperation(value = "Notify about an archived VSP. All VFs with relation to the given CSAR IDs will be martked as vspArchived=true", httpMethod = "POST")
137     @ApiResponses(value = {
138             @ApiResponse(code = 200, message = "Success"),
139             @ApiResponse(code = 400, message = "Bad request"),
140             @ApiResponse(code = 403, message = "Restricted operation"),
141             @ApiResponse(code = 500, message = "Internal Error. A list of the failed CSAR IDs may be returned.")
142     })
143     public Response onVspArchived(@HeaderParam(value = Constants.USER_ID_HEADER) String userId, @RequestBody List<String> csarIds){
144         List<String> failedCsarIds = this.archiveBusinessLogic.onVspArchive(userId, csarIds);
145         if (!failedCsarIds.isEmpty()){
146             //There are some failed CSAR IDs, return 500 and the list of failed CSAR IDs
147             Map<String, List<String>> entity = new HashMap<>();
148             entity.put("failedIds", failedCsarIds);
149             return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
150                     .entity(entity)
151                     .build();
152         }
153         return Response.ok().build();
154     }
155
156     @POST
157     @Path("/notif/vsp/restored")
158     @ApiOperation(value = "Notify about a restored VSP. All VFs with relation to the given CSAR IDs will be martked as vspArchived=false", httpMethod = "POST")
159     @ApiResponses(value = {
160             @ApiResponse(code = 200, message = "Success"),
161             @ApiResponse(code = 400, message = "Bad request"),
162             @ApiResponse(code = 403, message = "Restricted operation"),
163             @ApiResponse(code = 500, message = "Internal Error. A list of the failed CSAR IDs may be returned.")
164     })
165     public Response onVspRestored(@HeaderParam(value = Constants.USER_ID_HEADER) String userId, @RequestBody List<String> csarIds){
166         List<String> failedCsarIds = this.archiveBusinessLogic.onVspRestore(userId, csarIds);
167         if (!failedCsarIds.isEmpty()){
168             //There are some failed CSAR IDs, return 500 and the list of failed CSAR IDs
169             Map<String, List<String>> entity = new HashMap<>();
170             entity.put("failedIds", failedCsarIds);
171             return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
172                     .entity(entity)
173                     .build();
174         }
175         return Response.ok().build();
176     }
177
178 }