d278962bdb512a8da967edaad0669ae506a6c24c
[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.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;
49
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;
56 import java.util.Map;
57 import java.util.Objects;
58 import java.util.Optional;
59
60 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
61
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";
69
70   MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
71     this.componentArtifactDao = componentArtifactDao;
72
73     componentArtifactDao.registerVersioning(
74         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
75   }
76
77   @Override
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);
81
82     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
83         setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
84             monitoringUploadType, null);
85     Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
86         componentMonitoringUploadEntity);
87
88     if (!retrieved.isPresent()) {
89       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
90           componentMonitoringUploadEntity.getEntityType(),
91           monitoringUploadType.name(),
92           VspDetails.ENTITY_TYPE,
93           componentMonitoringUploadEntity.getFirstClassCitizenId(),
94           version).build());
95     }
96
97     componentArtifactDao.delete(retrieved.get());
98
99     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
100   }
101
102   @Override
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);
107
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(),
115               "zip")).build());
116     } else {
117       Map<String, List<ErrorMessage>> errors = new HashMap<>();
118       try {
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);
124         }
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())
133                   .build());
134         }
135
136         createArtifactInDatabase(vspId, version, filename, componentId, type,
137             uploadedFileData);
138
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());
145       }
146     }
147     logger.audit("Uploaded Monitoring File for component id:" + componentId + " ,vspId:" + vspId);
148     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
149   }
150
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())
163               .build());
164     }
165   }
166
167   private void createArtifactInDatabase(String vspId, Version version, String filename,
168                                         String componentId,
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);
176   }
177
178   @Override
179   public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
180     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
181
182     ComponentMonitoringUploadEntity current =
183         new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
184
185     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
186
187     return setMonitoringUploadStatusValues(current);
188   }
189
190
191   private MonitoringUploadStatus setMonitoringUploadStatusValues(
192       ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
193     MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
194
195     Collection<ComponentMonitoringUploadEntity> artifactNames =
196         componentArtifactDao.list(componentMonitoringUploadEntity);
197     Map<MonitoringUploadType, String> artifactTypeToFilename =
198         VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
199
200     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
201       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
202         monitoringUploadStatus
203             .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
204       }
205       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
206         monitoringUploadStatus
207             .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
208       }
209       if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
210         monitoringUploadStatus
211             .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
212       }
213     }
214
215     return monitoringUploadStatus;
216   }
217
218   private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
219       String vspId, Version version, String filename, String componentId, String artifactId,
220       MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
221
222     ComponentMonitoringUploadEntity
223         entity = new ComponentMonitoringUploadEntity();
224
225     entity.setVspId(vspId);
226     entity.setVersion(version);
227     entity.setComponentId(componentId);
228     entity.setId(artifactId);
229     entity.setType(monitoringUploadType);
230     entity.setArtifactName(filename);
231
232     if (Objects.nonNull(uploadedFileData)) {
233       entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
234     }
235
236     return entity;
237   }
238
239   private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
240                                          Map<String, List<ErrorMessage>> errors) {
241     FileContentHandler contentMap;
242     try {
243       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())
253               .build());
254     }
255     return contentMap;
256   }
257 }