25c99b294bda8ee14170c5b94c8bb401a8dc4263
[sdc.git] /
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 final MonitoringUploadsManager monitoringUploadsManager;
54   private final ComponentManager componentManager;
55
56   public ComponentMonitoringUploadsImpl() {
57     this.monitoringUploadsManager = MonitoringUploadsManagerFactory.getInstance().createInterface();
58     this.componentManager = ComponentManagerFactory.getInstance().createInterface();
59   }
60
61   public ComponentMonitoringUploadsImpl(MonitoringUploadsManager monitoringUploadsManager,
62       ComponentManager componentManager) {
63     this.monitoringUploadsManager = monitoringUploadsManager;
64     this.componentManager = componentManager;
65   }
66
67   @Override
68   public Response upload(Attachment attachment,
69                          String vspId, String versionId, String componentId, String type,
70                          String user) throws Exception {
71
72     Version version = new Version(versionId);
73     componentManager.validateComponentExistence(vspId, version, componentId);
74
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();
80   }
81
82   private MonitoringUploadType getMonitoringUploadType(String vspId, String componentId,
83                                                        String type) throws Exception {
84     MonitoringUploadType monitoringUploadType;
85     try {
86       monitoringUploadType = MonitoringUploadType.valueOf(type);
87     } catch (IllegalArgumentException exception) {
88       String errorWithParameters = ErrorMessagesFormatBuilder
89           .getErrorWithParameters(Messages.ILLEGAL_MONITORING_ARTIFACT_TYPE.getErrorMessage(),
90               componentId, vspId);
91       throw new Exception(errorWithParameters, exception);
92     }
93     return monitoringUploadType;
94   }
95
96   @Override
97   public Response delete(String vspId, String versionId, String componentId,
98                          String type, String user) throws Exception {
99
100     MonitoringUploadType monitoringUploadType = getMonitoringUploadType(vspId, componentId, type);
101
102     Version version = new Version(versionId);
103     componentManager.validateComponentExistence(vspId, version, componentId);
104     monitoringUploadsManager.delete(vspId, version, componentId, monitoringUploadType);
105     return Response.ok().build();
106   }
107
108   @Override
109   public Response list(String vspId, String versionId, String componentId,
110                        String user) {
111
112     Version version = new Version(versionId);
113     componentManager.validateComponentExistence(vspId, version, componentId);
114
115     MonitoringUploadStatus response =
116         monitoringUploadsManager.listFilenames(vspId, version, componentId);
117
118     MonitoringUploadStatusDto returnEntity =
119         new MapMonitoringUploadStatusToDto()
120             .applyMapping(response, MonitoringUploadStatusDto.class);
121     return Response.status(Response.Status.OK).entity(returnEntity).build();
122   }
123 }