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.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;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.ByteBuffer;
47 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
49 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
51 private final ComponentArtifactDao componentArtifactDao;
53 MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
54 this.componentArtifactDao = componentArtifactDao;
56 componentArtifactDao.registerVersioning(
57 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
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);
69 if (!retrieved.isPresent()) {
70 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
71 componentMonitoringUploadEntity.getEntityType(),
72 monitoringUploadType.name(),
73 VspDetails.ENTITY_TYPE,
74 componentMonitoringUploadEntity.getFirstClassCitizenId(),
78 componentArtifactDao.delete(retrieved.get());
82 public void upload(InputStream object, String filename, String vspId,
83 Version version, String componentId,
84 MonitoringUploadType type) {
86 throw new CoreException(new MonitoringUploadErrorBuilder(
87 getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
90 Map<String, List<ErrorMessage>> errors = new HashMap<>();
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);
98 if (MapUtils.isNotEmpty(errors)) {
99 throw new CoreException(
100 new MonitoringUploadErrorBuilder(
101 errors.values().iterator().next().get(0).getMessage())
105 createArtifactInDatabase(vspId, version, filename, componentId, type,
108 } catch (Exception exception) {
109 throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
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())
127 private void createArtifactInDatabase(String vspId, Version version, String filename,
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);
139 public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
140 ComponentMonitoringUploadEntity current =
141 new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
142 return setMonitoringUploadStatusValues(current);
146 private MonitoringUploadStatus setMonitoringUploadStatusValues(
147 ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
148 MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
150 Collection<ComponentMonitoringUploadEntity> artifactNames =
151 componentArtifactDao.list(componentMonitoringUploadEntity);
152 Map<MonitoringUploadType, String> artifactTypeToFilename =
153 VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
155 if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
156 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
157 monitoringUploadStatus
158 .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
160 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
161 monitoringUploadStatus
162 .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
164 if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
165 monitoringUploadStatus
166 .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
170 return monitoringUploadStatus;
173 private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
174 String vspId, Version version, String filename, String componentId, String artifactId,
175 MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
177 ComponentMonitoringUploadEntity
178 entity = new ComponentMonitoringUploadEntity();
180 entity.setVspId(vspId);
181 entity.setVersion(version);
182 entity.setComponentId(componentId);
183 entity.setId(artifactId);
184 entity.setType(monitoringUploadType);
185 entity.setArtifactName(filename);
187 if (Objects.nonNull(uploadedFileData)) {
188 entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
194 private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
195 Map<String, List<ErrorMessage>> errors) {
196 FileContentHandler 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())