[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / utils / VendorSoftwareProductUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.utils;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.enrichment.types.ArtifactType;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.common.errors.Messages;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
35 import org.openecomp.sdc.logging.types.LoggerConstants;
36 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
37 import org.openecomp.sdc.logging.types.LoggerServiceName;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
40 import org.slf4j.MDC;
41
42 import java.io.File;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 public class VendorSoftwareProductUtils {
50   protected static Logger logger =
51       (Logger) LoggerFactory.getLogger(VendorSoftwareProductUtils.class);
52
53   /**
54    * Add file names to upload file response.
55    *
56    * @param fileContentMap     the file content map
57    * @param uploadFileResponse the upload file response
58    */
59   public static void addFileNamesToUploadFileResponse(FileContentHandler fileContentMap,
60                                                       OrchestrationTemplateActionResponse uploadFileResponse) {
61     uploadFileResponse.setFileNames(new ArrayList<>());
62     for (String filename : fileContentMap.getFileList()) {
63       if (!new File(filename).isDirectory()) {
64         uploadFileResponse.addNewFileToList(filename);
65       }
66     }
67     uploadFileResponse.removeFileFromList(SdcCommon.MANIFEST_NAME);
68   }
69
70   /**
71    * Validate raw zip data.
72    *
73    * @param uploadedFileData the uploaded file data
74    * @param errors           the errors
75    */
76   public static void validateRawZipData(byte[] uploadedFileData,
77                                         Map<String, List<ErrorMessage>> errors) {
78     if (uploadedFileData.length == 0) {
79       MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
80       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors).add(
81           new ErrorMessage(ErrorLevel.ERROR,
82               Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage()));
83     }
84   }
85
86   /**
87    * Validate content zip data.
88    *
89    * @param contentMap the content map
90    * @param errors     the errors
91    */
92   public static void validateContentZipData(FileContentHandler contentMap,
93                                             Map<String, List<ErrorMessage>> errors) {
94     MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
95     if (contentMap == null) {
96       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors).add(
97           new ErrorMessage(ErrorLevel.ERROR,
98               Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()));
99
100     } else if (contentMap.getFileList().size() == 0) {
101       ErrorMessage.ErrorMessageUtil.addMessage(SdcCommon.UPLOAD_FILE, errors)
102           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
103     }
104   }
105
106
107   /**
108    * Filter non trap or poll artifacts map.
109    *
110    * @param artifacts the artifacts
111    * @return the map
112    */
113   public static Map<ArtifactType, String> filterNonTrapOrPollArtifacts(
114       Collection<MibEntity> artifacts) {
115     Map<ArtifactType, String> artifactTypeToFilename = new HashMap<>();
116
117     for (MibEntity entity : artifacts) {
118       if (isTrapOrPoll(entity.getType())) {
119         artifactTypeToFilename.put(entity.getType(), entity.getArtifactName());
120       }
121     }
122
123     return artifactTypeToFilename;
124   }
125
126
127   private static boolean isTrapOrPoll(ArtifactType type) {
128     return type.equals(ArtifactType.SNMP_POLL) || type.equals(ArtifactType.SNMP_TRAP);
129   }
130
131
132   /**
133    * Sets errors into logger.
134    *
135    * @param errors            the errors
136    * @param serviceName       the service name
137    * @param targetServiceName the target service name
138    */
139   public static void setErrorsIntoLogger(Map<String, List<ErrorMessage>> errors,
140                                          LoggerServiceName serviceName, String targetServiceName) {
141     MdcDataErrorMessage mdcDataErrorMessage =
142         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
143             ErrorLevel.ERROR.name(), null, null);
144     mdcDataErrorMessage.setMdcValues();
145
146     if (MapUtils.isEmpty(errors)) {
147       return;
148     }
149
150     for (Map.Entry<String, List<ErrorMessage>> listEntry : errors.entrySet()) {
151       List<ErrorMessage> errorList = listEntry.getValue();
152       for (ErrorMessage message : errorList) {
153         logger.error(message.getMessage());
154       }
155     }
156   }
157
158   /**
159    * Sets errors into logger.
160    *
161    * @param errors            the errors
162    * @param serviceName       the service name
163    * @param targetServiceName the target service name
164    */
165   public static void setErrorsIntoLogger(Collection<ErrorCode> errors,
166                                          LoggerServiceName serviceName, String targetServiceName) {
167     MdcDataErrorMessage mdcDataErrorMessage =
168         new MdcDataErrorMessage(targetServiceName, LoggerConstants.TARGET_ENTITY_DB,
169             ErrorLevel.ERROR.name(), null, null);
170     mdcDataErrorMessage.setMdcValues();
171
172     if (CollectionUtils.isEmpty(errors)) {
173       return;
174     }
175
176     for (ErrorCode error : errors) {
177       logger.error(error.message());
178     }
179   }
180
181 }