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.utilities.CommonMethods;
25 import org.openecomp.core.validation.interfaces.Validator;
26 import org.openecomp.sdc.common.utils.AsdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.io.InputStream;
32 import java.util.Collection;
33 import java.util.HashMap;
35 import java.util.function.BiPredicate;
36 import java.util.stream.Collectors;
38 public class GlobalValidationContext {
40 private static Logger logger = LoggerFactory.getLogger(Validator.class);
41 private Map<String, FileValidationContext> fileContextMap = new HashMap<>();
42 private Map<String, MessageContainer> messageContainerMap = new HashMap<>();
47 * @param fileName the file name
48 * @param level the level
49 * @param message the message
51 public void addMessage(String fileName, ErrorLevel level, String message) {
53 printLog(fileName, message, level);
55 if (fileContextMap.containsKey(fileName)) {
56 fileContextMap.get(fileName).getMessageContainer().getMessageBuilder().setMessage(message)
57 .setLevel(level).create();
59 if (CommonMethods.isEmpty(fileName)) {
60 fileName = AsdcCommon.UPLOAD_FILE;
62 MessageContainer messageContainer;
64 messageContainer = messageContainerMap.get(fileName);
65 if (messageContainer == null) {
66 messageContainer = new MessageContainer();
67 messageContainerMap.put(fileName, messageContainer);
70 messageContainer.getMessageBuilder().setMessage(message).setLevel(level).create();
77 * @param fileName the file name
78 * @return the file content
80 public InputStream getFileContent(String fileName) {
81 FileValidationContext fileContext = fileContextMap.get(fileName);
82 if (fileContext == null || fileContext.isEmpty()) {
85 return fileContext.getContent();
88 public void addFileContext(String fileName, byte[] fileContent) {
89 fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
93 * Gets context message containers.
95 * @return the context message containers
97 public Map<String, MessageContainer> getContextMessageContainers() {
99 Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
100 fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
101 .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
102 entry -> contextMessageContainer.put(
103 entry.getKey(), entry.getValue().getMessageContainer()));
104 messageContainerMap.entrySet().stream()
105 .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
106 .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue()));
107 return contextMessageContainer;
110 public Map<String, FileValidationContext> getFileContextMap() {
111 return fileContextMap;
114 private void printLog(String fileName, String message, ErrorLevel level) {
116 String messageToPrint = message + " in file[" + fileName + "]";
120 logger.error(messageToPrint);
123 logger.warn(messageToPrint);
126 logger.info(messageToPrint);
133 public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
134 return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
135 .collect(Collectors.toList());
138 public Collection<String> getFiles() {
139 return this.getFileContextMap().keySet();