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