[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / utils / VendorSoftwareProductUtils.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.utils;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.enrichment.types.MonitoringUploadType;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
35 import org.openecomp.sdc.logging.types.LoggerConstants;
36 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
37 import org.openecomp.sdc.logging.types.LoggerServiceName;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
40 import org.slf4j.MDC;
41
42 import java.io.File;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 public class VendorSoftwareProductUtils {
50
51   private static final String MANUAL = "Manual";
52   protected static Logger logger =
53       (Logger) LoggerFactory.getLogger(VendorSoftwareProductUtils.class);
54
55   /**
56    * Add file names to upload file response.
57    *
58    * @param fileContentMap     the file content map
59    * @param uploadFileResponse the upload file response
60    */
61   public static void addFileNamesToUploadFileResponse(FileContentHandler fileContentMap,
62                                                       OrchestrationTemplateActionResponse uploadFileResponse) {
63     uploadFileResponse.setFileNames(new ArrayList<>());
64     for (String filename : fileContentMap.getFileList()) {
65       if (!new File(filename).isDirectory()) {
66         uploadFileResponse.addNewFileToList(filename);
67       }
68     }
69     uploadFileResponse.removeFileFromList(SdcCommon.MANIFEST_NAME);
70   }
71
72   /**
73    * Validate raw zip data.
74    *
75    * @param uploadedFileData the uploaded file data
76    * @param errors           the errors
77    */
78   public static void validateRawZipData(byte[] uploadedFileData,
79                                         Map<String, List<ErrorMessage>> errors) {
80     if (uploadedFileData.length == 0) {
81       MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
82       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors).add(
83           new ErrorMessage(ErrorLevel.ERROR,
84               Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()));
85     }
86   }
87
88   /**
89    * Validate content zip data.
90    *
91    * @param contentMap the content map
92    * @param errors     the errors
93    */
94   public static void validateContentZipData(FileContentHandler contentMap,
95                                             Map<String, List<ErrorMessage>> errors) {
96     MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
97     if (contentMap.getFileList().size() == 0) {
98       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
99           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
100     }
101   }
102
103
104   /**
105    * Maps all artifacts by type.
106    *
107    * @param artifacts the artifacts
108    * @return the map
109    */
110   public static Map<MonitoringUploadType, String> mapArtifactsByType(
111       Collection<ComponentMonitoringUploadEntity> artifacts) {
112     Map<MonitoringUploadType, String> artifactTypeToFilename = new HashMap<>();
113
114     for (ComponentMonitoringUploadEntity entity : artifacts) {
115       artifactTypeToFilename.put(entity.getType(), entity.getArtifactName());
116     }
117
118     return artifactTypeToFilename;
119   }
120
121
122   private static boolean isTrapOrPoll(MonitoringUploadType type) {
123     return type.equals(MonitoringUploadType.SNMP_POLL) ||
124         type.equals(MonitoringUploadType.SNMP_TRAP);
125   }
126
127
128   /**
129    * Sets errors into logger.
130    *
131    * @param errors            the errors
132    * @param serviceName       the service name
133    * @param targetServiceName the target service name
134    */
135   public static void setErrorsIntoLogger(Map<String, List<ErrorMessage>> errors,
136                                          LoggerServiceName serviceName, String targetServiceName) {
137     MdcDataErrorMessage mdcDataErrorMessage =
138         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
139             ErrorLevel.ERROR.name(), null, null);
140     mdcDataErrorMessage.setMdcValues();
141
142     if (MapUtils.isEmpty(errors)) {
143       return;
144     }
145
146     for (Map.Entry<String, List<ErrorMessage>> listEntry : errors.entrySet()) {
147       List<ErrorMessage> errorList = listEntry.getValue();
148       for (ErrorMessage message : errorList) {
149         logger.error(message.getMessage());
150       }
151     }
152   }
153
154   /**
155    * Sets errors into logger.
156    *
157    * @param errors            the errors
158    * @param serviceName       the service name
159    * @param targetServiceName the target service name
160    */
161   public static void setErrorsIntoLogger(Collection<ErrorCode> errors,
162                                          LoggerServiceName serviceName, String targetServiceName) {
163     MdcDataErrorMessage mdcDataErrorMessage =
164         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
165             ErrorLevel.ERROR.name(), null, null);
166     mdcDataErrorMessage.setMdcValues();
167
168     if (CollectionUtils.isEmpty(errors)) {
169       return;
170     }
171
172     for (ErrorCode error : errors) {
173       logger.error(error.message());
174     }
175   }
176
177   /*public static boolean isManual(String vspId, Version version, VendorSoftwareProductInfoDao
178       vspInfoDao) {
179     String onboardingMethod = vspInfoDao.get(new VspDetails(vspId, version)).getOnboardingMethod();
180     if (MANUAL.equals(onboardingMethod)) {
181       return true;
182     }
183     return false;
184   }*/
185
186 }