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