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