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.core.validation.types;
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.sdc.datatypes.error.ErrorLevel;
22 import org.openecomp.sdc.logging.api.Logger;
23 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.HashMap;
29 import java.util.Optional;
30 import java.util.function.BiPredicate;
31 import java.util.stream.Collectors;
33 public class GlobalValidationContext {
35 private static final Logger LOGGER = LoggerFactory.getLogger(GlobalValidationContext.class);
36 private final Map<String, FileValidationContext> fileContextMap = new HashMap<>();
37 private final Map<String, MessageContainer> messageContainerMap = new HashMap<>();
38 private ErrorMessageCode messageCode;
40 public ErrorMessageCode getMessageCode() {
44 public void setMessageCode(ErrorMessageCode messageCode) {
45 this.messageCode = messageCode;
52 * @param fileName the file name
53 * @param level the level
54 * @param message the message
56 public void addMessage(String fileName, ErrorLevel level, String message) {
58 LOGGER.debug("'{}' [{}] in file '{}' ", message, level, fileName);
60 if (fileContextMap.containsKey(fileName)) {
61 fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
62 .setMessage(level.toString() + ": " + message).setLevel(level).create();
64 MessageContainer messageContainer;
66 messageContainer = messageContainerMap.computeIfAbsent(fileName, k -> new MessageContainer());
68 messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
69 .setLevel(level).create();
76 * @param fileName the file name
77 * @return the file content
79 public Optional<InputStream> getFileContent(String fileName) {
80 FileValidationContext fileContext = fileContextMap.get(fileName);
81 if (fileContext == null || fileContext.isEmpty()) {
82 return Optional.empty();
84 return Optional.of(fileContext.getContent());
87 public void addFileContext(String fileName, byte[] fileContent) {
88 fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
92 * Gets context message containers.
94 * @return the context message containers
96 public Map<String, MessageContainer> getContextMessageContainers() {
98 Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
99 fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
100 .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
101 entry -> contextMessageContainer.put(
102 entry.getKey(), entry.getValue()
103 .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 public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
115 return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
116 .collect(Collectors.toList());
119 public Collection<String> getFiles() {
120 return this.getFileContextMap().keySet();