push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-core / src / main / java / org / openecomp / sdc / validation / impl / ValidationManagerImpl.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.sdc.validation.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.validation.api.ValidationManager;
25 import org.openecomp.core.validation.interfaces.Validator;
26 import org.openecomp.core.validation.types.GlobalValidationContext;
27 import org.openecomp.core.validation.types.MessageContainer;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.validation.utils.ValidationConfigurationManager;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 public class ValidationManagerImpl implements ValidationManager {
38
39   private static Logger logger = LoggerFactory.getLogger(ValidationManagerImpl.class);
40   private GlobalValidationContext globalContext;
41   private List<Validator> validators;
42
43   public ValidationManagerImpl() {
44     globalContext = new GlobalValidationContext();
45     validators = ValidationConfigurationManager.initValidators();
46   }
47
48   @Override
49   public void addFile(String fileName, byte[] fileContent) {
50     globalContext.addFileContext(fileName, fileContent);
51   }
52
53   @Override
54   public Map<String, List<ErrorMessage>> validate() {
55     for (Validator validator : validators) {
56       validator.validate(globalContext);
57     }
58     return convertMessageContainsToErrorMessage(globalContext.getContextMessageContainers());
59   }
60
61   private Map<String, List<ErrorMessage>> convertMessageContainsToErrorMessage(
62       Map<String, MessageContainer> contextMessageContainers) {
63     Map<String, List<ErrorMessage>> errors = new HashMap<>();
64     contextMessageContainers.entrySet().stream()
65         .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
66         .forEach(entry -> errors.put(entry.getKey(), entry.getValue().getErrorMessageList()));
67     return errors;
68   }
69 }