re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / externalapi / servlet / ArtifactExternalServlet.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.externalapi.servlet;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiImplicitParam;
27 import io.swagger.annotations.ApiImplicitParams;
28 import io.swagger.annotations.ApiOperation;
29 import io.swagger.annotations.ApiParam;
30 import io.swagger.annotations.ApiResponse;
31 import io.swagger.annotations.ApiResponses;
32 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
34 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.model.ArtifactDefinition;
39 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
40 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
41 import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
42 import org.openecomp.sdc.be.servlets.AbstractValidationsServlet;
43 import org.openecomp.sdc.be.servlets.RepresentationUtils;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.common.datastructure.Wrapper;
46 import org.openecomp.sdc.common.util.GeneralUtility;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import javax.inject.Singleton;
52 import javax.servlet.ServletContext;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.ws.rs.DELETE;
55 import javax.ws.rs.GET;
56 import javax.ws.rs.HeaderParam;
57 import javax.ws.rs.POST;
58 import javax.ws.rs.Path;
59 import javax.ws.rs.PathParam;
60 import javax.ws.rs.Produces;
61 import javax.ws.rs.core.Context;
62 import javax.ws.rs.core.HttpHeaders;
63 import javax.ws.rs.core.MediaType;
64 import javax.ws.rs.core.Response;
65 import java.io.ByteArrayInputStream;
66 import java.io.IOException;
67 import java.io.InputStream;
68 import java.util.EnumMap;
69 import java.util.HashMap;
70 import java.util.Map;
71 /**
72  * This Servlet serves external users operations on artifacts.
73  * 
74  * @author mshitrit
75  *
76  */
77 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
78 @Path("/v1/catalog")
79 @Api(value = "Artifact External Servlet", description = "Servlet serves external users operations on artifacts.")
80 @Singleton
81 public class ArtifactExternalServlet extends AbstractValidationsServlet {
82
83     @Context
84     private HttpServletRequest request;
85
86     private static final Logger log = LoggerFactory.getLogger(ArtifactExternalServlet.class);
87
88     private static String startLog = "Start handle request of ";
89
90     @POST
91     @Path("/resources/{uuid}/interfaces/{operationUUID}/artifacts/{artifactUUID}")
92     @Produces(MediaType.APPLICATION_JSON)
93     @ApiOperation(value = "uploads of artifact to VF operation workflow", httpMethod = "POST", notes = "uploads of artifact to VF operation workflow")
94     @ApiResponses(value = {
95             @ApiResponse(code = 200, message = "Artifact uploaded", response = ArtifactDefinition.class),
96             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
97             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
98             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
99             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
100             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
101             @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"),
102             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
103             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
104             @ApiResponse(code = 400, message = "Artifact name given in input already exists in the context of the asset - SVC4125"),
105             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
106             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
107             @ApiResponse(code = 400, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
108             @ApiResponse(code = 400, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
109     @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.model.ArtifactDefinition", paramType = "body", value = "json describe the artifact")})
110     public Response uploadInterfaceOperationArtifact(
111             @ApiParam(value = "Determines the format of the body of the request", required = true) @HeaderParam(value = HttpHeaders.CONTENT_TYPE) String contenType,
112             @ApiParam(value = "The value for this header must be the MD5 checksum over the whole json body", required = true) @HeaderParam(value = Constants.MD5_HEADER) String checksum,
113             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true) @HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
114             @ApiParam(value = "X-ECOMP-RequestID header", required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
115             @ApiParam(value = "X-ECOMP-InstanceID header", required = true) @HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
116             @ApiParam(value = "Determines the format of the body of the response", required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
117             @ApiParam(value = "The username and password", required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
118             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
119             @ApiParam(value = "The uuid of the operation", required = true)@PathParam("operationUUID") final String operationUUID,
120             @ApiParam(value = "The uuid of the artifact", required = true)@PathParam("artifactUUID") final String artifactUUID,
121             @ApiParam( hidden = true) String data) {
122         Wrapper<Response> responseWrapper = new Wrapper<>();
123         ResponseFormat responseFormat = null;
124         String requestURI = request.getRequestURI();
125         String url = request.getMethod() + " " + requestURI;
126         log.debug("{} {}", startLog, url);
127         ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
128         String componentTypeValue = componentType.getValue();
129         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentTypeValue);
130         ArtifactDefinition artifactDefinition = null;
131
132         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
133             log.debug("updateArtifact: Missing X-ECOMP-InstanceID header");
134             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
135             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
136         }
137         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
138             log.debug("updateArtifact: Missing USER_ID");
139             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
140             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
141         }
142         try {
143             if (responseWrapper.isEmpty()) {
144                 ServletContext context = request.getSession().getServletContext();
145                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
146                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnInterfaceOperationByResourceUUID(data, request, componentType, uuid, artifactUUID, operationUUID,
147                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
148                 if (uploadArtifactEither.isRight()) {
149                     log.debug("failed to update artifact");
150                     responseFormat = uploadArtifactEither.right().value();
151                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
152                 } else {
153                     artifactDefinition=uploadArtifactEither.left().value();
154                     Object representation = RepresentationUtils.toRepresentation(artifactDefinition);
155                     Map<String, String> headers = new HashMap<>();
156                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
157                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
158                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
159                 }
160             }
161         } catch (Exception e) {
162             final String message = "failed to update artifact on a resource or service";
163             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
164             log.debug(message, e);
165             responseWrapper.setInnerElement(buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)));
166         } finally {
167             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_UPLOAD_BY_API,
168                     resourceCommonInfo, request, artifactDefinition, null);
169         }
170         return responseWrapper.getInnerElement();
171     }
172
173     /**
174      * Uploads an artifact to resource or service
175      *
176      * @param contenType
177      * @param checksum
178      * @param userId
179      * @param requestId
180      * @param instanceIdHeader
181      * @param accept
182      * @param authorization
183      * @param assetType
184      * @param uuid
185      * @param data
186      * @return
187      */
188     @POST
189     @Path("/{assetType}/{uuid}/artifacts")
190     @Produces(MediaType.APPLICATION_JSON)
191     @ApiOperation(value = "uploads of artifact to a resource or service", httpMethod = "POST", notes = "uploads of artifact to a resource or service")
192     @ApiResponses(value = {
193             @ApiResponse(code = 200, message = "Artifact uploaded", response = ArtifactDefinition.class),
194             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
195             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
196             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
197             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
198             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
199             @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"),
200             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
201             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
202             @ApiResponse(code = 400, message = "Artifact name given in input already exists in the context of the asset - SVC4125"),
203             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
204             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
205             @ApiResponse(code = 400, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
206             @ApiResponse(code = 400, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
207     @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.model.ArtifactDefinition", paramType = "body", value = "json describe the artifact")})
208     public Response uploadArtifact(
209             @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
210             @ApiParam(value = "The value for this header must be the MD5 checksum over the whole json body", required = true)@HeaderParam(value = Constants.MD5_HEADER) String checksum,
211             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
212             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
213             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
214             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
215             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
216             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
217             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
218             @ApiParam( hidden = true) String data) {
219
220         init();
221
222         Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
223         String requestURI = request.getRequestURI();
224         String url = request.getMethod() + " " + requestURI;
225         log.debug("{} {}", startLog, url);
226         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
227         String componentTypeValue = componentType == null ? null : componentType.getValue();
228         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentTypeValue);
229
230         if (componentType == null) {
231             log.debug("uploadArtifact: assetType parameter {} is not valid", assetType);
232             responseWrapper.setInnerElement(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
233         }
234         if (responseWrapper.isEmpty()) {
235             validateXECOMPInstanceIDHeader(instanceIdHeader, responseWrapper);
236         }
237         if (responseWrapper.isEmpty() ) {
238             validateHttpCspUserIdHeader(userId, responseWrapper);
239         }
240         Response response = null;
241         ArtifactDefinition artifactDefinition = null;
242         try {
243             if (responseWrapper.isEmpty()) {
244                 ServletContext context = request.getSession().getServletContext();
245                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
246                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.uploadArtifactToComponentByUUID(data, request, componentType, uuid,
247                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.CREATE));
248                 if (uploadArtifactEither.isRight()) {
249                     log.debug("failed to upload artifact");
250                     responseWrapper.setInnerElement(uploadArtifactEither.right().value());
251                 } else {
252                     artifactDefinition = uploadArtifactEither.left().value();
253                     Object representation = RepresentationUtils.toRepresentation(artifactDefinition);
254                     Map<String, String> headers = new HashMap<>();
255                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
256                     responseWrapper.setInnerElement(getComponentsUtils().getResponseFormat(ActionStatus.OK));
257                     response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers);
258                 }
259             }
260         } catch (IOException e) {
261             final String message = "failed to upload artifact to a resource or service";
262             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
263             log.debug(message, e);
264             responseWrapper.setInnerElement(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
265             response = buildErrorResponse(responseWrapper.getInnerElement());
266         }   catch (ComponentException e){
267             responseWrapper.setInnerElement(getComponentsUtils().getResponseFormat(e));
268         }finally {
269             if( response == null ){
270                 response = buildErrorResponse(responseWrapper.getInnerElement());
271             }
272             getComponentsUtils().auditExternalCrudApi(responseWrapper.getInnerElement(), AuditingActionEnum.ARTIFACT_UPLOAD_BY_API,
273                     resourceCommonInfo, request, artifactDefinition, null);
274         }
275         return response;
276     }
277
278     /**
279      * Uploads an artifact to resource instance
280      *
281      * @param assetType
282      * @param uuid
283      * @param resourceInstanceName
284      * @return
285      */
286     @POST
287     @Path("/{assetType}/{uuid}/resourceInstances/{resourceInstanceName}/artifacts")
288     @Produces(MediaType.APPLICATION_JSON)
289     @ApiOperation(value = "uploads an artifact to a resource instance", httpMethod = "POST", notes = "uploads an artifact to a resource instance")
290     @ApiResponses(value = {
291             @ApiResponse(code = 200, message = "Artifact uploaded", response = ArtifactDefinition.class),
292             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
293             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
294             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
295             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
296             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
297             @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"),
298             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
299             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
300             @ApiResponse(code = 400, message = "Artifact name given in input already exists in the context of the asset - SVC4125"),
301             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
302             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
303             @ApiResponse(code = 400, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
304             @ApiResponse(code = 400, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
305     @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.model.ArtifactDefinition", paramType = "body", value = "json describe the artifact")})
306     public Response uploadArtifactToInstance(
307             @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
308             @ApiParam(value = "The value for this header must be the MD5 checksum over the whole json body", required = true)@HeaderParam(value = Constants.MD5_HEADER) String checksum,
309             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
310             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
311             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
312             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
313             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
314             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
315             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
316             @ApiParam(value = "The component instance name (as publishedin the response of the detailed query)", required = true)@PathParam("resourceInstanceName") final String resourceInstanceName,
317             @ApiParam( hidden = true) String data) {
318
319         Wrapper<Response> responseWrapper = new Wrapper<>();
320         ResponseFormat responseFormat = null;
321         String requestURI = request.getRequestURI();
322         String url = request.getMethod() + " " + requestURI;
323         log.debug("{} {}", startLog, url);
324         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
325         String componentTypeValue = componentType == null ? null : componentType.getValue();
326         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(resourceInstanceName, componentTypeValue);
327         ArtifactDefinition artifactDefinition = null;
328
329         if (componentType == null) {
330             log.debug("uploadArtifact: assetType parameter {} is not valid", assetType);
331             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
332             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
333         }
334         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
335             log.debug("uploadArtifact: Missing X-ECOMP-InstanceID header");
336             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
337             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
338         }
339         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
340             log.debug("uploadArtifact: Missing USER_ID header");
341             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
342             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
343         }
344         try {
345             if (responseWrapper.isEmpty()) {
346                 ServletContext context = request.getSession().getServletContext();
347                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
348                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.uploadArtifactToRiByUUID(data, request, componentType, uuid, resourceInstanceName,
349                         artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.CREATE));
350                 if (uploadArtifactEither.isRight()) {
351                     log.debug("failed to upload artifact");
352                     responseFormat = uploadArtifactEither.right().value();
353                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
354                 } else {
355                     Object representation = RepresentationUtils.toRepresentation(uploadArtifactEither.left().value());
356                     Map<String, String> headers = new HashMap<>();
357                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
358                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
359                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
360                 }
361             }
362         }catch (IOException e) {
363             final String message = "failed to upload artifact to a resource instance";
364             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
365             log.debug(message, e);
366             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
367             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
368         }catch (ComponentException e){
369             responseFormat = getComponentsUtils().getResponseFormat(e);
370         }finally {
371             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_UPLOAD_BY_API,
372                     resourceCommonInfo, request, artifactDefinition, null);
373         }
374         return responseWrapper.getInnerElement();
375     }
376
377     /**
378      *
379      * @param contenType
380      * @param checksum
381      * @param userId
382      * @param requestId
383      * @param instanceIdHeader
384      * @param accept
385      * @param authorization
386      * @param assetType
387      * @param uuid
388      * @param artifactUUID
389      * @param data
390      * @return
391      */
392     @POST
393     @Path("/{assetType}/{uuid}/artifacts/{artifactUUID}")
394     @Produces(MediaType.APPLICATION_JSON)
395     @ApiOperation(value = "updates an artifact on a resource or service", httpMethod = "POST", notes = "uploads of artifact to a resource or service")
396     @ApiResponses(value = {
397             @ApiResponse(code = 200, message = "Artifact updated", response = ArtifactDefinition.class),
398             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
399             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
400             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
401             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
402             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
403             @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"),
404             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
405             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
406             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
407             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
408             @ApiResponse(code = 403, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
409             @ApiResponse(code = 409, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
410     @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.model.ArtifactDefinition", paramType = "body", value = "json describe the artifact")})
411     public Response updateArtifact(
412             @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
413             @ApiParam(value = "The value for this header must be the MD5 checksum over the whole json body", required = true)@HeaderParam(value = Constants.MD5_HEADER) String checksum,
414             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
415             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
416             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
417             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
418             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
419             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
420             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
421             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID,
422             @ApiParam(hidden = true) String data) {
423
424         Wrapper<Response> responseWrapper = new Wrapper<>();
425         ResponseFormat responseFormat = null;
426         String requestURI = request.getRequestURI();
427         String url = request.getMethod() + " " + requestURI;
428         log.debug("{} {}", startLog, url);
429         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
430         String componentTypeValue = componentType == null ? null : componentType.getValue();
431         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentTypeValue);
432         if (componentType == null) {
433             log.debug("updateArtifact: assetType parameter {} is not valid", assetType);
434             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
435             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
436         }
437         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
438             log.debug("updateArtifact: Missing X-ECOMP-InstanceID header");
439             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
440             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
441         }
442         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
443             log.debug("updateArtifact: Missing USER_ID");
444             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
445             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
446         }
447         ArtifactDefinition artifactDefinition = null;
448         try {
449             if (responseWrapper.isEmpty()) {
450                 ServletContext context = request.getSession().getServletContext();
451                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
452                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnComponentByUUID(data, request, componentType, uuid, artifactUUID,
453                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
454                 if (uploadArtifactEither.isRight()) {
455                     log.debug("failed to update artifact");
456                     responseFormat = uploadArtifactEither.right().value();
457                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
458                 } else {
459                     Object representation = RepresentationUtils.toRepresentation(uploadArtifactEither.left().value());
460                     Map<String, String> headers = new HashMap<>();
461                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
462                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
463                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
464                 }
465             }
466         } catch (IOException e) {
467             final String message = "failed to update artifact on a resource or service";
468             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
469             log.debug(message, e);
470             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
471             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
472         }  catch (ComponentException e){
473             responseFormat = getComponentsUtils().getResponseFormat(e);
474         }
475         finally{
476             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_UPDATE_BY_API, resourceCommonInfo,
477                     request, artifactDefinition, artifactUUID);
478         }
479         return responseWrapper.getInnerElement();
480     }
481
482     /**
483      * updates an artifact on a resource instance
484      *
485      * @param assetType
486      * @param uuid
487      * @param resourceInstanceName
488      * @param artifactUUID
489      * @return
490      */
491     @POST
492     @Path("/{assetType}/{uuid}/resourceInstances/{resourceInstanceName}/artifacts/{artifactUUID}")
493     @Produces(MediaType.APPLICATION_JSON)
494     @ApiOperation(value = "updates an artifact on a resource instance", httpMethod = "POST", notes = "uploads of artifact to a resource or service")
495     @ApiResponses(value = {
496             @ApiResponse(code = 200, message = "Artifact updated", response = ArtifactDefinition.class),
497             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
498             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
499             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
500             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
501             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
502             @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"),
503             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
504             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
505             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
506             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
507             @ApiResponse(code = 403, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
508             @ApiResponse(code = 409, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
509     @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.model.ArtifactDefinition", paramType = "body", value = "json describe the artifact")})
510     public Response updateArtifactOnResourceInstance(
511             @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
512             @ApiParam(value = "The value for this header must be the MD5 checksum over the whole json body", required = true)@HeaderParam(value = Constants.MD5_HEADER) String checksum,
513             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
514             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
515             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
516             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
517             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
518             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
519             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
520             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID,
521             @ApiParam(value = "The component instance name (as publishedin the response of the detailed query)", required = true)@PathParam("resourceInstanceName") final String resourceInstanceName,
522             @ApiParam( hidden = true) String data) {
523
524         Wrapper<Response> responseWrapper = new Wrapper<>();
525         ResponseFormat responseFormat = null;
526         String requestURI = request.getRequestURI();
527         String url = request.getMethod() + " " + requestURI;
528         log.debug("{} {}", startLog, url);
529         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
530         String componentTypeValue = componentType == null ? null : componentType.getValue();
531         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(resourceInstanceName, componentTypeValue);
532         if (componentType == null) {
533             log.debug("updateArtifactOnResourceInstance: assetType parameter {} is not valid", assetType);
534             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
535             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
536         }
537         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
538             log.debug("updateArtifactOnResourceInstance: Missing X-ECOMP-InstanceID header");
539             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
540             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
541         }
542         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
543             log.debug("updateArtifactOnResourceInstance: Missing USER_ID header");
544             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
545             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
546         }
547         ArtifactDefinition artifactDefinition = null;
548         try {
549             if (responseWrapper.isEmpty()) {
550                 ServletContext context = request.getSession().getServletContext();
551                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
552                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnRiByUUID(data, request, componentType, uuid, resourceInstanceName, artifactUUID,
553                          artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
554                 if (uploadArtifactEither.isRight()) {
555                     log.debug("failed to update artifact");
556                     responseFormat = uploadArtifactEither.right().value();
557                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
558                 } else {
559                     Object representation = RepresentationUtils.toRepresentation(uploadArtifactEither.left().value());
560                     Map<String, String> headers = new HashMap<>();
561                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
562                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
563                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
564                 }
565             }
566         } catch (IOException e) {
567             final String message = "failed to update artifact on resource instance";
568             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
569             log.debug(message, e);
570             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
571             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
572         }  catch (ComponentException e){
573             responseFormat = getComponentsUtils().getResponseFormat(e);
574         }
575         finally{
576             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_UPDATE_BY_API, resourceCommonInfo,
577                     request, artifactDefinition, artifactUUID);
578         }
579         return responseWrapper.getInnerElement();
580     }
581
582     /**
583      * deletes an artifact of a resource or service
584      *
585      * @param assetType
586      * @param uuid
587      * @param artifactUUID
588      * @return
589      */
590     @DELETE
591     @Path("/{assetType}/{uuid}/artifacts/{artifactUUID}")
592     @Produces(MediaType.APPLICATION_JSON)
593     @ApiOperation(value = "deletes an artifact of a resource or service", httpMethod = "DELETE", notes = "deletes an artifact of a resource or service", response = Response.class)
594     @ApiResponses(value = {
595             @ApiResponse(code = 200, message = "Artifact deleted", response = ArtifactDefinition.class),
596             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
597             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
598             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
599             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
600             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
601             @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"),
602             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
603             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
604             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
605             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
606             @ApiResponse(code = 403, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
607             @ApiResponse(code = 409, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
608     public Response deleteArtifact(
609             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
610             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
611             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
612             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
613             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
614             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
615             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
616             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID) {
617
618         Wrapper<Response> responseWrapper = new Wrapper<>();
619         ResponseFormat responseFormat = null;
620         String requestURI = request.getRequestURI();
621         String url = request.getMethod() + " " + requestURI;
622         log.debug("{} {}", startLog, url);
623         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
624         String componentTypeValue = componentType == null ? null : componentType.getValue();
625         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentTypeValue);
626         ArtifactDefinition artifactDefinition = null;
627         if (componentType == null) {
628             log.debug("deleteArtifact: assetType parameter {} is not valid", assetType);
629             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
630             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
631         }
632         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
633             log.debug("deleteArtifact: Missing X-ECOMP-InstanceID header");
634             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
635             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
636         }
637         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
638             log.debug("deleteArtifact: Missing USER_ID header");
639             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
640             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
641         }
642         try {
643             if (responseWrapper.isEmpty()) {
644                 ServletContext context = request.getSession().getServletContext();
645                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
646                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.deleteArtifactOnComponentByUUID(request, componentType, uuid, artifactUUID,
647                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.DELETE));
648                 if (uploadArtifactEither.isRight()) {
649                     log.debug("failed to delete artifact");
650                     responseFormat = uploadArtifactEither.right().value();
651                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
652                 } else {
653                     Object representation = RepresentationUtils.toRepresentation(uploadArtifactEither.left().value());
654                     Map<String, String> headers = new HashMap<>();
655                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
656                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
657                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
658                 }
659             }
660         } catch (IOException e) {
661             final String message = "failed to delete an artifact of a resource or service";
662             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
663             log.debug(message, e);
664             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
665             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
666         }  catch (ComponentException e){
667             responseFormat = getComponentsUtils().getResponseFormat(e);
668         }
669         finally{
670             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_DELETE_BY_API, resourceCommonInfo,
671                     request, artifactDefinition, artifactUUID);
672         }
673         return responseWrapper.getInnerElement();
674     }
675
676     /**
677      * deletes an artifact of a resource instance
678      *
679      * @param assetType
680      * @param uuid
681      * @param resourceInstanceName
682      * @return
683      */
684     @DELETE
685     @Path("{assetType}/{uuid}/resourceInstances/{resourceInstanceName}/artifacts/{artifactUUID}")
686     @Produces(MediaType.APPLICATION_JSON)
687     @ApiOperation(value = "deletes an artifact of a resource insatnce", httpMethod = "DELETE", notes = "deletes an artifact of a resource insatnce", response = Response.class)
688     @ApiResponses(value = {
689             @ApiResponse(code = 200, message = "Artifact deleted", response = ArtifactDefinition.class),
690             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
691             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
692             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
693             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
694             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
695             @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"),
696             @ApiResponse(code = 400, message = "Invalid artifactType was defined as input - SVC4122"),
697             @ApiResponse(code = 400, message = "Artifact type (mandatory field) is missing in request - SVC4124"),
698             @ApiResponse(code = 400, message = "Invalid MD5 header - SVC4127"),
699             @ApiResponse(code = 400, message = "Artifact name is missing in input - SVC4128"),
700             @ApiResponse(code = 403, message = "Asset is being edited by different user. Only one user can checkout and edit an asset on given time. The asset will be available for checkout after the other user will checkin the asset - SVC4086"),
701             @ApiResponse(code = 409, message = "Restricted Operation – the user provided does not have role of Designer or the asset is being used by another designer - SVC4301")})
702     public Response deleteArtifactOnResourceInstance(
703             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
704             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
705             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
706             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
707             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
708             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
709             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
710             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID,
711             @ApiParam(value = "The component instance name (as publishedin the response of the detailed query)", required = true)@PathParam("resourceInstanceName") final String resourceInstanceName) {
712
713         Wrapper<Response> responseWrapper = new Wrapper<>();
714         ResponseFormat responseFormat = null;
715         String requestURI = request.getRequestURI();
716         String url = request.getMethod() + " " + requestURI;
717         log.debug("{} {}", startLog, url);
718         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
719         String componentTypeValue = componentType == null ? null : componentType.getValue();
720         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(resourceInstanceName, componentTypeValue);
721         if (componentType == null) {
722             log.debug("deleteArtifactOnResourceInsatnce: assetType parameter {} is not valid", assetType);
723             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
724             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
725         }
726         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
727             log.debug("deleteArtifactOnResourceInsatnce: Missing X-ECOMP-InstanceID header");
728             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
729             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
730         }
731         if (responseWrapper.isEmpty() && (userId == null || userId.isEmpty())) {
732             log.debug("deleteArtifactOnResourceInsatnce: Missing USER_ID header");
733             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_USER_ID);
734             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
735         }
736         ArtifactDefinition artifactDefinition = null;
737         try {
738             if (responseWrapper.isEmpty()) {
739                 ServletContext context = request.getSession().getServletContext();
740                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
741                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.deleteArtifactOnRiByUUID(request, componentType, uuid, resourceInstanceName, artifactUUID,
742                          artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.DELETE));
743                 if (uploadArtifactEither.isRight()) {
744                     log.debug("failed to delete artifact");
745                     responseFormat = uploadArtifactEither.right().value();
746                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
747                 } else {
748                     Object representation = RepresentationUtils.toRepresentation(uploadArtifactEither.left().value());
749                     Map<String, String> headers = new HashMap<>();
750                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByString((String) representation));
751                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
752                     responseWrapper.setInnerElement(buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), representation, headers));
753                 }
754             }
755         } catch (IOException e) {
756             final String message = "failed to delete an artifact of a resource instance";
757             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(message);
758             log.debug(message, e);
759             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
760             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
761         }  catch (ComponentException e){
762             responseFormat = getComponentsUtils().getResponseFormat(e);
763         }
764         finally{
765             getComponentsUtils().auditExternalCrudApi(responseFormat, AuditingActionEnum.ARTIFACT_DELETE_BY_API, resourceCommonInfo,
766                     request, artifactDefinition, artifactUUID);
767         }
768         return responseWrapper.getInnerElement();
769     }
770
771     /**
772      * downloads an artifact of a component (either a service or a resource) by artifactUUID
773      *
774      * @param assetType
775      * @param uuid
776      * @param artifactUUID
777      * @return
778      */
779     @GET
780     @Path("/{assetType}/{uuid}/artifacts/{artifactUUID}")
781     @Produces(MediaType.APPLICATION_OCTET_STREAM)
782     @ApiOperation(value = "Download component artifact", httpMethod = "GET", notes = "Returns downloaded artifact")
783     @ApiResponses(value = {
784             @ApiResponse(code = 200, message = "Artifact downloaded", response = String.class),
785             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
786             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
787             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
788             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
789             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
790             @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"),
791             @ApiResponse(code = 404, message = "Artifact was not found - SVC4505")})
792     public Response downloadComponentArtifact(
793             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
794             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
795             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
796             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
797             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
798             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
799             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
800             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID) {
801
802         Wrapper<Response> responseWrapper = new Wrapper<>();
803         ResponseFormat responseFormat = null;
804         String requestURI = request.getRequestURI();
805         String url = request.getMethod() + " " + requestURI;
806         log.debug("{} {}", startLog, url);
807         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
808         String componentTypeValue = componentType == null ? null : componentType.getValue();
809         if (componentType == null) {
810             log.debug("downloadComponentArtifact: assetType parameter {} is not valid", assetType);
811             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
812             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
813         }
814         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
815             log.debug("downloadComponentArtifact: Missing X-ECOMP-InstanceID header");
816             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
817             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
818         }
819         ResourceCommonInfo resourceCommonInfo = new ResourceCommonInfo(componentTypeValue);
820         try {
821             if (responseWrapper.isEmpty()) {
822                 ServletContext context = request.getSession().getServletContext();
823                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
824                 Either<byte[], ResponseFormat> downloadComponentArtifactEither = artifactsLogic.downloadComponentArtifactByUUIDs(componentType, uuid, artifactUUID, resourceCommonInfo);
825                 if (downloadComponentArtifactEither.isRight()) {
826                     responseFormat = downloadComponentArtifactEither.right().value();
827                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
828                 } else {
829                     byte[] value = downloadComponentArtifactEither.left().value();
830                     InputStream is = new ByteArrayInputStream(value);
831                     Map<String, String> headers = new HashMap<>();
832                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByByteArray(value));
833                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
834                     responseWrapper.setInnerElement(buildOkResponse(responseFormat, is, headers));
835                 }
836             }
837         }  catch (ComponentException e){
838             responseFormat = getComponentsUtils().getResponseFormat(e);
839         }
840         finally{
841             getComponentsUtils().auditExternalDownloadArtifact(responseFormat, resourceCommonInfo,
842                     new DistributionData(instanceIdHeader, requestURI), requestId, artifactUUID, userId);
843         }
844         return responseWrapper.getInnerElement();
845     }
846
847     /**
848      * downloads an artifact of a resource instance of a component (either a service or a resource) by artifactUUID
849      *
850      * @param assetType
851      * @param uuid
852      * @param resourceInstanceName
853      * @param artifactUUID
854      * @return
855      */
856     @GET
857     @Path("/{assetType}/{uuid}/resourceInstances/{resourceInstanceName}/artifacts/{artifactUUID}")
858     @Produces(MediaType.APPLICATION_OCTET_STREAM)
859     @ApiOperation(value = "Download resource instance artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = Response.class)
860     @ApiResponses(value = {
861             @ApiResponse(code = 200, message = "Artifact downloaded", response = String.class),
862             @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
863             @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
864             @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
865             @ApiResponse(code = 404, message = "Specified resource is not found - SVC4063"),
866             @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
867             @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"),
868             @ApiResponse(code = 404, message = "Artifact was not found - SVC4505")})
869     public Response downloadResourceInstanceArtifact(
870             @ApiParam(value = "The user ID of the DCAE Designer. This user must also have Designer role in SDC", required = true)@HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
871             @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
872             @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
873             @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
874             @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
875             @ApiParam(value = "The requested asset type", required = true, allowableValues = "resources, services")@PathParam("assetType") final String assetType,
876             @ApiParam(value = "The uuid of the asset as published in the metadata", required = true)@PathParam("uuid") final String uuid,
877             @ApiParam(value = "The uuid of the artifact as published in the asset detailed metadata or in the response of the upload / update operation", required = true)@PathParam("artifactUUID") final String artifactUUID,
878             @ApiParam(value = "The component instance name (as publishedin the response of the detailed query)", required = true)@PathParam("resourceInstanceName") final String resourceInstanceName) {
879
880         Wrapper<Response> responseWrapper = new Wrapper<>();
881         ResponseFormat responseFormat = null;
882         String requestURI = request.getRequestURI();
883         String url = request.getMethod() + " " + requestURI;
884         log.debug("{} {}", startLog, url);
885         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(assetType);
886         String componentTypeValue = componentType == null ? null : componentType.getValue();
887         if (componentType == null) {
888             log.debug("downloadResourceInstanceArtifact: assetType parameter {} is not valid", assetType);
889             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
890             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
891         }
892         if (responseWrapper.isEmpty() && (instanceIdHeader == null || instanceIdHeader.isEmpty())) {
893             log.debug("downloadResourceInstanceArtifact: Missing X-ECOMP-InstanceID header");
894             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
895             responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
896         }
897         try {
898             if (responseWrapper.isEmpty()) {
899                 ServletContext context = request.getSession().getServletContext();
900                 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
901                 Either<byte[], ResponseFormat> downloadResourceArtifactEither = artifactsLogic.downloadResourceInstanceArtifactByUUIDs(componentType, uuid, resourceInstanceName, artifactUUID);
902                 if (downloadResourceArtifactEither.isRight()) {
903                     responseFormat = downloadResourceArtifactEither.right().value();
904                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
905                 } else {
906                     byte[] value = downloadResourceArtifactEither.left().value();
907                     InputStream is = new ByteArrayInputStream(value);
908                     Map<String, String> headers = new HashMap<>();
909                     headers.put(Constants.MD5_HEADER, GeneralUtility.calculateMD5Base64EncodedByByteArray(value));
910                     responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
911                     responseWrapper.setInnerElement(buildOkResponse(responseFormat, is, headers));
912                 }
913             }
914         }  catch (ComponentException e){
915             responseFormat = getComponentsUtils().getResponseFormat(e);
916         }
917         finally{
918             getComponentsUtils().auditExternalDownloadArtifact(responseFormat, new ResourceCommonInfo(resourceInstanceName, componentTypeValue),
919                     new DistributionData(instanceIdHeader, requestURI), requestId, artifactUUID, userId);
920         }
921         return responseWrapper.getInnerElement();
922     }
923 }