update documentation
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / distribution / servlet / DistributionCatalogServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.distribution.servlet;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.inject.Singleton;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40
41 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
45 import org.openecomp.sdc.be.servlets.BeGenericServlet;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.jcabi.aspects.Loggable;
52
53 import fj.data.Either;
54 import io.swagger.annotations.Api;
55 import io.swagger.annotations.ApiOperation;
56 import io.swagger.annotations.ApiParam;
57 import io.swagger.annotations.ApiResponse;
58 import io.swagger.annotations.ApiResponses;
59
60 /**
61  * This Servlet serves external users to download artifacts.
62  * 
63  * @author tgitelman
64  *
65  */
66
67 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
68 @Path("/v1/catalog")
69 @Api(value = "Distribution Catalog Servlet", description = "This Servlet serves external users to download artifacts.")
70 @Singleton
71 public class DistributionCatalogServlet extends BeGenericServlet {
72
73     private static final Logger log = LoggerFactory.getLogger(DistributionCatalogServlet.class);
74     @Context
75     private HttpServletRequest request;
76
77     // *******************************************************
78     // Download (GET) artifacts
79     // **********************************************************/
80     /**
81      *
82      * @param requestId
83      * @param instanceIdHeader
84      * @param accept
85      * @param authorization
86      * @param serviceName
87      * @param serviceVersion
88      * @param artifactName
89      * @return
90      */
91     @GET
92     @Path("/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}")
93     @Consumes(MediaType.APPLICATION_JSON)
94     @Produces(MediaType.APPLICATION_OCTET_STREAM)
95     @ApiOperation(value = "Download service artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
96     @ApiResponses(value = {
97             @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
98             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
99             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
100             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
101             @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
102             @ApiResponse(code = 404, message = "Specified Service Version is  not  found - SVC4504"),
103             @ApiResponse(code = 404, message = "Specified artifact is  not found - SVC4505"),
104             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
105             @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
106     public Response downloadServiceArtifact(
107             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
108             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
109             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
110             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
111             @PathParam("serviceName") final String serviceName,
112             @PathParam("serviceVersion") final String serviceVersion,
113             @PathParam("artifactName") final String artifactName) {
114
115         Response response = null;
116         String requestURI = request.getRequestURI();
117         if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
118             log.debug("Missing X-ECOMP-InstanceID header");
119             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
120             getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
121             return buildErrorResponse(responseFormat);
122         }
123
124         try {
125             ServletContext context = request.getSession().getServletContext();
126             ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
127             Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName);
128             if (downloadRsrcArtifactEither.isRight()) {
129                 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
130                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
131                 response = buildErrorResponse(responseFormat);
132             } else {
133                 byte[] value = downloadRsrcArtifactEither.left().value();
134                 InputStream is = new ByteArrayInputStream(value);
135
136                 Map<String, String> headers = new HashMap<>();
137                 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
138                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
139                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
140                 response = buildOkResponse(responseFormat, is, headers);
141             }
142             return response;
143
144         } catch (Exception e) {
145             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API");
146             log.debug("download artifact failed with exception", e);
147             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
148         }
149     }
150
151     /**
152      *
153      * @param requestId
154      * @param instanceIdHeader
155      * @param accept
156      * @param authorization
157      * @param serviceName
158      * @param serviceVersion
159      * @param resourceName
160      * @param resourceVersion
161      * @param artifactName
162      * @return
163      */
164     @GET
165     @Path("/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
166     @Consumes(MediaType.APPLICATION_JSON)
167     @Produces(MediaType.APPLICATION_OCTET_STREAM)
168     @ApiOperation(value = "Download resource artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
169     @ApiResponses(value = {
170             @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
171             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
172             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
173             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
174             @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
175             @ApiResponse(code = 404, message = "Specified Resource Instance  is not found - SVC4526"),
176             @ApiResponse(code = 404, message = "Specified Service Version is  not  found - SVC4504"),
177             @ApiResponse(code = 404, message = "Specified artifact is  not found - SVC4505"),
178             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
179             @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
180     public Response downloadResourceArtifact(
181             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
182             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
183             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
184             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
185             @PathParam("serviceName") final String serviceName,
186             @PathParam("serviceVersion") final String serviceVersion,
187             @PathParam("resourceName") final String resourceName,
188             @PathParam("resourceVersion") final String resourceVersion,
189             @PathParam("artifactName") final String artifactName) {
190
191         Response response = null;
192         String requestURI = request.getRequestURI();
193
194         if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
195             log.debug("Missing X-ECOMP-InstanceID header");
196             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
197             getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
198             return buildErrorResponse(responseFormat);
199         }
200
201         try {
202             ServletContext context = request.getSession().getServletContext();
203             ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
204             Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, resourceVersion, artifactName);
205             if (downloadRsrcArtifactEither.isRight()) {
206                 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
207                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
208                 response = buildErrorResponse(responseFormat);
209             } else {
210                 byte[] value = downloadRsrcArtifactEither.left().value();
211                 // Returning 64-encoded as it was received during upload
212                 InputStream is = new ByteArrayInputStream(value);
213                 Map<String, String> headers = new HashMap<>();
214                 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
215                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
216                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
217                 response = buildOkResponse(responseFormat, is, headers);
218             }
219             return response;
220
221         } catch (Exception e) {
222             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
223             log.debug("download artifact failed with exception", e);
224             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
225         }
226     }
227
228     /**
229      *
230      * @param requestId
231      * @param instanceIdHeader
232      * @param accept
233      * @param authorization
234      * @param serviceName
235      * @param serviceVersion
236      * @param resourceInstanceName
237      * @param artifactName
238      * @return
239      */
240     @GET
241     @Path("/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}")
242     @Consumes(MediaType.APPLICATION_JSON)
243     @Produces(MediaType.APPLICATION_OCTET_STREAM)
244     @ApiOperation(value = "Download resource instance artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
245     @ApiResponses(value = {
246             @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
247             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
248             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
249             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
250             @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
251             @ApiResponse(code = 404, message = "Specified Resource Instance  is not found - SVC4526"),
252             @ApiResponse(code = 404, message = "Specified Service Version is  not  found - SVC4504"),
253             @ApiResponse(code = 404, message = "Specified artifact is  not found - SVC4505"),
254             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
255             @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
256     public Response downloadResourceInstanceArtifactByName(
257             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
258             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
259             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
260             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
261             @PathParam("serviceName") final String serviceName,
262             @PathParam("serviceVersion") final String serviceVersion,
263             @PathParam("resourceInstanceName") final String resourceInstanceName,
264             @PathParam("artifactName") final String artifactName) {
265
266         Response response = null;
267         String requestURI = request.getRequestURI();
268
269         if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
270             log.debug("Missing X-ECOMP-InstanceID header");
271             ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
272             getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
273             return buildErrorResponse(responseFormat);
274         }
275
276         try {
277             ServletContext context = request.getSession().getServletContext();
278             ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
279             Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, artifactName);
280             if (downloadRsrcArtifactEither.isRight()) {
281                 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
282                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
283                 response = buildErrorResponse(responseFormat);
284             } else {
285                 byte[] value = downloadRsrcArtifactEither.left().value();
286                 // Returning 64-encoded as it was received during upload
287                 InputStream is = new ByteArrayInputStream(value);
288                 Map<String, String> headers = new HashMap<>();
289                 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
290                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
291                 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
292                 response = buildOkResponse(responseFormat, is, headers);
293             }
294             return response;
295
296         } catch (Exception e) {
297             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
298             log.debug("download artifact failed with exception", e);
299             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
300         }
301     }
302 }