2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.utils;
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;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.EnumMap;
41 import java.util.List;
44 public class VendorSoftwareProductUtils {
46 private static final Logger LOGGER = LoggerFactory.getLogger(VendorSoftwareProductUtils.class);
48 private VendorSoftwareProductUtils(){
53 * Add file names to upload file response.
55 * @param fileContentMap the file content map
56 * @param uploadFileResponse the upload file response
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);
66 uploadFileResponse.removeFileFromList(SdcCommon.MANIFEST_NAME);
69 * Validate content zip data.
71 * @param contentMap the content map
72 * @param errors the errors
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()));
85 * Maps all artifacts by type.
87 * @param artifacts the artifacts
90 public static Map<MonitoringUploadType, String> mapArtifactsByType(
91 Collection<ComponentMonitoringUploadEntity> artifacts) {
92 Map<MonitoringUploadType, String> artifactTypeToFilename
93 = new EnumMap<>(MonitoringUploadType.class);
95 for (ComponentMonitoringUploadEntity entity : artifacts) {
96 artifactTypeToFilename.put(entity.getType(), entity.getArtifactName());
99 return artifactTypeToFilename;
104 * Sets errors into logger.
105 * @param errors the errors
106 * @param targetServiceName the target service name
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();
115 if (MapUtils.isEmpty(errors)) {
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());
128 * Sets errors into logger.
129 * @param errors the errors
130 * @param targetServiceName the target service name
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();
139 if (CollectionUtils.isEmpty(errors)) {
143 for (ErrorCode error : errors) {
144 LOGGER.error(error.message());