d32c09b7dafa79e9b7426bd7a88bb3f95af220b8
[sdc.git] /
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.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
34 import org.openecomp.sdc.logging.types.LoggerConstants;
35 import org.openecomp.sdc.logging.types.LoggerErrorCode;
36 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
37 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
38 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
39 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
43 import org.openecomp.sdc.vendorsoftwareproduct.errors.MonitoringUploadErrorBuilder;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
45 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
48
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.nio.ByteBuffer;
52 import java.util.Collection;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Objects;
57 import java.util.Optional;
58
59 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
60
61 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
62   private final ComponentArtifactDao componentArtifactDao;
63   private static final Logger logger =
64       LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
65   private static final String INVALID = "Invalid ";
66   private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
67
68   MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
69     this.componentArtifactDao = componentArtifactDao;
70
71     componentArtifactDao.registerVersioning(
72         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
73   }
74
75   @Override
76   public void delete(String vspId, Version version, String componentId,
77                      MonitoringUploadType monitoringUploadType) {
78     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
79         setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
80             monitoringUploadType, null);
81     Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
82         componentMonitoringUploadEntity);
83
84     if (!retrieved.isPresent()) {
85       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
86           componentMonitoringUploadEntity.getEntityType(),
87           monitoringUploadType.name(),
88           VspDetails.ENTITY_TYPE,
89           componentMonitoringUploadEntity.getFirstClassCitizenId(),
90           version).build());
91     }
92
93     componentArtifactDao.delete(retrieved.get());
94   }
95
96   @Override
97   public void upload(InputStream object, String filename, String vspId,
98                      Version version, String componentId,
99                      MonitoringUploadType type) {
100     if (object == null) {
101       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
102           LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
103           LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
104               .toString() + " zip file");
105       throw new CoreException(new MonitoringUploadErrorBuilder(
106           getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
107               "zip")).build());
108     } else {
109       Map<String, List<ErrorMessage>> errors = new HashMap<>();
110       try {
111         byte[] uploadedFileData = FileUtils.toByteArray(object);
112         final FileContentHandler upload =
113             validateZip(vspId, version, uploadedFileData, errors);
114         if (type.equals(MonitoringUploadType.VES_EVENTS)) {
115           validateVesEventUpload(upload, errors);
116         }
117         if (MapUtils.isNotEmpty(errors)) {
118           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
119               LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
120               LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
121                   .toString() + " zip file");
122           throw new CoreException(
123               new MonitoringUploadErrorBuilder(
124                   errors.values().iterator().next().get(0).getMessage())
125                   .build());
126         }
127
128         createArtifactInDatabase(vspId, version, filename, componentId, type,
129             uploadedFileData);
130
131       } catch (Exception exception) {
132         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
133             LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
134             LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
135                 .toString() + "zip file");
136         throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
137       }
138     }
139     logger.audit("Uploaded Monitoring File for component id:" + componentId + " ,vspId:" + vspId);
140   }
141
142   private void validateVesEventUpload(FileContentHandler upload,
143                                       Map<String, List<ErrorMessage>> errors) {
144     if (!CommonUtil.validateAllFilesYml(upload)) {
145       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
146           .add(new ErrorMessage(ErrorLevel.ERROR,
147               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage()));
148       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
149           LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
150           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VES_FILE);
151       throw new CoreException(
152           new MonitoringUploadErrorBuilder(
153               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage())
154               .build());
155     }
156   }
157
158   private void createArtifactInDatabase(String vspId, Version version, String filename,
159                                         String componentId,
160                                         MonitoringUploadType type,
161                                         byte[] uploadedFileData) {
162     String artifactId = CommonMethods.nextUuId();
163     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
164         setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
165             artifactId, type, uploadedFileData);
166     componentArtifactDao.create(componentMonitoringUploadEntity);
167   }
168
169   @Override
170   public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
171     ComponentMonitoringUploadEntity current =
172         new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
173     return setMonitoringUploadStatusValues(current);
174   }
175
176
177   private MonitoringUploadStatus setMonitoringUploadStatusValues(
178       ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
179     MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
180
181     Collection<ComponentMonitoringUploadEntity> artifactNames =
182         componentArtifactDao.list(componentMonitoringUploadEntity);
183     Map<MonitoringUploadType, String> artifactTypeToFilename =
184         VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
185
186     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
187       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
188         monitoringUploadStatus
189             .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
190       }
191       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
192         monitoringUploadStatus
193             .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
194       }
195       if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
196         monitoringUploadStatus
197             .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
198       }
199     }
200
201     return monitoringUploadStatus;
202   }
203
204   private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
205       String vspId, Version version, String filename, String componentId, String artifactId,
206       MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
207
208     ComponentMonitoringUploadEntity
209         entity = new ComponentMonitoringUploadEntity();
210
211     entity.setVspId(vspId);
212     entity.setVersion(version);
213     entity.setComponentId(componentId);
214     entity.setId(artifactId);
215     entity.setType(monitoringUploadType);
216     entity.setArtifactName(filename);
217
218     if (Objects.nonNull(uploadedFileData)) {
219       entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
220     }
221
222     return entity;
223   }
224
225   private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
226                                          Map<String, List<ErrorMessage>> errors) {
227     FileContentHandler contentMap;
228     try {
229       contentMap =
230           CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, uploadedFileData);
231       VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
232     } catch (IOException exception) {
233       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
234           LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
235           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid Monitoring zip file");
236       throw new CoreException(
237           new MonitoringUploadErrorBuilder(vspId, version,
238               Messages.INVALID_ZIP_FILE.getErrorMessage())
239               .build());
240     }
241     return contentMap;
242   }
243 }