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