re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / MonitoringUploadsManagerImpl.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.enrichment.types.MonitoringUploadType;
21 import org.openecomp.core.utilities.CommonMethods;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.common.utils.CommonUtil;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
36 import org.openecomp.sdc.vendorsoftwareproduct.errors.MonitoringUploadErrorBuilder;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
38 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.ByteBuffer;
45 import java.util.*;
46
47 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
48
49 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
50
51   private final ComponentArtifactDao componentArtifactDao;
52
53   MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
54     this.componentArtifactDao = componentArtifactDao;
55
56     componentArtifactDao.registerVersioning(
57         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
58   }
59
60   @Override
61   public void delete(String vspId, Version version, String componentId,
62                      MonitoringUploadType monitoringUploadType) {
63     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
64         setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
65             monitoringUploadType, null);
66     Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
67         componentMonitoringUploadEntity);
68
69     if (!retrieved.isPresent()) {
70       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
71           componentMonitoringUploadEntity.getEntityType(),
72           monitoringUploadType.name(),
73           VspDetails.ENTITY_TYPE,
74           componentMonitoringUploadEntity.getFirstClassCitizenId(),
75           version).build());
76     }
77
78     componentArtifactDao.delete(retrieved.get());
79   }
80
81   @Override
82   public void upload(InputStream object, String filename, String vspId,
83                      Version version, String componentId,
84                      MonitoringUploadType type) {
85     if (object == null) {
86       throw new CoreException(new MonitoringUploadErrorBuilder(
87           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
88               "zip")).build());
89     } else {
90       Map<String, List<ErrorMessage>> errors = new HashMap<>();
91       try {
92         byte[] uploadedFileData = FileUtils.toByteArray(object);
93         final FileContentHandler upload =
94             validateZip(vspId, version, uploadedFileData, errors);
95         if (type.equals(MonitoringUploadType.VES_EVENTS)) {
96           validateVesEventUpload(upload, errors);
97         }
98         if (MapUtils.isNotEmpty(errors)) {
99           throw new CoreException(
100               new MonitoringUploadErrorBuilder(
101                   errors.values().iterator().next().get(0).getMessage())
102                   .build());
103         }
104
105         createArtifactInDatabase(vspId, version, filename, componentId, type,
106             uploadedFileData);
107
108       } catch (Exception exception) {
109         throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
110       }
111     }
112   }
113
114   private void validateVesEventUpload(FileContentHandler upload,
115                                       Map<String, List<ErrorMessage>> errors) {
116     if (!CommonUtil.validateAllFilesYml(upload)) {
117       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
118           .add(new ErrorMessage(ErrorLevel.ERROR,
119               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage()));
120       throw new CoreException(
121           new MonitoringUploadErrorBuilder(
122               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage())
123               .build());
124     }
125   }
126
127   private void createArtifactInDatabase(String vspId, Version version, String filename,
128                                         String componentId,
129                                         MonitoringUploadType type,
130                                         byte[] uploadedFileData) {
131     String artifactId = CommonMethods.nextUuId();
132     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
133         setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
134             artifactId, type, uploadedFileData);
135     componentArtifactDao.create(componentMonitoringUploadEntity);
136   }
137
138   @Override
139   public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
140     ComponentMonitoringUploadEntity current =
141         new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
142     return setMonitoringUploadStatusValues(current);
143   }
144
145
146   private MonitoringUploadStatus setMonitoringUploadStatusValues(
147       ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
148     MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
149
150     Collection<ComponentMonitoringUploadEntity> artifactNames =
151         componentArtifactDao.list(componentMonitoringUploadEntity);
152     Map<MonitoringUploadType, String> artifactTypeToFilename =
153         VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
154
155     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
156       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
157         monitoringUploadStatus
158             .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
159       }
160       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
161         monitoringUploadStatus
162             .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
163       }
164       if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
165         monitoringUploadStatus
166             .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
167       }
168     }
169
170     return monitoringUploadStatus;
171   }
172
173   private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
174       String vspId, Version version, String filename, String componentId, String artifactId,
175       MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
176
177     ComponentMonitoringUploadEntity
178         entity = new ComponentMonitoringUploadEntity();
179
180     entity.setVspId(vspId);
181     entity.setVersion(version);
182     entity.setComponentId(componentId);
183     entity.setId(artifactId);
184     entity.setType(monitoringUploadType);
185     entity.setArtifactName(filename);
186
187     if (Objects.nonNull(uploadedFileData)) {
188       entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
189     }
190
191     return entity;
192   }
193
194   private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
195                                          Map<String, List<ErrorMessage>> errors) {
196     FileContentHandler contentMap;
197     try {
198       contentMap =
199           CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, uploadedFileData);
200       VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
201     } catch (IOException exception) {
202       throw new CoreException(
203           new MonitoringUploadErrorBuilder(vspId, version,
204               Messages.INVALID_ZIP_FILE.getErrorMessage())
205               .build());
206     }
207     return contentMap;
208   }
209 }