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.MdcDataDebugMessage;
34 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
35 import org.openecomp.sdc.logging.types.LoggerConstants;
36 import org.openecomp.sdc.logging.types.LoggerErrorCode;
37 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
38 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
39 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
40 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
44 import org.openecomp.sdc.vendorsoftwareproduct.errors.MonitoringUploadErrorBuilder;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
46 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
47 import org.openecomp.sdc.versioning.dao.types.Version;
48 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.nio.ByteBuffer;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.Objects;
58 import java.util.Optional;
60 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
62 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
63 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
64 private final ComponentArtifactDao componentArtifactDao;
65 private static final Logger logger =
66 LoggerFactory.getLogger(VendorSoftwareProductManagerImpl.class);
67 private static final String INVALID = "Invalid ";
68 private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
70 MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
71 this.componentArtifactDao = componentArtifactDao;
73 componentArtifactDao.registerVersioning(
74 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
78 public void delete(String vspId, Version version, String componentId,
79 MonitoringUploadType monitoringUploadType) {
80 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
82 ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
83 setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
84 monitoringUploadType, null);
85 Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
86 componentMonitoringUploadEntity);
88 if (!retrieved.isPresent()) {
89 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
90 componentMonitoringUploadEntity.getEntityType(),
91 monitoringUploadType.name(),
92 VspDetails.ENTITY_TYPE,
93 componentMonitoringUploadEntity.getFirstClassCitizenId(),
97 componentArtifactDao.delete(retrieved.get());
99 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
103 public void upload(InputStream object, String filename, String vspId,
104 Version version, String componentId,
105 MonitoringUploadType type) {
106 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
108 if (object == null) {
109 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
110 LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
111 LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
112 .toString() + " zip file");
113 throw new CoreException(new MonitoringUploadErrorBuilder(
114 getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
117 Map<String, List<ErrorMessage>> errors = new HashMap<>();
119 byte[] uploadedFileData = FileUtils.toByteArray(object);
120 final FileContentHandler upload =
121 validateZip(vspId, version, uploadedFileData, errors);
122 if (type.equals(MonitoringUploadType.VES_EVENTS)) {
123 validateVesEventUpload(upload, errors);
125 if (MapUtils.isNotEmpty(errors)) {
126 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
127 LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
128 LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
129 .toString() + " zip file");
130 throw new CoreException(
131 new MonitoringUploadErrorBuilder(
132 errors.values().iterator().next().get(0).getMessage())
136 createArtifactInDatabase(vspId, version, filename, componentId, type,
139 } catch (Exception exception) {
140 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
141 LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
142 LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
143 .toString() + "zip file");
144 throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
147 logger.audit("Uploaded Monitoring File for component id:" + componentId + " ,vspId:" + vspId);
148 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
151 private void validateVesEventUpload(FileContentHandler upload,
152 Map<String, List<ErrorMessage>> errors) {
153 if (!CommonUtil.validateAllFilesYml(upload)) {
154 ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
155 .add(new ErrorMessage(ErrorLevel.ERROR,
156 Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage()));
157 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
158 LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
159 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VES_FILE);
160 throw new CoreException(
161 new MonitoringUploadErrorBuilder(
162 Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage())
167 private void createArtifactInDatabase(String vspId, Version version, String filename,
169 MonitoringUploadType type,
170 byte[] uploadedFileData) {
171 String artifactId = CommonMethods.nextUuId();
172 ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
173 setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
174 artifactId, type, uploadedFileData);
175 componentArtifactDao.create(componentMonitoringUploadEntity);
179 public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
180 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
182 ComponentMonitoringUploadEntity current =
183 new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
185 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
187 return setMonitoringUploadStatusValues(current);
191 private MonitoringUploadStatus setMonitoringUploadStatusValues(
192 ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
193 MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
195 Collection<ComponentMonitoringUploadEntity> artifactNames =
196 componentArtifactDao.list(componentMonitoringUploadEntity);
197 Map<MonitoringUploadType, String> artifactTypeToFilename =
198 VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
200 if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
201 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
202 monitoringUploadStatus
203 .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
205 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
206 monitoringUploadStatus
207 .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
209 if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
210 monitoringUploadStatus
211 .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
215 return monitoringUploadStatus;
218 private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
219 String vspId, Version version, String filename, String componentId, String artifactId,
220 MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
222 ComponentMonitoringUploadEntity
223 entity = new ComponentMonitoringUploadEntity();
225 entity.setVspId(vspId);
226 entity.setVersion(version);
227 entity.setComponentId(componentId);
228 entity.setId(artifactId);
229 entity.setType(monitoringUploadType);
230 entity.setArtifactName(filename);
232 if (Objects.nonNull(uploadedFileData)) {
233 entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
239 private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
240 Map<String, List<ErrorMessage>> errors) {
241 FileContentHandler contentMap;
244 CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, uploadedFileData);
245 VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
246 } catch (IOException exception) {
247 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
248 LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
249 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid Monitoring zip file");
250 throw new CoreException(
251 new MonitoringUploadErrorBuilder(vspId, version,
252 Messages.INVALID_ZIP_FILE.getErrorMessage())