42ea681b1bd62e91b98e95d2e34fea9f3f9f3180
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.core.validation.types;
18
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;
24
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.function.BiPredicate;
31 import java.util.stream.Collectors;
32
33 public class GlobalValidationContext {
34
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;
39
40   public ErrorMessageCode getMessageCode() {
41     return messageCode;
42   }
43
44   public void setMessageCode(ErrorMessageCode messageCode) {
45     this.messageCode = messageCode;
46   }
47
48
49   /**
50    * Add message.
51    *
52    * @param fileName the file name
53    * @param level    the level
54    * @param message  the message
55    */
56   public void addMessage(String fileName, ErrorLevel level, String message) {
57
58     LOGGER.debug("'{}' [{}] in file '{}' ", message, level, fileName);
59
60     if (fileContextMap.containsKey(fileName)) {
61       fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
62           .setMessage(level.toString() + ": " + message).setLevel(level).create();
63     } else {
64       MessageContainer messageContainer;
65       synchronized (this) {
66         messageContainer = messageContainerMap.computeIfAbsent(fileName, k -> new MessageContainer());
67       }
68       messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
69           .setLevel(level).create();
70     }
71   }
72
73   /**
74    * Gets file content.
75    *
76    * @param fileName the file name
77    * @return the file content
78    */
79   public Optional<InputStream> getFileContent(String fileName) {
80     FileValidationContext fileContext = fileContextMap.get(fileName);
81     if (fileContext == null || fileContext.isEmpty()) {
82       return Optional.empty();
83     }
84     return Optional.of(fileContext.getContent());
85   }
86
87   public void addFileContext(String fileName, byte[] fileContent) {
88     fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
89   }
90
91   /**
92    * Gets context message containers.
93    *
94    * @return the context message containers
95    */
96   public Map<String, MessageContainer> getContextMessageContainers() {
97
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;
108   }
109
110   public Map<String, FileValidationContext> getFileContextMap() {
111     return fileContextMap;
112   }
113
114   public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
115     return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
116         .collect(Collectors.toList());
117   }
118
119   public Collection<String> getFiles() {
120     return this.getFileContextMap().keySet();
121   }
122
123 }