2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
23 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
24 import org.openecomp.core.enrichment.types.MonitoringUploadType;
25 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
28 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
29 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
30 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManagerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.MonitoringUploadStatusDto;
34 import org.openecomp.sdcrests.vsp.rest.ComponentMonitoringUploads;
35 import org.openecomp.sdcrests.vsp.rest.mapping.MapMonitoringUploadStatusToDto;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.io.InputStream;
45 * @since June 26, 2017
49 @Service("componentMonitoringUploads")
50 @Scope(value = "prototype")
52 public class ComponentMonitoringUploadsImpl implements ComponentMonitoringUploads {
53 private final MonitoringUploadsManager monitoringUploadsManager;
54 private final ComponentManager componentManager;
56 public ComponentMonitoringUploadsImpl() {
57 this.monitoringUploadsManager = MonitoringUploadsManagerFactory.getInstance().createInterface();
58 this.componentManager = ComponentManagerFactory.getInstance().createInterface();
61 public ComponentMonitoringUploadsImpl(MonitoringUploadsManager monitoringUploadsManager,
62 ComponentManager componentManager) {
63 this.monitoringUploadsManager = monitoringUploadsManager;
64 this.componentManager = componentManager;
68 public Response upload(Attachment attachment,
69 String vspId, String versionId, String componentId, String type,
70 String user) throws Exception {
72 Version version = new Version(versionId);
73 componentManager.validateComponentExistence(vspId, version, componentId);
75 MonitoringUploadType monitoringUploadType = getMonitoringUploadType(vspId, componentId, type);
76 monitoringUploadsManager.upload(attachment.getObject(InputStream.class),
77 attachment.getContentDisposition().getParameter("filename"), vspId, version, componentId,
78 monitoringUploadType);
79 return Response.ok().build();
82 private MonitoringUploadType getMonitoringUploadType(String vspId, String componentId,
83 String type) throws Exception {
84 MonitoringUploadType monitoringUploadType;
86 monitoringUploadType = MonitoringUploadType.valueOf(type);
87 } catch (IllegalArgumentException exception) {
88 String errorWithParameters = ErrorMessagesFormatBuilder
89 .getErrorWithParameters(Messages.ILLEGAL_MONITORING_ARTIFACT_TYPE.getErrorMessage(),
91 throw new Exception(errorWithParameters, exception);
93 return monitoringUploadType;
97 public Response delete(String vspId, String versionId, String componentId,
98 String type, String user) throws Exception {
100 MonitoringUploadType monitoringUploadType = getMonitoringUploadType(vspId, componentId, type);
102 Version version = new Version(versionId);
103 componentManager.validateComponentExistence(vspId, version, componentId);
104 monitoringUploadsManager.delete(vspId, version, componentId, monitoringUploadType);
105 return Response.ok().build();
109 public Response list(String vspId, String versionId, String componentId,
112 Version version = new Version(versionId);
113 componentManager.validateComponentExistence(vspId, version, componentId);
115 MonitoringUploadStatus response =
116 monitoringUploadsManager.listFilenames(vspId, version, componentId);
118 MonitoringUploadStatusDto returnEntity =
119 new MapMonitoringUploadStatusToDto()
120 .applyMapping(response, MonitoringUploadStatusDto.class);
121 return Response.status(Response.Status.OK).entity(returnEntity).build();