2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.core.validation.types;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.validation.ErrorMessageCode;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.logging.context.MdcUtil;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import java.io.InputStream;
33 import java.util.Collection;
34 import java.util.HashMap;
36 import java.util.Optional;
37 import java.util.function.BiPredicate;
38 import java.util.stream.Collectors;
40 public class GlobalValidationContext {
42 private static Logger logger = (Logger) LoggerFactory.getLogger(GlobalValidationContext.class);
43 private Map<String, FileValidationContext> fileContextMap = new HashMap<>();
44 private Map<String, MessageContainer> messageContainerMap = new HashMap<>();
45 private ErrorMessageCode messageCode;
47 public ErrorMessageCode getMessageCode() {
51 public void setMessageCode(ErrorMessageCode messageCode) {
52 this.messageCode = messageCode;
59 * @param fileName the file name
60 * @param level the level
61 * @param message the message
62 * @param targetServiceName the target service name
63 * @param errorDescription the error details
65 public void addMessage(String fileName, ErrorLevel level, String message,
66 String targetServiceName, String errorDescription) {
68 printLog(fileName, message, level, targetServiceName, errorDescription);
70 if (fileContextMap.containsKey(fileName)) {
71 fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
72 .setMessage(level.toString() + ": " + message).setLevel(level).create();
74 // if (CommonMethods.isEmpty(fileName)) {
75 // fileName = SdcCommon.UPLOAD_FILE;
77 MessageContainer messageContainer;
79 messageContainer = messageContainerMap.get(fileName);
80 if (messageContainer == null) {
81 messageContainer = new MessageContainer();
82 messageContainerMap.put(fileName, messageContainer);
85 messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
86 .setLevel(level).create();
93 * @param fileName the file name
94 * @return the file content
96 public Optional<InputStream> getFileContent(String fileName) {
97 FileValidationContext fileContext = fileContextMap.get(fileName);
98 if (fileContext == null || fileContext.isEmpty()) {
99 return Optional.empty();
101 return Optional.of(fileContext.getContent());
104 public void addFileContext(String fileName, byte[] fileContent) {
105 fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
109 * Gets context message containers.
111 * @return the context message containers
113 public Map<String, MessageContainer> getContextMessageContainers() {
115 Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
116 fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
117 .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
118 entry -> contextMessageContainer.put(
119 entry.getKey(), entry.getValue()
120 .getMessageContainer()));
121 messageContainerMap.entrySet().stream()
122 .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
123 .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue()));
124 return contextMessageContainer;
127 public Map<String, FileValidationContext> getFileContextMap() {
128 return fileContextMap;
131 private void printLog(String fileName, String message, ErrorLevel level, String targetServiceName,
132 String errorDescription) {
134 String messageToPrint = message + " in file[" + fileName + "]";
135 MdcUtil.setValuesForMdc(LoggerConstants.TARGET_ENTITY_API, targetServiceName, level.name(),
136 LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription);
140 logger.error(messageToPrint);
143 logger.warn(messageToPrint);
146 logger.info(messageToPrint);
154 public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
155 return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
156 .collect(Collectors.toList());
159 public Collection<String> getFiles() {
160 return this.getFileContextMap().keySet();