Add collaboration feature
[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  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.validation.types;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.validation.ErrorMessageCode;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.logging.context.MdcUtil;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
31
32 import java.io.InputStream;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.function.BiPredicate;
38 import java.util.stream.Collectors;
39
40 public class GlobalValidationContext {
41
42   private static Logger logger = (Logger) LoggerFactory.getLogger(GlobalValidationContext.class);
43   private Map<String, FileValidationContext> fileContextMap = new HashMap<>();
44   private Map<String, MessageContainer> messageContainerMap = new HashMap<>();
45   private ErrorMessageCode messageCode;
46
47   public ErrorMessageCode getMessageCode() {
48     return messageCode;
49   }
50
51   public void setMessageCode(ErrorMessageCode messageCode) {
52     this.messageCode = messageCode;
53   }
54
55
56   /**
57    * Add message.
58    *
59    * @param fileName the file name
60    * @param level    the level
61    * @param message  the message
62    * @param targetServiceName  the target service name
63    * @param errorDescription  the error details
64    */
65   public void addMessage(String fileName, ErrorLevel level, String message,
66                          String targetServiceName, String errorDescription) {
67
68     printLog(fileName, message, level, targetServiceName, errorDescription);
69
70     if (fileContextMap.containsKey(fileName)) {
71       fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
72           .setMessage(level.toString() + ": " + message).setLevel(level).create();
73     } else {
74 //      if (CommonMethods.isEmpty(fileName)) {
75 //        fileName = SdcCommon.UPLOAD_FILE;
76 //      }
77       MessageContainer messageContainer;
78       synchronized (this) {
79         messageContainer = messageContainerMap.get(fileName);
80         if (messageContainer == null) {
81           messageContainer = new MessageContainer();
82           messageContainerMap.put(fileName, messageContainer);
83         }
84       }
85       messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
86           .setLevel(level).create();
87     }
88   }
89
90   /**
91    * Gets file content.
92    *
93    * @param fileName the file name
94    * @return the file content
95    */
96   public Optional<InputStream> getFileContent(String fileName) {
97     FileValidationContext fileContext = fileContextMap.get(fileName);
98     if (fileContext == null || fileContext.isEmpty()) {
99       return Optional.empty();
100     }
101     return Optional.of(fileContext.getContent());
102   }
103
104   public void addFileContext(String fileName, byte[] fileContent) {
105     fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
106   }
107
108   /**
109    * Gets context message containers.
110    *
111    * @return the context message containers
112    */
113   public Map<String, MessageContainer> getContextMessageContainers() {
114
115     Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
116     fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
117         .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
118           entry -> contextMessageContainer.put(
119              entry.getKey(), entry.getValue()
120              .getMessageContainer()));
121     messageContainerMap.entrySet().stream()
122         .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
123         .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue()));
124     return contextMessageContainer;
125   }
126
127   public Map<String, FileValidationContext> getFileContextMap() {
128     return fileContextMap;
129   }
130
131   private void printLog(String fileName, String message, ErrorLevel level, String targetServiceName,
132                         String errorDescription) {
133
134     String messageToPrint = message + " in file[" + fileName + "]";
135     MdcUtil.setValuesForMdc(LoggerConstants.TARGET_ENTITY_API, targetServiceName, level.name(),
136         LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription);
137
138     switch (level) {
139       case ERROR:
140         logger.error(messageToPrint);
141         break;
142       case WARNING:
143         logger.warn(messageToPrint);
144         break;
145       case INFO:
146         logger.info(messageToPrint);
147         break;
148       default:
149         break;
150     }
151   }
152
153
154   public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
155     return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
156         .collect(Collectors.toList());
157   }
158
159   public Collection<String> getFiles() {
160     return this.getFileContextMap().keySet();
161   }
162
163 }