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