2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.openecomp.core.validation.types;
 
  23 import org.apache.commons.collections4.CollectionUtils;
 
  24 import org.openecomp.sdc.logging.api.Logger;
 
  25 import org.openecomp.sdc.logging.api.LoggerFactory;
 
  26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
 
  27 import org.openecomp.sdc.logging.context.MdcUtil;
 
  28 import org.openecomp.sdc.logging.types.LoggerConstants;
 
  29 import org.openecomp.sdc.logging.types.LoggerErrorCode;
 
  31 import java.io.InputStream;
 
  32 import java.util.Collection;
 
  33 import java.util.HashMap;
 
  35 import java.util.Optional;
 
  36 import java.util.function.BiPredicate;
 
  37 import java.util.stream.Collectors;
 
  39 public class GlobalValidationContext {
 
  41   private static Logger logger = (Logger) LoggerFactory.getLogger(GlobalValidationContext.class);
 
  42   private Map<String, FileValidationContext> fileContextMap = new HashMap<>();
 
  43   private Map<String, MessageContainer> messageContainerMap = new HashMap<>();
 
  48    * @param fileName the file name
 
  49    * @param level    the level
 
  50    * @param message  the message
 
  51    * @param targetServiceName  the target service name
 
  52    * @param errorDescription  the error details
 
  54   public void addMessage(String fileName, ErrorLevel level, String message,
 
  55                          String targetServiceName, String errorDescription) {
 
  57     printLog(fileName, message, level, targetServiceName, errorDescription);
 
  59     if (fileContextMap.containsKey(fileName)) {
 
  60       fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
 
  61           .setMessage(level.toString() + ": " + message).setLevel(level).create();
 
  63 //      if (CommonMethods.isEmpty(fileName)) {
 
  64 //        fileName = SdcCommon.UPLOAD_FILE;
 
  66       MessageContainer messageContainer;
 
  68         messageContainer = messageContainerMap.get(fileName);
 
  69         if (messageContainer == null) {
 
  70           messageContainer = new MessageContainer();
 
  71           messageContainerMap.put(fileName, messageContainer);
 
  74       messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
 
  75           .setLevel(level).create();
 
  82    * @param fileName the file name
 
  83    * @return the file content
 
  85   public Optional<InputStream> getFileContent(String fileName) {
 
  86     FileValidationContext fileContext = fileContextMap.get(fileName);
 
  87     if (fileContext == null || fileContext.isEmpty()) {
 
  88       return Optional.empty();
 
  90     return Optional.of(fileContext.getContent());
 
  93   public void addFileContext(String fileName, byte[] fileContent) {
 
  94     fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
 
  98    * Gets context message containers.
 
 100    * @return the context message containers
 
 102   public Map<String, MessageContainer> getContextMessageContainers() {
 
 104     Map<String, MessageContainer> contextMessageContainer = new HashMap<>();
 
 105     fileContextMap.entrySet().stream().filter(entry -> CollectionUtils
 
 106         .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach(
 
 107           entry -> contextMessageContainer.put(
 
 108              entry.getKey(), entry.getValue()
 
 109              .getMessageContainer()));
 
 110     messageContainerMap.entrySet().stream()
 
 111         .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
 
 112         .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue()));
 
 113     return contextMessageContainer;
 
 116   public Map<String, FileValidationContext> getFileContextMap() {
 
 117     return fileContextMap;
 
 120   private void printLog(String fileName, String message, ErrorLevel level, String targetServiceName,
 
 121                         String errorDescription) {
 
 123     String messageToPrint = message + " in file[" + fileName + "]";
 
 124     MdcUtil.setValuesForMdc(LoggerConstants.TARGET_ENTITY_API, targetServiceName, level.name(),
 
 125         LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription);
 
 129         logger.error(messageToPrint);
 
 132         logger.warn(messageToPrint);
 
 135         logger.info(messageToPrint);
 
 143   public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
 
 144     return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
 
 145         .collect(Collectors.toList());
 
 148   public Collection<String> getFiles() {
 
 149     return this.getFileContextMap().keySet();