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