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