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
55 * @param targetService the target service name
56 * @param description validation event details
58 public void addMessage(String fileName, ErrorLevel level, String message,
59 String targetService, String description) {
61 LOGGER.debug("{}: {} [{}]. Target: {}. Description: {}", fileName, message, level, targetService, description);
63 if (fileContextMap.containsKey(fileName)) {
64 fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
65 .setMessage(level.toString() + ": " + message).setLevel(level).create();
67 MessageContainer messageContainer;
69 messageContainer = messageContainerMap.computeIfAbsent(fileName, k -> new MessageContainer());
71 messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
72 .setLevel(level).create();
79 * @param fileName the file name
80 * @return the file content
82 public Optional<InputStream> getFileContent(String fileName) {
83 FileValidationContext fileContext = fileContextMap.get(fileName);
84 if (fileContext == null || fileContext.isEmpty()) {
85 return Optional.empty();
87 return Optional.of(fileContext.getContent());
90 public void addFileContext(String fileName, byte[] fileContent) {
91 fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
95 * Gets context message containers.
97 * @return the context message containers
99 public Map<String, MessageContainer> getContextMessageContainers() {
101 Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
102 fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
103 .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
104 entry -> contextMessageContainer.put(
105 entry.getKey(), entry.getValue()
106 .getMessageContainer()));
107 messageContainerMap.entrySet().stream()
108 .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
109 .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue()));
110 return contextMessageContainer;
113 public Map<String, FileValidationContext> getFileContextMap() {
114 return fileContextMap;
117 public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
118 return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
119 .collect(Collectors.toList());
122 public Collection<String> getFiles() {
123 return this.getFileContextMap().keySet();