8e8b27301566f4ef1bb1a537990c5cd446e4d61c
[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 public class MonitoringUploadsManagerImpl implements MonitoringUploadsManager {
61   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
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";
67
68   MonitoringUploadsManagerImpl(ComponentArtifactDao componentArtifactDao) {
69     this.componentArtifactDao = componentArtifactDao;
70
71     componentArtifactDao.registerVersioning(
72         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
73   }
74
75   @Override
76   public void delete(String vspId, Version version, String componentId,
77                      MonitoringUploadType monitoringUploadType) {
78     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
79
80     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
81         setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
82             monitoringUploadType, null);
83     Optional<ComponentMonitoringUploadEntity> retrieved = componentArtifactDao.getByType(
84         componentMonitoringUploadEntity);
85
86     if (!retrieved.isPresent()) {
87       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
88           componentMonitoringUploadEntity.getEntityType(),
89           monitoringUploadType.name(),
90           VspDetails.ENTITY_TYPE,
91           componentMonitoringUploadEntity.getFirstClassCitizenId(),
92           version).build());
93     }
94
95     componentArtifactDao.delete(retrieved.get());
96
97     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
98   }
99
100   @Override
101   public void upload(InputStream object, String filename, String vspId,
102                      Version version, String componentId,
103                      MonitoringUploadType type) {
104     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
105
106     if (object == null) {
107       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
108           LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
109           LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
110               .toString() + " zip file");
111       throw new CoreException(new MonitoringUploadErrorBuilder(
112           Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()).build());
113     } else {
114       Map<String, List<ErrorMessage>> errors = new HashMap<>();
115       try {
116         byte[] uploadedFileData = FileUtils.toByteArray(object);
117         final FileContentHandler upload =
118             validateZip(vspId, version, uploadedFileData, errors);
119         if (type.equals(MonitoringUploadType.VES_EVENTS)) {
120           validateVesEventUpload(upload, errors);
121         }
122         if (MapUtils.isNotEmpty(errors)) {
123           MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
124               LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
125               LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
126                   .toString() + " zip file");
127           throw new CoreException(
128               new MonitoringUploadErrorBuilder(
129                   errors.values().iterator().next().get(0).getMessage())
130                   .build());
131         }
132
133         createArtifactInDatabase(vspId, version, filename, componentId, type,
134             uploadedFileData);
135
136       } catch (Exception exception) {
137         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
138             LoggerTragetServiceName.UPLOAD_MONITORING_FILE, ErrorLevel.ERROR.name(),
139             LoggerErrorCode.DATA_ERROR.getErrorCode(), INVALID + type
140                         .toString() + "zip file" );
141         throw new CoreException(new MonitoringUploadErrorBuilder(exception.getMessage()).build());
142       }
143     }
144     logger.audit("Uploaded Monitoring File for component id:" + componentId + " ,vspId:" + vspId);
145     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
146   }
147
148   private void validateVesEventUpload(FileContentHandler upload,
149                                       Map<String, List<ErrorMessage>> errors) {
150     if (!CommonUtil.validateAllFilesYml(upload)) {
151       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
152           .add(new ErrorMessage(ErrorLevel.ERROR,
153               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage()));
154       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
155           LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
156           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_VES_FILE);
157       throw new CoreException(
158           new MonitoringUploadErrorBuilder(
159               Messages.VES_ZIP_SHOULD_CONTAIN_YML_ONLY.getErrorMessage())
160               .build());
161     }
162   }
163
164   private void createArtifactInDatabase(String vspId, Version version, String filename,
165                                         String componentId,
166                                         MonitoringUploadType type,
167                                         byte[] uploadedFileData) {
168     String artifactId = CommonMethods.nextUuId();
169     ComponentMonitoringUploadEntity componentMonitoringUploadEntity =
170         setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
171             artifactId, type, uploadedFileData);
172     componentArtifactDao.create(componentMonitoringUploadEntity);
173   }
174
175   @Override
176   public MonitoringUploadStatus listFilenames(String vspId, Version version, String componentId) {
177     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
178
179     ComponentMonitoringUploadEntity current =
180         new ComponentMonitoringUploadEntity(vspId, version, componentId, null);
181
182     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
183
184     return setMonitoringUploadStatusValues(current);
185   }
186
187
188   private MonitoringUploadStatus setMonitoringUploadStatusValues(
189       ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
190     MonitoringUploadStatus monitoringUploadStatus = new MonitoringUploadStatus();
191
192     Collection<ComponentMonitoringUploadEntity> artifactNames =
193         componentArtifactDao.list(componentMonitoringUploadEntity);
194     Map<MonitoringUploadType, String> artifactTypeToFilename =
195         VendorSoftwareProductUtils.mapArtifactsByType(artifactNames);
196
197     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
198       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_TRAP)) {
199         monitoringUploadStatus
200             .setSnmpTrap(artifactTypeToFilename.get(MonitoringUploadType.SNMP_TRAP));
201       }
202       if (artifactTypeToFilename.containsKey(MonitoringUploadType.SNMP_POLL)) {
203         monitoringUploadStatus
204             .setSnmpPoll(artifactTypeToFilename.get(MonitoringUploadType.SNMP_POLL));
205       }
206       if (artifactTypeToFilename.containsKey(MonitoringUploadType.VES_EVENTS)) {
207         monitoringUploadStatus
208             .setVesEvent(artifactTypeToFilename.get(MonitoringUploadType.VES_EVENTS));
209       }
210     }
211
212     return monitoringUploadStatus;
213   }
214
215   private ComponentMonitoringUploadEntity setValuesForComponentArtifactEntityUpload(
216       String vspId, Version version, String filename, String componentId, String artifactId,
217       MonitoringUploadType monitoringUploadType, byte[] uploadedFileData) {
218
219     ComponentMonitoringUploadEntity
220         entity = new ComponentMonitoringUploadEntity();
221
222     entity.setVspId(vspId);
223     entity.setVersion(version);
224     entity.setComponentId(componentId);
225     entity.setId(artifactId);
226     entity.setType(monitoringUploadType);
227     entity.setArtifactName(filename);
228
229     if (Objects.nonNull(uploadedFileData)) {
230       entity.setArtifact(ByteBuffer.wrap(uploadedFileData));
231     }
232
233     return entity;
234   }
235
236   private FileContentHandler validateZip(String vspId, Version version, byte[] uploadedFileData,
237                                          Map<String, List<ErrorMessage>> errors) {
238     FileContentHandler contentMap;
239     try {
240       contentMap =
241           CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, uploadedFileData);
242       VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
243     } catch (IOException exception) {
244       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
245           LoggerTragetServiceName.VALIDATE_MONITORING_FILE, ErrorLevel.ERROR.name(),
246           LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid Monitoring zip file");
247       throw new CoreException(
248           new MonitoringUploadErrorBuilder(vspId, version,
249               Messages.INVALID_ZIP_FILE.getErrorMessage())
250               .build());
251     }
252     return contentMap;
253   }
254 }