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