[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / MibManagerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
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;
51
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;
58 import java.util.Map;
59 import java.util.Objects;
60 import java.util.Optional;
61
62 public class MibManagerImpl implements MibManager {
63   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
64   private ActivityLogManager activityLogManager;
65   private MibDao mibDao;
66
67   public MibManagerImpl(MibDao mibDao,
68                         ActivityLogManager activityLogManager) {
69     this.mibDao = mibDao;
70
71     this.activityLogManager = activityLogManager;
72     mibDao.registerVersioning(
73         VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE);
74   }
75
76   @Override
77   public void delete(String vspId, Version version, String componentId,
78                      ArtifactType artifactType, String user) {
79     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
80
81     MibEntity mibEntity =
82         setValuesForComponentArtifactEntityUpload(vspId, version, null, componentId, null,
83             artifactType, null);
84     Optional<MibEntity> retrieved = mibDao.getByType(mibEntity);
85
86     if (!retrieved.isPresent()) {
87       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
88           mibEntity.getEntityType(),
89           artifactType.name(),
90           VspDetails.ENTITY_TYPE,
91           mibEntity.getFirstClassCitizenId(),
92           version).build());
93     }
94
95     mibDao.delete(retrieved.get());
96
97     mdcDataDebugMessage.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, ArtifactType artifactType,
103                      String user) {
104     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
105
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());
112     } else {
113       Map<String, List<ErrorMessage>> errors = new HashMap<>();
114       try {
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())
123                   .build());
124         }
125
126         createArtifactInDatabase(vspId, version, filename, componentId, artifactType,
127             uploadedFileData);
128
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());
134       }
135     }
136
137     ActivityLogEntity activityLogEntity =
138         new ActivityLogEntity(vspId, String.valueOf(version.getMajor() + 1),
139             ActivityType.UPLOAD_ARTIFACT.toString(), user, true, "", "");
140     activityLogManager.addActionLog(activityLogEntity, user);
141
142
143     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
144   }
145
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);
154   }
155
156   @Override
157   public MibUploadStatus listFilenames(String vspId, Version version, String componentId,
158                                        String user) {
159     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
160
161     MibEntity current =
162         new MibEntity(vspId, version, componentId, null);
163
164     mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
165
166     return setMibUploadStatusValues(current);
167   }
168
169
170   private MibUploadStatus setMibUploadStatusValues(
171       MibEntity mibEntity) {
172     MibUploadStatus mibUploadStatus = new MibUploadStatus();
173
174     Collection<MibEntity> artifactNames =
175         mibDao.list(mibEntity);
176     Map<ArtifactType, String> artifactTypeToFilename =
177         VendorSoftwareProductUtils.filterNonTrapOrPollArtifacts(artifactNames);
178
179     if (MapUtils.isNotEmpty(artifactTypeToFilename)) {
180       if (artifactTypeToFilename.containsKey(ArtifactType.SNMP_TRAP)) {
181         mibUploadStatus.setSnmpTrap(artifactTypeToFilename.get(ArtifactType.SNMP_TRAP));
182       }
183       if (artifactTypeToFilename.containsKey(ArtifactType.SNMP_POLL)) {
184         mibUploadStatus.setSnmpPoll(artifactTypeToFilename.get(ArtifactType.SNMP_POLL));
185       }
186     }
187
188     return mibUploadStatus;
189   }
190
191   private MibEntity setValuesForComponentArtifactEntityUpload(
192       String vspId, Version version, String filename, String componentId, String artifactId,
193       ArtifactType artifactType, byte[] uploadedFileData) {
194
195     MibEntity mibEntity = new MibEntity();
196
197     mibEntity.setVspId(vspId);
198     mibEntity.setVersion(version);
199     mibEntity.setComponentId(componentId);
200     mibEntity.setId(artifactId);
201     mibEntity.setType(artifactType);
202     mibEntity.setArtifactName(filename);
203
204     if (Objects.nonNull(uploadedFileData)) {
205       mibEntity.setArtifact(ByteBuffer.wrap(uploadedFileData));
206     }
207
208     return mibEntity;
209   }
210
211   private void validateMibZipContent(String vspId, Version version, byte[] uploadedFileData,
212                                      Map<String, List<ErrorMessage>> errors) {
213     FileContentHandler contentMap;
214     try {
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())
223               .build());
224     }
225   }
226 }