Removed MdcUtil
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-api / src / main / java / org / openecomp / core / validation / types / GlobalValidationContext.java
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    * @param targetService  the target service name
56    * @param description  validation event details
57    */
58   public void addMessage(String fileName, ErrorLevel level, String message,
59                          String targetService, String description) {
60
61     LOGGER.debug("{}: {} [{}]. Target: {}. Description: {}", fileName, message, level, targetService, description);
62
63     if (fileContextMap.containsKey(fileName)) {
64       fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
65           .setMessage(level.toString() + ": " + message).setLevel(level).create();
66     } else {
67       MessageContainer messageContainer;
68       synchronized (this) {
69         messageContainer = messageContainerMap.computeIfAbsent(fileName, k -> new MessageContainer());
70       }
71       messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
72           .setLevel(level).create();
73     }
74   }
75
76   /**
77    * Gets file content.
78    *
79    * @param fileName the file name
80    * @return the file content
81    */
82   public Optional<InputStream> getFileContent(String fileName) {
83     FileValidationContext fileContext = fileContextMap.get(fileName);
84     if (fileContext == null || fileContext.isEmpty()) {
85       return Optional.empty();
86     }
87     return Optional.of(fileContext.getContent());
88   }
89
90   public void addFileContext(String fileName, byte[] fileContent) {
91     fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
92   }
93
94   /**
95    * Gets context message containers.
96    *
97    * @return the context message containers
98    */
99   public Map<String, MessageContainer> getContextMessageContainers() {
100
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;
111   }
112
113   public Map<String, FileValidationContext> getFileContextMap() {
114     return fileContextMap;
115   }
116
117   public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
118     return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
119         .collect(Collectors.toList());
120   }
121
122   public Collection<String> getFiles() {
123     return this.getFileContextMap().keySet();
124   }
125
126 }