[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.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;
30
31 import java.io.InputStream;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.function.BiPredicate;
37 import java.util.stream.Collectors;
38
39 public class GlobalValidationContext {
40
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<>();
44
45   /**
46    * Add message.
47    *
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
53    */
54   public void addMessage(String fileName, ErrorLevel level, String message,
55                          String targetServiceName, String errorDescription) {
56
57     printLog(fileName, message, level, targetServiceName, errorDescription);
58
59     if (fileContextMap.containsKey(fileName)) {
60       fileContextMap.get(fileName).getMessageContainer().getMessageBuilder()
61           .setMessage(level.toString() + ": " + message).setLevel(level).create();
62     } else {
63 //      if (CommonMethods.isEmpty(fileName)) {
64 //        fileName = SdcCommon.UPLOAD_FILE;
65 //      }
66       MessageContainer messageContainer;
67       synchronized (this) {
68         messageContainer = messageContainerMap.get(fileName);
69         if (messageContainer == null) {
70           messageContainer = new MessageContainer();
71           messageContainerMap.put(fileName, messageContainer);
72         }
73       }
74       messageContainer.getMessageBuilder().setMessage(level.toString() + ": " + message)
75           .setLevel(level).create();
76     }
77   }
78
79   /**
80    * Gets file content.
81    *
82    * @param fileName the file name
83    * @return the file content
84    */
85   public Optional<InputStream> getFileContent(String fileName) {
86     FileValidationContext fileContext = fileContextMap.get(fileName);
87     if (fileContext == null || fileContext.isEmpty()) {
88       return Optional.empty();
89     }
90     return Optional.of(fileContext.getContent());
91   }
92
93   public void addFileContext(String fileName, byte[] fileContent) {
94     fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent));
95   }
96
97   /**
98    * Gets context message containers.
99    *
100    * @return the context message containers
101    */
102   public Map<String, MessageContainer> getContextMessageContainers() {
103
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;
114   }
115
116   public Map<String, FileValidationContext> getFileContextMap() {
117     return fileContextMap;
118   }
119
120   private void printLog(String fileName, String message, ErrorLevel level, String targetServiceName,
121                         String errorDescription) {
122
123     String messageToPrint = message + " in file[" + fileName + "]";
124     MdcUtil.setValuesForMdc(LoggerConstants.TARGET_ENTITY_API, targetServiceName, level.name(),
125         LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription);
126
127     switch (level) {
128       case ERROR:
129         logger.error(messageToPrint);
130         break;
131       case WARNING:
132         logger.warn(messageToPrint);
133         break;
134       case INFO:
135         logger.info(messageToPrint);
136         break;
137       default:
138         break;
139     }
140   }
141
142
143   public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) {
144     return fileContextMap.keySet().stream().filter(t -> func.test(t, this))
145         .collect(Collectors.toList());
146   }
147
148   public Collection<String> getFiles() {
149     return this.getFileContextMap().keySet();
150   }
151
152 }