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