2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
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;
56 import java.util.Objects;
57 import java.util.Optional;
59 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
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";
68 MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
69 this.componentArtifactDao = componentArtifactDao;
71 componentArtifactDao.registerVersioning(
72 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
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);
84 if (!retrieved.isPresent()) {
85 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
86 componentMonitoringUploadEntity.getEntityType(),
87 monitoringUploadType.name(),
88 VspDetails.ENTITY_TYPE,
89 componentMonitoringUploadEntity.getFirstClassCitizenId(),
93 componentArtifactDao.delete(retrieved.get());
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(),
109 Map<String, List<ErrorMessage>> errors = new HashMap<>();
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);
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())
128 createArtifactInDatabase(vspId, version, filename, componentId, type,
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());
139 logger.audit("Uploaded Monitoring File for component id:" + componentId + " ,vspId:" + vspId);
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())
158 private void createArtifactInDatabase(String vspId, Version version, String filename,
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);
170 public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
171 ComponentMonitoringUploadEntity current =
172 new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
173 return setMonitoringUploadStatusValues(current);
177 private MonitoringUploadStatus setMonitoringUploadStatusValues(
178 ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
179 MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
181 Collection<ComponentMonitoringUploadEntity> artifactNames =
182 componentArtifactDao.list(componentMonitoringUploadEntity);
183 Map<MonitoringUploadType, String> artifactTypeToFilename =
184 VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
186 if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
187 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
188 monitoringUploadStatus
189 .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
191 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
192 monitoringUploadStatus
193 .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
195 if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
196 monitoringUploadStatus
197 .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
201 return monitoringUploadStatus;
204 private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
205 String vspId, Version version, String filename, String componentId, String artifactId,
206 MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
208 ComponentMonitoringUploadEntity
209 entity = new ComponentMonitoringUploadEntity();
211 entity.setVspId(vspId);
212 entity.setVersion(version);
213 entity.setComponentId(componentId);
214 entity.setId(artifactId);
215 entity.setType(monitoringUploadType);
216 entity.setArtifactName(filename);
218 if (Objects.nonNull(uploadedFileData)) {
219 entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
225 private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
226 Map<String, List<ErrorMessage>> errors) {
227 FileContentHandler 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())