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