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=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.services.utils;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.datatypes.error.ErrorLevel;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
37 * Created by Talio on 12/6/2016.
39 public class CandidateServiceValidator {
41 public Optional<List<ErrorMessage>> validateFileDataStructure(FilesDataStructure filesDataStructure) {
42 if (Objects.isNull(filesDataStructure)) {
43 return Optional.empty();
45 if (validateAtLeaseOneModuleExist(filesDataStructure)) {
46 return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages.NO_MODULES_IN_MANIFEST.getErrorMessage())));
48 List<ErrorMessage> errors = new ArrayList<>();
49 for (Module module : filesDataStructure.getModules()) {
50 validateModuleHaveYaml(errors, module);
51 validateNoVolEnvWithoutVol(errors, module);
53 return Optional.of(errors);
56 private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) {
57 return CollectionUtils.isEmpty(filesDataStructure.getModules());
60 private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) {
61 if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) {
62 errors.add(new ErrorMessage(ErrorLevel.ERROR,
63 ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(), module.getName())));
67 private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) {
68 if (StringUtils.isEmpty(module.getYaml())) {
69 errors.add(new ErrorMessage(ErrorLevel.ERROR,
70 ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(), module.getName())));