d5c6ab4c64bb98ba0346e7244da7b9bba3ceb0a9
[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.utils;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.enrichment.types.MonitoringUploadType;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.common.utils.SdcCommon;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
35 import org.slf4j.MDC;
36
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.EnumMap;
41 import java.util.List;
42 import java.util.Map;
43
44 public class VendorSoftwareProductUtils {
45
46   private static final Logger LOGGER = LoggerFactory.getLogger(VendorSoftwareProductUtils.class);
47
48   private VendorSoftwareProductUtils(){
49
50   }
51
52   /**
53    * Add file names to upload file response.
54    *
55    * @param fileContentMap     the file content map
56    * @param uploadFileResponse the upload file response
57    */
58   public static void addFileNamesToUploadFileResponse(FileContentHandler fileContentMap,
59                                                       OrchestrationTemplateActionResponse uploadFileResponse) {
60     uploadFileResponse.setFileNames(new ArrayList<>());
61     for (String filename : fileContentMap.getFileList()) {
62       if (!new File(filename).isDirectory()) {
63         uploadFileResponse.addNewFileToList(filename);
64       }
65     }
66     uploadFileResponse.removeFileFromList(SdcCommon.MANIFEST_NAME);
67   }
68   /**
69    * Validate content zip data.
70    *
71    * @param contentMap the content map
72    * @param errors     the errors
73    */
74   public static void validateContentZipData(FileContentHandler contentMap,
75                                             Map<String, List<ErrorMessage>> errors) {
76     MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
77     if (contentMap.getFileList().isEmpty()) {
78       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
79           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
80     }
81   }
82
83
84   /**
85    * Maps all artifacts by type.
86    *
87    * @param artifacts the artifacts
88    * @return the map
89    */
90   public static Map<MonitoringUploadType, String> mapArtifactsByType(
91       Collection<ComponentMonitoringUploadEntity> artifacts) {
92     Map<MonitoringUploadType, String> artifactTypeToFilename
93             = new EnumMap<>(MonitoringUploadType.class);
94
95     for (ComponentMonitoringUploadEntity entity : artifacts) {
96       artifactTypeToFilename.put(entity.getType(), entity.getArtifactName());
97     }
98
99     return artifactTypeToFilename;
100   }
101
102
103   /**
104    * Sets errors into logger.
105    *  @param errors            the errors
106    * @param targetServiceName the target service name
107    */
108   public static void setErrorsIntoLogger(Map<String, List<ErrorMessage>> errors,
109                                          String targetServiceName) {
110     MdcDataErrorMessage mdcDataErrorMessage =
111         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
112             ErrorLevel.ERROR.name(), null, null);
113     mdcDataErrorMessage.setMdcValues();
114
115     if (MapUtils.isEmpty(errors)) {
116       return;
117     }
118
119     for (Map.Entry<String, List<ErrorMessage>> listEntry : errors.entrySet()) {
120       List<ErrorMessage> errorList = listEntry.getValue();
121       for (ErrorMessage message : errorList) {
122         LOGGER.error(message.getMessage());
123       }
124     }
125   }
126
127   /**
128    * Sets errors into logger.
129    *  @param errors            the errors
130    * @param targetServiceName the target service name
131    */
132   public static void setErrorsIntoLogger(Collection<ErrorCode> errors,
133                                          String targetServiceName) {
134     MdcDataErrorMessage mdcDataErrorMessage =
135         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
136             ErrorLevel.ERROR.name(), null, null);
137     mdcDataErrorMessage.setMdcValues();
138
139     if (CollectionUtils.isEmpty(errors)) {
140       return;
141     }
142
143     for (ErrorCode error : errors) {
144       LOGGER.error(error.message());
145     }
146   }
147 }