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