Added oparent to sdc main
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / services / ComponentMonitoringUploadsImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
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;
38
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.io.InputStream;
42
43 /**
44  * @author katyr
45  * @since June 26, 2017
46  */
47
48 @Named
49 @Service("componentMonitoringUploads")
50 @Scope(value = "prototype")
51 //@Validated
52 public class ComponentMonitoringUploadsImpl implements ComponentMonitoringUploads {
53   private MonitoringUploadsManager
54       monitoringUploadsManager = MonitoringUploadsManagerFactory.getInstance().createInterface();
55   private ComponentManager componentManager =
56       ComponentManagerFactory.getInstance().createInterface();
57
58   @Override
59   public Response upload(Attachment attachment,
60                          String vspId, String versionId, String componentId, String type,
61                          String user) throws Exception {
62
63     Version version = new Version(versionId);
64     componentManager.validateComponentExistence(vspId, version, componentId);
65
66     MonitoringUploadType monitoringUploadType = getMonitoringUploadType(vspId, componentId, type);
67     monitoringUploadsManager.upload(attachment.getObject(InputStream.class),
68         attachment.getContentDisposition().getParameter("filename"), vspId, version, componentId,
69         monitoringUploadType);
70     return Response.ok().build();
71   }
72
73   private MonitoringUploadType getMonitoringUploadType(String vspId, String componentId,
74                                                        String type) throws Exception {
75     MonitoringUploadType monitoringUploadType;
76     try {
77       monitoringUploadType = MonitoringUploadType.valueOf(type);
78     } catch (IllegalArgumentException exception) {
79       String errorWithParameters = ErrorMessagesFormatBuilder
80           .getErrorWithParameters(Messages.ILLEGAL_MONITORING_ARTIFACT_TYPE.getErrorMessage(),
81               componentId, vspId);
82       throw new Exception(errorWithParameters, exception);
83     }
84     return monitoringUploadType;
85   }
86
87   @Override
88   public Response delete(String vspId, String versionId, String componentId,
89                          String type, String user) throws Exception {
90
91     MonitoringUploadType monitoringUploadType = getMonitoringUploadType(vspId, componentId, type);
92
93     Version version = new Version(versionId);
94     componentManager.validateComponentExistence(vspId, version, componentId);
95     monitoringUploadsManager.delete(vspId, version, componentId, monitoringUploadType);
96     return Response.ok().build();
97   }
98
99   @Override
100   public Response list(String vspId, String versionId, String componentId,
101                        String user) {
102
103     Version version = new Version(versionId);
104     componentManager.validateComponentExistence(vspId, version, componentId);
105
106     MonitoringUploadStatus response =
107         monitoringUploadsManager.listFilenames(vspId, version, componentId);
108
109     MonitoringUploadStatusDto returnEntity =
110         new MapMonitoringUploadStatusToDto()
111             .applyMapping(response, MonitoringUploadStatusDto.class);
112     return Response.status(Response.Status.OK).entity(returnEntity).build();
113   }
114 }