2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.enrichment.types.ArtifactType;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.core.utilities.file.FileUtils;
28 import org.openecomp.sdc.activityLog.ActivityLogManager;
29 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.common.utils.CommonUtil;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.datatypes.error.ErrorMessage;
35 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
36 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
37 import org.openecomp.sdc.logging.types.LoggerConstants;
38 import org.openecomp.sdc.logging.types.LoggerErrorCode;
39 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
40 import org.openecomp.sdc.vendorsoftwareproduct.MibManager;
41 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.MibDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
45 import org.openecomp.sdc.vendorsoftwareproduct.errors.MibUploadErrorBuilder;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MibUploadStatus;
47 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
48 import org.openecomp.sdc.versioning.dao.types.Version;
49 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
50 import org.openecomp.sdcrests.activitylog.types.ActivityType;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.nio.ByteBuffer;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
59 import java.util.Objects;
60 import java.util.Optional;
62 public class MibManagerImpl implements MibManager {
63 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
64 private ActivityLogManager activityLogManager;
65 private MibDao mibDao;
67 public MibManagerImpl(MibDao mibDao,
68 ActivityLogManager activityLogManager) {
71 this.activityLogManager = activityLogManager;
72 mibDao.registerVersioning(
73 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
77 public void delete(String vspId, Version version, String componentId,
78 ArtifactType artifactType, String user) {
79 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
82 setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
84 Optional<MibEntity> retrieved = mibDao.getByType(mibEntity);
86 if (!retrieved.isPresent()) {
87 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
88 mibEntity.getEntityType(),
90 VspDetails.ENTITY_TYPE,
91 mibEntity.getFirstClassCitizenId(),
95 mibDao.delete(retrieved.get());
97 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
101 public void upload(InputStream object, String filename, String vspId,
102 Version version, String componentId, ArtifactType artifactType,
104 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
106 if (object == null) {
107 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
108 LoggerTragetServiceName.UPLOAD_MIB, ErrorLevel.ERROR.name(),
109 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid MIB zip file");
110 throw new CoreException(new MibUploadErrorBuilder(
111 Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()).build());
113 Map<String, List<ErrorMessage>> errors = new HashMap<>();
115 byte[] uploadedFileData = FileUtils.toByteArray(object);
116 validateMibZipContent(vspId, version, uploadedFileData, errors);
117 if (MapUtils.isNotEmpty(errors)) {
118 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
119 LoggerTragetServiceName.UPLOAD_MIB, ErrorLevel.ERROR.name(),
120 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid MIB zip file");
121 throw new CoreException(
122 new MibUploadErrorBuilder(errors.values().iterator().next().get(0).getMessage())
126 createArtifactInDatabase(vspId, version, filename, componentId, artifactType,
129 } catch (Exception exception) {
130 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
131 LoggerTragetServiceName.UPLOAD_MIB, ErrorLevel.ERROR.name(),
132 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid MIB zip file");
133 throw new CoreException(new MibUploadErrorBuilder(exception.getMessage()).build());
137 ActivityLogEntity activityLogEntity =
138 new ActivityLogEntity(vspId, String.valueOf(version.getMajor() + 1),
139 ActivityType.UPLOAD_ARTIFACT.toString(), user, true, "", "");
140 activityLogManager.addActionLog(activityLogEntity, user);
143 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
146 private void createArtifactInDatabase(String vspId, Version version, String filename,
147 String componentId, ArtifactType artifactType,
148 byte[] uploadedFileData) {
149 String artifactId = CommonMethods.nextUuId();
150 MibEntity mibEntity =
151 setValuesForComponentArtifactEntityUpload(vspId, version, filename, componentId,
152 artifactId, artifactType, uploadedFileData);
153 mibDao.create(mibEntity);
157 public MibUploadStatus listFilenames(String vspId, Version version, String componentId,
159 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
162 new MibEntity(vspId, version, componentId, null);
164 mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
166 return setMibUploadStatusValues(current);
170 private MibUploadStatus setMibUploadStatusValues(
171 MibEntity mibEntity) {
172 MibUploadStatus mibUploadStatus = new MibUploadStatus();
174 Collection<MibEntity> artifactNames =
175 mibDao.list(mibEntity);
176 Map<ArtifactType, String> artifactTypeToFilename =
177 VendorSoftwareProductUtils.filterNonTrapOrPollArtifacts(artifactNames);
179 if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
180 if (artifactTypeToFilename.containsKey(ArtifactType.SNMP_TRAP)) {
181 mibUploadStatus.setSnmpTrap(artifactTypeToFilename.get(ArtifactType.SNMP_TRAP));
183 if (artifactTypeToFilename.containsKey(ArtifactType.SNMP_POLL)) {
184 mibUploadStatus.setSnmpPoll(artifactTypeToFilename.get(ArtifactType.SNMP_POLL));
188 return mibUploadStatus;
191 private MibEntity setValuesForComponentArtifactEntityUpload(
192 String vspId, Version version, String filename, String componentId, String artifactId,
193 ArtifactType artifactType, byte[] uploadedFileData) {
195 MibEntity mibEntity = new MibEntity();
197 mibEntity.setVspId(vspId);
198 mibEntity.setVersion(version);
199 mibEntity.setComponentId(componentId);
200 mibEntity.setId(artifactId);
201 mibEntity.setType(artifactType);
202 mibEntity.setArtifactName(filename);
204 if (Objects.nonNull(uploadedFileData)) {
205 mibEntity.setArtifact(ByteBuffer.wrap(uploadedFileData));
211 private void validateMibZipContent(String vspId, Version version, byte[] uploadedFileData,
212 Map<String, List<ErrorMessage>> errors) {
213 FileContentHandler contentMap;
215 contentMap = CommonUtil.loadUploadFileContent(uploadedFileData);
216 VendorSoftwareProductUtils.validateContentZipData(contentMap, errors);
217 } catch (IOException exception) {
218 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
219 LoggerTragetServiceName.VALIDATE_MIB, ErrorLevel.ERROR.name(),
220 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Invalid MIB zip file");
221 throw new CoreException(
222 new MibUploadErrorBuilder(vspId, version, Messages.INVALID_ZIP_FILE.getErrorMessage())