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.sdc.validation.impl;
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;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Objects;
38 public class ValidationManagerImpl implements ValidationManager {
40 private static Logger logger = (Logger) LoggerFactory.getLogger(ValidationManagerImpl.class);
42 private GlobalValidationContext globalContext;
43 private List<Validator> validators;
45 public ValidationManagerImpl() {
46 globalContext = new GlobalValidationContext();
47 validators = ValidationFactory.getValidators();
51 public Map<String, List<ErrorMessage>> validate() {
52 for (Validator validator : validators) {
53 if(Objects.nonNull(validator)) {
54 validator.validate(globalContext);
57 return convertMessageContainsToErrorMessage(globalContext.getContextMessageContainers());
61 public void addFile(String fileName, byte[] fileContent) {
62 globalContext.addFileContext(fileName, fileContent);
66 public void updateGlobalContext(GlobalValidationContext globalContext) {
67 this.globalContext = globalContext;
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()));