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;
45 import java.util.Collection;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Objects;
50 import java.util.Optional;
52 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
54 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
56 private final ComponentArtifactDao componentArtifactDao;
58 MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
59 this.componentArtifactDao = componentArtifactDao;
61 componentArtifactDao.registerVersioning(
62 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
66 public void delete(String vspId, Version version, String componentId,
67 MonitoringUploadType monitoringUploadType) {
68 ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
69 setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
70 monitoringUploadType, null);
71 Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
72 componentMonitoringUploadEntity);
74 if (!retrieved.isPresent()) {
75 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
76 componentMonitoringUploadEntity.getEntityType(),
77 monitoringUploadType.name(),
78 VspDetails.ENTITY_TYPE,
79 componentMonitoringUploadEntity.getFirstClassCitizenId(),
83 componentArtifactDao.delete(retrieved.get());
87 public void upload(InputStream object, String filename, String vspId,
88 Version version, String componentId,
89 MonitoringUploadType type) {
91 throw new CoreException(new MonitoringUploadErrorBuilder(
92 getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(),
95 Map<String, List<ErrorMessage>> errors = new HashMap<>();
97 byte[] uploadedFileData = FileUtils.toByteArray(object);
98 final FileContentHandler upload =
99 validateZip(vspId, version, uploadedFileData, errors);
100 if (type.equals(MonitoringUploadType.VES_EVENTS)) {
101 validateVesEventUpload(upload, errors);
103 if (MapUtils.isNotEmpty(errors)) {
104 throw new CoreException(
105 new MonitoringUploadErrorBuilder(
106 errors.values().iterator().next().get(0).getMessage())
110 createArtifactInDatabase(vspId, version, filename, componentId, type,
113 } catch (Exception exception) {
114 throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
119 private void validateVesEventUpload(FileContentHandler upload,
120 Map<String, List<ErrorMessage>> errors) {
121 if (!CommonUtil.validateAllFilesYml(upload)) {
122 ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
123 .add(new ErrorMessage(ErrorLevel.ERROR,
124 Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage()));
125 throw new CoreException(
126 new MonitoringUploadErrorBuilder(
127 Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage())
132 private void createArtifactInDatabase(String vspId, Version version, String filename,
134 MonitoringUploadType type,
135 byte[] uploadedFileData) {
136 String artifactId = CommonMethods.nextUuId();
137 ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
138 setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
139 artifactId, type, uploadedFileData);
140 componentArtifactDao.create(componentMonitoringUploadEntity);
144 public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
145 ComponentMonitoringUploadEntity current =
146 new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
147 return setMonitoringUploadStatusValues(current);
151 private MonitoringUploadStatus setMonitoringUploadStatusValues(
152 ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
153 MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
155 Collection<ComponentMonitoringUploadEntity> artifactNames =
156 componentArtifactDao.list(componentMonitoringUploadEntity);
157 Map<MonitoringUploadType, String> artifactTypeToFilename =
158 VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
160 if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
161 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
162 monitoringUploadStatus
163 .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
165 if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
166 monitoringUploadStatus
167 .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
169 if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
170 monitoringUploadStatus
171 .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
175 return monitoringUploadStatus;
178 private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
179 String vspId, Version version, String filename, String componentId, String artifactId,
180 MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
182 ComponentMonitoringUploadEntity
183 entity = new ComponentMonitoringUploadEntity();
185 entity.setVspId(vspId);
186 entity.setVersion(version);
187 entity.setComponentId(componentId);
188 entity.setId(artifactId);
189 entity.setType(monitoringUploadType);
190 entity.setArtifactName(filename);
192 if (Objects.nonNull(uploadedFileData)) {
193 entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
199 private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
200 Map<String, List<ErrorMessage>> errors) {
201 FileContentHandler contentMap;
204 CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, uploadedFileData);
205 VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
206 } catch (IOException exception) {
207 throw new CoreException(
208 new MonitoringUploadErrorBuilder(vspId, version,
209 Messages.INVALID_ZIP_FILE.getErrorMessage())