Upgrade Vulnerable Direct Dependencies [snakeyaml]
[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.types.GlobalValidationContext;
26 import org.openecomp.core.validation.types.MessageContainer;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.validation.Validator;
29 import org.openecomp.sdc.validation.services.ValidationFactory;
30
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35
36 public class ValidationManagerImpl implements ValidationManager {
37
38   private GlobalValidationContext globalContext;
39   private List<Validator> validators;
40
41   public ValidationManagerImpl() {
42     globalContext = new GlobalValidationContext();
43     validators = ValidationFactory.getValidators();
44   }
45
46   @Override
47   public Map<String, List<ErrorMessage>> validate() {
48     for (Validator validator : validators) {
49       if(Objects.nonNull(validator)) {
50         validator.validate(globalContext);
51       }
52     }
53     return convertMessageContainsToErrorMessage(globalContext.getContextMessageContainers());
54   }
55
56   @Override
57   public void addFile(String fileName, byte[] fileContent) {
58     globalContext.addFileContext(fileName, fileContent);
59   }
60
61   @Override
62   public void updateGlobalContext(GlobalValidationContext globalContext) {
63     this.globalContext = globalContext;
64   }
65
66   private Map<String, List<ErrorMessage>> convertMessageContainsToErrorMessage(
67       Map<String, MessageContainer> contextMessageContainers) {
68     Map<String, List<ErrorMessage>> errors = new HashMap<>();
69     contextMessageContainers.entrySet().stream()
70         .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList()))
71         .forEach(entry -> errors.put(entry.getKey(), entry.getValue().getErrorMessageList()));
72     return errors;
73   }
74
75 }